Replaced timer by a percentage chance / Added option to change the word

This commit is contained in:
Marco Loewe 2021-08-29 18:10:19 +02:00
parent b4031b43f9
commit c566d3e9f3
2 changed files with 38 additions and 35 deletions

71
bot.js
View File

@ -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());
}
}
}

View File

@ -1 +1 @@
{"timer":120,"limit":10,"ignoredUsers":["StreamElements","Nightbot"]}
{"word": "butt","timer":120,"chance":20,"ignoredUsers":["StreamElements","Nightbot"]}