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

35
bot.js
View File

@ -1,6 +1,5 @@
const fs = require('fs'); const fs = require('fs');
const tmi = require('tmi.js'); const tmi = require('tmi.js');
const moment = require('moment');
// Twitch bot options // Twitch bot options
const optsFile = fs.readFileSync('twitch.config'); const optsFile = fs.readFileSync('twitch.config');
@ -16,8 +15,6 @@ let config = JSON.parse(configFile);
console.log(`* Startup: Loaded config: ${config}`); console.log(`* Startup: Loaded config: ${config}`);
let lastMessageButtified = 0;
// Client // Client
client.on('message', onMessageHandler); client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler); client.on('connected', onConnectedHandler);
@ -52,13 +49,22 @@ function onMessageHandler (channel, userstate, message, self) {
switch (action) { switch (action) {
case "config": 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; break;
case "timer": case "timer":
if (isNumeric(value)) { 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)); fs.writeFileSync('buttsbot.config', JSON.stringify(config));
client.say(channel, "Set new timer to " + value + " seconds.") client.say(channel, "Chance set to " + value + "%");
} else { } else {
client.say(channel, "Expected a number, got " + value); client.say(channel, "Expected a number, got " + value);
} }
@ -110,18 +116,16 @@ function onMessageHandler (channel, userstate, message, self) {
console.log(`* buttsbot: User not authorized`); console.log(`* buttsbot: User not authorized`);
} }
} else { } else {
if (trimmedMessage.startsWith("!"))
return;
if (config.ignoredUsers.includes(userstate['display-name']) || config.ignoredUsers.includes(userstate['username'])) { if (config.ignoredUsers.includes(userstate['display-name']) || config.ignoredUsers.includes(userstate['username'])) {
console.log(`* User ${userstate['username']} is on the ignore list.`); console.log(`* User ${userstate['username']} is on the ignore list.`);
return; return;
} }
var checkDate = moment().subtract(config.timer, 'seconds'); var number = Math.random() * 100;
if (number <= config.chance) {
if (checkDate < lastMessageButtified)
return;
if (trimmedMessage.startsWith("!"))
return;
var words = trimmedMessage.split(' '); var words = trimmedMessage.split(' ');
var syllables = words.map(syllabify); var syllables = words.map(syllabify);
@ -145,7 +149,7 @@ function onMessageHandler (channel, userstate, message, self) {
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;
} }
} }
@ -157,8 +161,7 @@ function onMessageHandler (channel, userstate, message, self) {
}); });
client.say(channel, newMessage.trim()); client.say(channel, newMessage.trim());
}
lastMessageButtified = moment();
} }
} }

View File

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