2.0.2 • Published 1 year ago
twitch-chat.js v2.0.2
Twitch-chat.js
A simple Twitch API wrapper
Contents
Installing
npm i twitch-chat.js --save
- Node version "16.18.0" or newer is required
Getting Starting
const { Client } = require('twitch-chat.js');
const client = new Client({
client_name: 'YOUR_BOT_NAME',
client_id: 'CLIENT_ID',
channels: ['foo']
})
.on('ready', () => {
console.log(`Hello ${client.user.login_name}`)
})
.on('message', message => {
if(message.channel.type !== 'whisper') return;
if(message.content === '!ping') return message.channel.send(`Websocket: ${client.ping}ms`)
})
client.login('BOT_TOKEN')
Creating a Command and Event Handlers
const { Client, Collection } = require('twitch-chat.js');
const { Command, Event } = require('@twitchchatjs/handlers');
const client = new Client({
client_name: 'YOUR_BOT_NAME',
client_id: 'CLIENT_ID',
channels: ['foo']
})
client.commands = new Collection()
client.aliases = new Collection()
/*
Handlers Examples
'./path/to/folder/'
Example:
*/
let commands = new Command(client, './test/commands/')
let event = new Event(client, './test/events/')
event.load()
commands.load()
client.login('BOT_TOKEN')
Note: You need to create a folder for your commands and events
Option (Handler) | Argument | Description | Default |
---|---|---|---|
subFolder | Boolean | Enable/Disable subfolder | false |
First Event
module.exports = class Message {
run(message) {
if(message.channel.type == 'whisper') return;
if(!message.content.startsWith('!')) return;
const args = message.content
.trim()
.slice(1)
.split(/ +/g);
const command = args.shift().toLowerCase();
let commands = global.client.commands.get(command) || global.client.aliases.get(command)
let ctx = {
client: global.client,
message: message,
args: args
}
if(!commands) return;
commands.run(ctx)
}
}
First Command
module.exports = class PingCommand {
constructor() {
this.name = 'ping',
this.aliases = ['pong'],
this.run = this.run
}
run(ctx) {
return ctx.message.channel.send(`Pong! WebSocket ping: ${ctx.client.ping}ms`)
}
}
Advenced
You can create a ".env" file for set
CLIENT_TOKEN
&CLIENT_ID
Creating an env file
CLIENT_TOKEN=TOKEN_HERE
CLIENT_ID=CLIENT_ID_HERE
Example main file
const { Client } = require('twitch-chat.js');
const client = new Client({
client_name: 'CLIENT_NAME',
channels: ['foo']
})
client.login()
Releases
Packages
twitch-chat.js@2.0.0
Bug Fixed
join()
doesn't work correctly and stop the code
Added (Methods)
Channel.edit()
Channel.deleteMessages()
Channel.moderators()
Client.user.block()
Client.user.unblock()
Message.channel.stream.ads()
Message.channel.stream.ban()
Message.channel.stream.block()
Message.channel.stream.thumbnailURL()
Message.channel.stream.unban()
Message.channel.stream.user.ban()
Message.channel.stream.user.unban()
Message.delete()
Added (Properties)
Client.id
Client.token
Message.channel.stream.user
User.email
Added (Events)
punishment
unpunishment
Removed (ENV and options)
TWITCH_NAME
CHANNELS
Change (ENV and options)
TWITCH_TOKEN
toCLIENT_TOKEN
OptionsClient = { name: 'foo'}
toOptionsClient = { client_name: 'foo'}
Change (Methods)
Message.channel.createClip()
toMessage.channel.stream.createClip()
Message.channel.clip()
toMessage.channel.stream.clip()
Client.join()
toClient.user.join()