Files
YouTube.js/examples/livechat/index.js
LuanRT 1d62e469a9 refactor: rewrite Comments Section logic (#88)
* feat: add core comments section classes

* chore: update type declarations

* chore: fix linter warnings

* style: fix linter

* chore: update tests

* chore(tests): fix typo

* chore(tests): fix typo x2

* fix(tests): `getReplies()` method is only present in `CommentThread` and not `Comment`

* chore(tests): fix comment id path

* chore(tests): remove outdated code

* chore(tests): fix results path

* chore: enforce code style

* chore: update type declarations

* docs: add examples and documentation

* chore(docs): fix paths

* chore(docs): fix more paths

* chore(docs): fix `Comments.js` path

* chore(docs): fix typo

* chore(docs): mention example file

* chore(examples): fix imports

* chore(examples): fix typo
2022-07-02 19:55:33 -03:00

62 lines
1.8 KiB
JavaScript

import Innertube from 'youtubei.js';
const session = await new Innertube();
const info = await session.getInfo('video_id');
const livechat = await info.getLiveChat();
livechat.ev.on('start', (initial_data) => {
/**
* Initial info is what you see when you first open a Live Chat — this is; inital actions, account's info and so on.
*/
console.info(`Hey ${initial_data.viewer_name}, welcome to ${info.basic_info.channel.name}\'s Live Chat!`);
});
livechat.ev.on('chat-update', (action) => {
/**
* An action represents what is being added to
* the live chat. All actions have a `type` property,
* including their item (if the action has an item).
*
* Below are a few examples of how this can be used.
*/
if (action.type === 'AddChatItemAction') {
const hours = new Date(action.item.timestamp).toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit'
});
switch (action.item.type) {
case 'LiveChatTextMessage':
console.info(
`${hours} - ${action.item.author.name.toString()}:\n` +
`${action.item.message.toString()}\n`
);
break;
case 'LiveChatPaidMessage':
console.info(`
${hours} - ${action.item.author.name.toString()}
${action.item.purchase_amount}
`);
break;
default:
console.debug(action);
}
}
if (action.type === 'MarkChatItemAsDeletedAction') {
console.warn(`Message ${action.target_item_id} just got deleted and should be replaced with ${action.delete_state_message.toString()}!`);
}
});
livechat.ev.on('metadata-update', (metadata) => {
console.info(`
VIEWS: ${metadata.views.view_count.toString()}
LIKES: ${metadata.likes.default_text}
DATE: ${metadata.date.date_text}
`);
});
livechat.start();