Added possibilty for multiple configurations

This commit is contained in:
Marco Loewe 2021-08-29 21:00:52 +02:00
parent f43cfcb370
commit a8f79d06a6
2 changed files with 43 additions and 13 deletions

53
bot.js
View File

@ -11,7 +11,14 @@ const client = tmi.client(opts);
const syllableRegex = /[^aeiouy]*[aeiouy]+(?:[^aeiouy]*$|[^aeiouy](?=[^aeiouy]))?/gi;
let configFile = fs.readFileSync('buttsbot.config')
let config = JSON.parse(configFile);
let configurations = JSON.parse(configFile);
let defaultConfig = {
word: "butt",
chance: 20,
limit: 10,
ignoredUsers: []
};
console.log(`* Startup: Loaded config: ${config}`);
@ -37,22 +44,39 @@ function isNumeric(n) {
// Event handlers
function onMessageHandler (channel, userstate, message, self) {
var message = message.trim();
var messageChannel = userstate['username'];
if (channel === "#butt5b0t") {
var messageChannel = "#" + userstate['username'];
if (message === "!subscribe") {
client.join(messageChannel);
client.join("#" + messageChannel);
client.say(channel, "Joined channel " + messageChannel);
if (!opts.channels.includes(messageChannel)) {
opts.channels.push(messageChannel);
fs.writeFileSync('twitch.config', opts);
}
if (!(messageChannel in configurations)) {
configurations[messageChannel] = defaultConfig;
fs.writeFileSync('buttsbot.config');
}
}
if (message === "!unsibscribe") {
client.part(messageChannel);
if (message === "!unsubscribe") {
client.part("#" + messageChannel);
client.say(channel, "Left channel " + messageChannel);
if (opts.channels.includes(messageChannel)) {
var indexChannel = opts.channels.indexOf(messageChannel);
opts.splice(indexChannel, 1);
fs.writeFileSync('twitch.config', opts);
}
}
} else {
if (self) return;
let config = (messageChannel in configurations) ? configurations[messageChannel] : defaultConfig;
if (message.startsWith("!buttsbot")) {
if (userstate['room-id'] === userstate['user-id'] || userstate['mod']) {
@ -66,7 +90,8 @@ function onMessageHandler (channel, userstate, message, self) {
break;
case "word":
config.word = value;
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
configurations[messageChannel] = config;
fs.writeFileSync('buttsbot.config', JSON.stringify(configurations));
client.say(channel, "Word set to " + value);
break;
case "chance":
@ -76,7 +101,8 @@ function onMessageHandler (channel, userstate, message, self) {
if (value < 0)
value = 0;
config.chance = parseInt(value);
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
configurations[messageChannel] = config;
fs.writeFileSync('buttsbot.config', JSON.stringify(configurations));
client.say(channel, "Chance set to " + value + "%");
} else {
client.say(channel, "Expected a number, got " + value);
@ -89,7 +115,8 @@ function onMessageHandler (channel, userstate, message, self) {
if (!config.ignoredUsers.includes(value)) {
console.log(`* ignore: value not already ignored`);
config.ignoredUsers.push(value);
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
configurations[messageChannel] = config;
fs.writeFileSync('buttsbot.config', JSON.stringify(configurations));
}
client.say(channel, "User " + value + " will be ignored from now on.");
} else {
@ -105,8 +132,9 @@ function onMessageHandler (channel, userstate, message, self) {
let index = config.ignoredUsers.indexOf(value);
config.ignoredUsers.splice(index, 1);
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
configurations[messageChannel] = config;
fs.writeFileSync('buttsbot.config', JSON.stringify(configurations));
}
client.say(channel, "User " + value + " will no longer be ignored.");
} else {
@ -116,7 +144,8 @@ function onMessageHandler (channel, userstate, message, self) {
case "limit":
if (isNumeric(value)) {
config.limit = parseInt(value);
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
configurations[messageChannel] = config;
fs.writeFileSync('buttsbot.config', JSON.stringify(configurations));
client.say(channel, "Set new limit: 1 butt per " + value + " words");
} else {
client.say(channel, "Expected a number, got " + value);

View File

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