From 98260ccdb98b7b5b8fa287c83d75443fa8d8d1a2 Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Sun, 13 Mar 2022 13:51:37 -0600 Subject: added createcommand function, will also test later --- twitch-bot/bot.js | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/twitch-bot/bot.js b/twitch-bot/bot.js index f4a2140..0399a1b 100644 --- a/twitch-bot/bot.js +++ b/twitch-bot/bot.js @@ -64,12 +64,15 @@ try { } var modCommand = false; +var message, fullMessage; function onMessageHandler (target, context, msg, self) { if (self) { return; } // Ignore messages from the bot const user = context.username; + // save full message if adding/editing command + fullMessage = msg.trim(); msg = msg.toLowerCase(); - var message = msg.trim(); + message = msg.trim(); // link protection stuff, only enables when true in settings.json var findUrlEndings = false; @@ -134,14 +137,20 @@ function commands (target, commandName, user, mods) { } } -function modCommands(target, commandName, isMod) { +function modCommands(target, commandName, message, isMod) { var valid = false; - mod_commands.forEach(command => { - if (message.search(command.command) !== -1) { + test_mod_commands = ["addcommand", "editcommand"]; + test_mod_commands.forEach(command => { + if (commandName.search(command) !== -1) { if (!isMod) { client.say(target, `Only moderators can run this command...`); return true; } else { + if (command === "addcommand") { + createCommand(target); + } else if (command === "editcommand") { + editCommand(target); + } client.say(target, `${command.reply}`); valid = true; return true; @@ -151,6 +160,32 @@ function modCommands(target, commandName, isMod) { return valid; } +function createCommand(target) { + chat_commands.forEach(command => { + if (fullMessage[1].search(command.command) !== -1) { + client.say(target, `The command ${settings.command_char}${fullMessage[1]} already exists. Use !editcommand to change it's contents.`); + return false; + } + }); + var command_reply = ""; + for (int i = 2; i < fullMessage.length; i++) { + command_reply = command_reply + fullMessage[i] + " "; + chat_commands.push(['command': fullMessage[1], 'reply': command_reply]); + try { + const data = JSON.stringify(chat_commands, null, 4); + fs.writeFileSync('user.json', data); + console.log('* Added command ' + settings.command_char + fullMessage[1]); + client.say(target, `Successfully added command: ${settings.command_char}${fullMessage[1]}`); + } catch (err) { + console.error('An error occured trying to run !addcommand: ' + err); + client.say(target, `Something went wrong adding this command, please try again later.`); + } +} + +function editCommand(target) { + +} + function reactions (target, message, user) { reacts.forEach(react => { if (message.search(react.trigger) !== -1) { @@ -173,7 +208,7 @@ function linkProtect() { trustedString = trustedString + `,${user}`; trustedString = trustedString.replace(/(\r\n|\n|\r)/gm, ""); trustedUsers.push(user); - fs.writeFile('/home/bryson/git/bryzinga-bot/trusted-users', `${trustedString}`, function (err) { + fs.writeFile('trusted_users.txt', `${trustedString}`, function (err) { if (err) return console.log(err); console.log(`* ${user} is now a trusted chatter.`); }); -- cgit v1.2.3