Created
February 14, 2022 08:34
-
-
Save iamnotacoder-djs/2f6d66f5fac13b36011f4f9ec3fe9640 to your computer and use it in GitHub Desktop.
tmi.js Twitch Chat Bot Example | Command handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const help = { | |
name: "coin", | |
usage: "Flip a coin" | |
}; | |
module.exports = { | |
help: help, | |
messageExecute(client, opts) { | |
exec(client, opts); | |
} | |
} | |
async function exec(client, {channel, tags, message, command, args} = { channel: `#`, tags: {}, args: [], message, command}) { | |
const x = (Math.floor(Math.random() * 2) == 0) + 0; // Without +0 we are getting true or false | |
const coin = ["⛀ (Tails)", "⛂ (Eagle)"]; | |
client.say(channel, `@${tags.username}, came up: ${coin[x]}`); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const help = { | |
name: "commands", | |
usage: "Show available commandsream schedule" | |
}; | |
module.exports = { | |
help: help, | |
messageExecute(client, opts) { | |
exec(client, opts); | |
} | |
} | |
async function exec(client, {channel, tags, message, command, args} = { channel: `#`, tags: {}, args: [], message, command}) { | |
let commands = ``; | |
client.commands.forEach((cmd) => { | |
commands += ` | !${cmd.help.name} - ${cmd.help.usage}`; | |
}); | |
client.say(channel, `@${tags.username}, available commands ${commands}`); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tmi = require('tmi.js'), | |
fs = require('fs'); | |
const config = { | |
token: "your oauth token", | |
prefix: "!", | |
channel_name: "channelname", | |
debug: true | |
}; | |
var notifications = { | |
message: ["How are you?", "Don't forget to follow up ;)", "Get on YouTube"], | |
next_message: 0, | |
chat_activity: 0 | |
}; | |
const client = new tmi.Client({ | |
options: { | |
debug: config.debug | |
}, | |
connection: { | |
secure: true, | |
reconnect: true | |
}, | |
channels: [ config.channel_name ], | |
identity: { | |
username: config.channel_name, | |
password: config.token | |
} | |
}); | |
client.commands = new Map(); | |
client.connect(); | |
loadCommands(); | |
client.on('message', (channel, tags, message, self) => { | |
notifications.chat_activity++; | |
notifications.channel = channel; | |
if (!message.startsWith(config.prefix) || self) return; | |
const args = message.slice(config.prefix.length).trim().split(/ +/g); | |
const command = args.shift(); | |
if (command == "hi") { | |
return client.say(channel, `@${tags.username}, hello to you too!`) | |
} | |
const cmd = client.commands.get(`${command}`); | |
if (cmd) { | |
cmd.messageExecute(client, { | |
channel: channel, | |
tags: tags, | |
message: message, | |
command: command, | |
args: args | |
}); | |
} | |
}); | |
async function loadCommands() { | |
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); | |
for (const file of commandFiles) { | |
const command = require(`./commands/${file}`); | |
client.commands.set(command.help.name, command); | |
} | |
if (config.debug) console.log(`Loaded ${commandFiles.length} commands.`) | |
} | |
const timeout = function() { | |
if (config.debug) console.log(`Trying to send a scheduled message`); | |
if (notifications.chat_activity >= 20 && notifications.channel != undefined) { | |
notifications.chat_activity = 0; | |
client.say(notifications.channel, notifications.messages[notifications.next_message]); | |
if (config.debug) console.log(`Message sent: [${notifications.messages[notifications.next_message]}]`); | |
notifications.next_message++; | |
if (notifications.next_message >= notifications.messages.length) { | |
notifications.next_message = 0; | |
} | |
} | |
setTimeout(timeout, 1000 * 60 * 5); // 5 minutes | |
} | |
timeout(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const help = { | |
name: "schedule", | |
usage: "Show stream schedule" | |
}; | |
module.exports = { | |
help: help, | |
messageExecute(client, opts) { | |
exec(client, opts); | |
} | |
} | |
async function exec(client, {channel, tags, message, command, args} = { channel: `#`, tags: {}, args: [], message, command}) { | |
client.say(channel, `@${tags.username}, channel schedule is available at the link: <link here>`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment