This repository has been archived on 2025-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
lset/twitch-bot/bot.js

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-03-11 15:00:05 -07:00
const tmi = require('tmi.js');
2022-03-11 16:23:51 -07:00
const fs = require('fs');
// read settings and reactions json files
2022-03-11 18:23:24 -07:00
try {
2022-03-11 20:48:52 -07:00
var settings = JSON.parse(fs.readFileSync('./settings.json', 'utf8'));
var reacts = JSON.parse(fs.readFileSync('./reacts.json', 'utf8'));
2022-03-11 23:13:00 -07:00
var chat_commands = JSON.parse(fs.readFileSync('./commands.json', 'utf8'));
2022-03-11 20:48:52 -07:00
var mod_commands = JSON.parse(fs.readFileSync('./mod_commands.json', 'utf8'));
2022-03-11 18:23:24 -07:00
} catch (err) {
console.error("An error occured trying to read the files for the Twitch bot: " + err);
}
2022-03-11 15:00:05 -07:00
const opts = {
identity: {
2022-03-11 16:23:51 -07:00
username: settings.bot_username,
password: settings.bot_token
2022-03-11 15:00:05 -07:00
},
2022-03-11 16:23:51 -07:00
channels: settings.channels
2022-03-11 15:00:05 -07:00
};
var modString = "";
var mods;
try {
2022-03-11 23:13:00 -07:00
modString = fs.readFileSync('mods.txt', 'utf8');
2022-03-11 15:00:05 -07:00
modString = modString.replace(/(\r\n|\n|\r)/gm, "");
mods = modString.split(",");
} catch (err) {
console.error(err)
}
var modCommand = false;
function onMessageHandler (target, context, msg, self) {
if (self) { return; } // Ignore messages from the bot
2022-03-11 16:23:51 -07:00
const user = context.username;
2022-03-11 15:00:05 -07:00
msg = msg.toLowerCase();
var message = msg.trim();
2022-03-11 16:23:51 -07:00
if (message.charAt(0) === settings.command_char) {
2022-03-11 15:00:05 -07:00
message = message.substr(1);
2022-03-11 16:23:51 -07:00
valid = commands(target, message, user, mods);
} else {
reactions(target, message, user);
2022-03-11 15:00:05 -07:00
}
}
function commands (target, commandName, user, mods) {
modCommand = false;
var valid = false;
var isMod = false;
if (mods.indexOf(user) >= 0) isMod = true;
2022-03-11 16:23:51 -07:00
if (commandName === "help") {
var finalString = settings.command_char + "help " + settings.command_char + "about ";
2022-03-11 23:13:00 -07:00
chat_commands.forEach(command => {
finalString = finalString + settings.command_char + command.command + " ";
2022-03-11 16:23:51 -07:00
});
client.say(target, `Here is the list of commands: ${finalString}`);
2022-03-11 23:13:00 -07:00
valid = true;
} else if (commandName === "about") {
client.say(target, `This bot was stolen from https://github.com/brysonsteck/lset under the MIT License and configured by your streamer, ${settings.your_username}!`);
valid = true;
2022-03-11 16:23:51 -07:00
} else {
2022-03-11 23:13:00 -07:00
chat_commands.forEach(command => {
if (commandName.search(command.command) !== -1) {
2022-03-11 16:23:51 -07:00
client.say(target, `${command.reply}`);
valid = true;
2022-03-11 17:47:59 -07:00
return true;
2022-03-11 16:23:51 -07:00
}
});
2022-03-11 15:00:05 -07:00
}
if (valid === false) {
2022-03-11 16:23:51 -07:00
if (isMod) {
valid = modCommands(target, commandName, isMod);
}
2022-03-11 15:00:05 -07:00
}
2022-03-11 16:23:51 -07:00
2022-03-11 15:00:05 -07:00
if (valid) {
console.log(`* ${user} executed !${commandName}`);
} else {
if (modCommand === false) {
2022-03-11 16:23:51 -07:00
client.say(target, `@${user} Unknown command "${settings.command_char}${commandName}". Type ${settings.command_char}help for all commands.`);
2022-03-11 15:00:05 -07:00
}
console.warn(`! ${user} tried to execute unknown/banned command !${commandName}`);
}
}
function modCommands(target, commandName, isMod) {
var valid = false;
2022-03-11 16:23:51 -07:00
mod_commands.forEach(command => {
if (message.search(command.command) !== -1) {
if (!isMod) {
client.say(target, `Only moderators can run this command...`);
2022-03-11 17:47:59 -07:00
return true;
2022-03-11 16:23:51 -07:00
} else {
client.say(target, `${command.reply}`);
valid = true;
2022-03-11 17:47:59 -07:00
return true;
2022-03-11 16:23:51 -07:00
}
2022-03-11 15:00:05 -07:00
}
2022-03-11 16:23:51 -07:00
});
2022-03-11 15:00:05 -07:00
return valid;
}
2022-03-11 16:23:51 -07:00
function reactions (target, message, user) {
reacts.forEach(react => {
if (message.search(react.trigger) !== -1) {
client.say(target, `${react.reply}`);
}
});
2022-03-11 15:00:05 -07:00
}
2022-03-11 23:13:00 -07:00
// Twitch bot initialization
const client = new tmi.client(opts);
// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
// Connect to Twitch:
client.connect();
// Notify when connected
function onConnectedHandler (addr, port) {
console.log(`* Main Bot successfully connected to ${addr}:${port}`);
}
2022-03-11 15:00:05 -07:00