From c566d3e9f34d281290e8e4ea0ce33619f6e939b0 Mon Sep 17 00:00:00 2001 From: Marco Loewe Date: Sun, 29 Aug 2021 18:10:19 +0200 Subject: [PATCH] Replaced timer by a percentage chance / Added option to change the word --- bot.js | 71 ++++++++++++++++++++++++++----------------------- buttsbot.config | 2 +- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/bot.js b/bot.js index 1b735a4..af4200c 100644 --- a/bot.js +++ b/bot.js @@ -1,6 +1,5 @@ const fs = require('fs'); const tmi = require('tmi.js'); -const moment = require('moment'); // Twitch bot options const optsFile = fs.readFileSync('twitch.config'); @@ -16,8 +15,6 @@ let config = JSON.parse(configFile); console.log(`* Startup: Loaded config: ${config}`); -let lastMessageButtified = 0; - // Client client.on('message', onMessageHandler); client.on('connected', onConnectedHandler); @@ -52,13 +49,22 @@ function onMessageHandler (channel, userstate, message, self) { switch (action) { case "config": - client.say(channel, "Timer: " + config.timer + " - Limit: " + config.limit + " - Ignored Users: " + config.ignoredUsers.join(', ')); + client.say(channel, "Chance: " + config.chance + "% - Limit: " + config.limit + " - Ignored Users: " + config.ignoredUsers.join(', ')); + break; + case "word": + config.word = value; + fs.writeFileSync('buttsbot.config', JSON.stringify(config)); + client.say(channel, "Word set to " + value); break; case "timer": if (isNumeric(value)) { - config.timer = parseInt(value) + if (value > 100) + value = 100; + if (value < 0) + value = 0; + config.chance = parseInt(value); fs.writeFileSync('buttsbot.config', JSON.stringify(config)); - client.say(channel, "Set new timer to " + value + " seconds.") + client.say(channel, "Chance set to " + value + "%"); } else { client.say(channel, "Expected a number, got " + value); } @@ -110,55 +116,52 @@ function onMessageHandler (channel, userstate, message, self) { console.log(`* buttsbot: User not authorized`); } } else { + if (trimmedMessage.startsWith("!")) + return; + if (config.ignoredUsers.includes(userstate['display-name']) || config.ignoredUsers.includes(userstate['username'])) { console.log(`* User ${userstate['username']} is on the ignore list.`); return; } - var checkDate = moment().subtract(config.timer, 'seconds'); - - if (checkDate < lastMessageButtified) - return; + var number = Math.random() * 100; + if (number <= config.chance) { - if (trimmedMessage.startsWith("!")) - return; + var words = trimmedMessage.split(' '); + var syllables = words.map(syllabify); - var words = trimmedMessage.split(' '); - var syllables = words.map(syllabify); + var buttCount = Math.ceil(words.length / config.limit); - var buttCount = Math.ceil(words.length / config.limit); + var randomNumbersUsed = []; - var randomNumbersUsed = []; + for (var i = 0; i < buttCount; i++) { - for (var i = 0; i < buttCount; i++) { + var random = randomIntFromInterval(1, words.length); - var random = randomIntFromInterval(1, words.length); + while (randomNumbersUsed.includes(random)) random = randomIntFromInterval(1, words.length); - while (randomNumbersUsed.includes(random)) random = randomIntFromInterval(1, words.length); + randomNumbersUsed.push(random); - randomNumbersUsed.push(random); + var word = syllables[random - 1]; - var word = syllables[random - 1]; + if (word) { + var randomSyllable = randomIntFromInterval(1, word.length); - if (word) { - var randomSyllable = randomIntFromInterval(1, word.length); + var firstLetterOfSyllable = word[randomSyllable - 1][0]; - var firstLetterOfSyllable = word[randomSyllable - 1][0]; - - word[randomSyllable - 1] = firstLetterOfSyllable == firstLetterOfSyllable.toUpperCase() ? "BUTT" : "butt"; + word[randomSyllable - 1] = firstLetterOfSyllable == firstLetterOfSyllable.toUpperCase() ? config.word.toUpperCase() : config.word; + } } - } - var newMessage = ""; + var newMessage = ""; - syllables.forEach((s) => { + syllables.forEach((s) => { if (s != null) - newMessage += s.join('') + ' '; - }); + newMessage += s.join('') + ' '; + }); - client.say(channel, newMessage.trim()); - - lastMessageButtified = moment(); + client.say(channel, newMessage.trim()); + } } } diff --git a/buttsbot.config b/buttsbot.config index bc95180..b767654 100644 --- a/buttsbot.config +++ b/buttsbot.config @@ -1 +1 @@ -{"timer":120,"limit":10,"ignoredUsers":["StreamElements","Nightbot"]} \ No newline at end of file +{"word": "butt","timer":120,"chance":20,"ignoredUsers":["StreamElements","Nightbot"]} \ No newline at end of file