From 3cdaab8b7ab8d5a8cce27ba2bdb6209db254aa91 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Mon, 1 Aug 2022 15:55:27 -0300 Subject: [PATCH] docs: update live chat examples --- examples/livechat/README.md | 8 ++-- examples/livechat/index.js | 62 ---------------------------- examples/livechat/index.ts | 82 +++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 66 deletions(-) delete mode 100644 examples/livechat/index.js create mode 100644 examples/livechat/index.ts diff --git a/examples/livechat/README.md b/examples/livechat/README.md index 7da2a46f..ef5e30a9 100644 --- a/examples/livechat/README.md +++ b/examples/livechat/README.md @@ -34,21 +34,21 @@ Live Chat's EventEmitter. Arguments: | Type | Description | | --- | --- | - | `object` | Initial chat data, actions, info, etc. | + | `LiveChatContinuation` | Initial chat data, actions, info, etc. | - `chat-update` Arguments: | Type | Description | | --- | --- | - | `object` | Chat Action | + | `ChatAction` | Chat Action | - `metadata-update` Arguments: | Type | Description | | --- | --- | - | `object` | LiveStream Metadata | + | `LiveMetadata` | LiveStream Metadata | ### start() @@ -66,7 +66,7 @@ Sends a message. | --- | --- | --- | | text | `string` | Message content | -**Returns:** `Promise.` +**Returns:** `Promise>` ## Example See [`index.js`]('./index.js'). diff --git a/examples/livechat/index.js b/examples/livechat/index.js deleted file mode 100644 index feceb4e8..00000000 --- a/examples/livechat/index.js +++ /dev/null @@ -1,62 +0,0 @@ -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(); \ No newline at end of file diff --git a/examples/livechat/index.ts b/examples/livechat/index.ts new file mode 100644 index 00000000..d5c3cbbb --- /dev/null +++ b/examples/livechat/index.ts @@ -0,0 +1,82 @@ +import { Innertube, UniversalCache } from 'youtubei.js'; +import { LiveChatContinuation } from 'youtubei.js/dist/src/parser'; + +import LiveChat, { ChatAction, LiveMetadata } from 'youtubei.js/dist/src/parser/youtube/LiveChat'; + +import Video from 'youtubei.js/dist/src/parser/classes/Video'; +import AddChatItemAction from 'youtubei.js/dist/src/parser/classes/livechat/AddChatItemAction'; +import MarkChatItemAsDeletedAction from 'youtubei.js/dist/src/parser/classes/livechat/MarkChatItemAsDeletedAction'; + +import LiveChatTextMessage from 'youtubei.js/dist/src/parser/classes/livechat/items/LiveChatTextMessage'; +import LiveChatPaidMessage from 'youtubei.js/dist/src/parser/classes/livechat/items/LiveChatPaidMessage'; + +(async () => { + const yt = await Innertube.create({ cache: new UniversalCache() }); + + const search = await yt.search('Lofi girl live'); + const info = await yt.getInfo(search.videos[0].as(Video).id); + + const livechat = await info.getLiveChat(); + + livechat.on('start', (initial_data: LiveChatContinuation) => { + /** + * Initial info is what you see when you first open a Live Chat — this is; inital actions (pinned messages, top donations..), account's info and so on. + */ + + console.info(`Hey ${initial_data.viewer_name}, welcome to ${info.basic_info.channel.name}\'s Live Chat!`); + }); + + livechat.on('chat-update', (action: ChatAction) => { + /** + * 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.is(AddChatItemAction)) { + const item = action.as(AddChatItemAction).item; + + if (!item) + return console.info('Action did not have an item.', action); + + const hours = new Date(item.hasKey('timestamp') ? item.timestamp : Date.now()).toLocaleTimeString('en-US', { + hour: '2-digit', + minute: '2-digit' + }); + + switch (item.type) { + case 'LiveChatTextMessage': + console.info( + `${hours} - ${item.as(LiveChatTextMessage).author.name.toString()}:\n` + + `${item.as(LiveChatTextMessage).message.toString()}\n` + ); + break; + case 'LiveChatPaidMessage': + console.info( + `${hours} - ${item.as(LiveChatPaidMessage).author.name.toString()}:\n` + + `${item.as(LiveChatPaidMessage).purchase_amount}\n` + ); + break; + default: + console.debug(action); + break; + } + } + + if (action.is(MarkChatItemAsDeletedAction)) { + console.warn(`Message ${action.target_item_id} just got deleted and should be replaced with ${action.deleted_state_message.toString()}!`, '\n'); + } + }); + + livechat.on('metadata-update', (metadata: LiveMetadata) => { + console.info(` + VIEWS: ${metadata.views?.view_count.toString()} + LIKES: ${metadata.likes?.default_text} + DATE: ${metadata.date?.date_text} + `); + }); + + livechat.start(); +})(); \ No newline at end of file