YT-Livechat
Create easily chat bots for any YouTube stream's LiveChat.
Install
Install via NPM, PNPM or Yarn from repo
$ npm i yt-livechat --save$ pnpm i yt-livechat$ yarn add yt-livechat
Simple example
// Import the libconst LiveChat = ;// Or with TypeScript:// import LiveChat from "yt-livechat" // Let's do some configconst config = liveChatID: processenvLIVE_CHAT_ID || "" // ID of the LiveChat oauth: // OAuth2 keys from Google Developers Console client_id: processenvCLIENT_ID || "" client_secret: processenvCLIENT_SECRET || "" refresh_token: processenvREFRESH_TOKEN || "" ; const chat = config; // Init chat object // Register some eventschat;chat; chat; // Start polling messageschat;
Summary
- Constructor
- Properties
auth
connected
- Methods
connect()
disconnect()
reconnect()
say()
delete()
- Events
connected
disconnected
reconnected
polling
tokens
error
chat
Constructor
Usage
const LiveChat = ;const config = ... ;const chat = config;
Config structure
You might be able to find ID of the Live Chat with this API endpoint.
Properties
auth: OAuth2Client
OAuth2 client from the google-auth-library lib. You can use it to make custom authenticated requests for example.
connected: boolean
Equals true
if the lib polls messages. Else equals false
.
Methods
connect(): Promise<this>
Start polling messages from the chat.
Usage
chat
disconnect(): Promise<this>
Stop polling messages from the chat.
Usage
chat
reconnect(): Promise<this>
Re-create OAuth client and just execute disconnect()
and connect()
.
Usage
chat
say(message: string): Promise<LiveChatMessage>
Send a message.
Usage
chat
delete(messageId: string): Promise<this>
Delete a message based on his ID.
Usage
chat
Events
connected -> ()
Emitted when the lib start polling messages from YouTube. Usually after the execution of the connect()
method
Usage
chat
disconnected -> ()
Emitted when the lib stop polling messages from YouTube. Usually after the execution of the disconnect()
method
Usage
chat
reconnected -> ()
Emitted when the lib reconnects to YouTube. Usually after the execution of the reconnect()
methods.
Usage
chat
polling -> ()
Emitted when the lib poll messages from YouTube. /!\ This event is usually issued a lot of times in less than a second: if you perform too many operations, you risk running out of resources!
Usage
chat
tokens -> (tokens: Tokens)
Emitted when the access token is refreshed.
Usage
chat
Data structure
error -> (error: Error)
Emitted when an error occured.
Usage
chat
How to handle errors
The error object is very VERY VERY big because it contains all the request and response ! But just a small part can be enough :happy:
chat
Let's take a look at the result:
domain: 'youtube.liveChat' reason: 'liveChatNotFound' message: 'The live chat that you are trying to retrieve cannot be found. Check the value of the requests <code>liveChatId</code> parameter to ensure that it is correct.'
With this link to help you, I think it's enough to understand how to handle errors 😃
chat -> (message: LiveChatMessage)
Emitted when an user sent a message. (Pretty obvious...)
Usage
chat
Data structure
Take a look here : https://developers.google.com/youtube/v3/live/docs/liveChatMessages#resource
Todo List
A checked item is considered as a work in progress.
- Write unit tests
- Methods should return promises (but still support events)
- Add methods to get a Live Chat ID
Feel free to suggest features !
Contributions
You're free to contribute by publishing pull requests, issues, ideas, ...
You can also buy me a drink ❤️