Added subscribe and unsubscribe commands to the buttsbot channel
This commit is contained in:
parent
0fa257237e
commit
f43cfcb370
232
bot.js
232
bot.js
|
|
@ -36,131 +36,145 @@ function isNumeric(n) {
|
||||||
|
|
||||||
// Event handlers
|
// Event handlers
|
||||||
function onMessageHandler (channel, userstate, message, self) {
|
function onMessageHandler (channel, userstate, message, self) {
|
||||||
var trimmedMessage = message.trim();
|
var message = message.trim();
|
||||||
|
|
||||||
if (trimmedMessage.startsWith("!buttsbot")) {
|
if (channel === "#butt5b0t") {
|
||||||
if (userstate['room-id'] === userstate['user-id'] || userstate['mod']) {
|
var messageChannel = "#" + userstate['username'];
|
||||||
|
|
||||||
var parts = trimmedMessage.split(' ');
|
if (message === "!subscribe") {
|
||||||
var action = parts[1];
|
client.join(messageChannel);
|
||||||
var value = parts[2];
|
client.say(channel, "Joined channel " + messageChannel);
|
||||||
|
}
|
||||||
|
|
||||||
switch (action) {
|
if (message === "!unsibscribe") {
|
||||||
case "config":
|
client.part(messageChannel);
|
||||||
client.say(channel, "Chance: " + config.chance + "% - Limit: " + config.limit + " - Word: " + config.word + " - Ignored Users: " + config.ignoredUsers.join(', '));
|
client.say(channel, "Left channel " + messageChannel);
|
||||||
break;
|
|
||||||
case "word":
|
|
||||||
config.word = value;
|
|
||||||
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
|
|
||||||
client.say(channel, "Word set to " + value);
|
|
||||||
break;
|
|
||||||
case "chance":
|
|
||||||
if (isNumeric(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, "Chance set to " + value + "%");
|
|
||||||
} else {
|
|
||||||
client.say(channel, "Expected a number, got " + value);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "ignore":
|
|
||||||
if (value && value !== "") {
|
|
||||||
console.log(`* ignore: value ok`);
|
|
||||||
value = value.replace("@", "");
|
|
||||||
if (!config.ignoredUsers.includes(value)) {
|
|
||||||
console.log(`* ignore: value not already ignored`);
|
|
||||||
config.ignoredUsers.push(value);
|
|
||||||
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
|
|
||||||
}
|
|
||||||
client.say(channel, "User " + value + " will be ignored from now on.");
|
|
||||||
} else {
|
|
||||||
client.say(channel, "Excepted username, got empty value");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "rmignore":
|
|
||||||
if (value && value !== "") {
|
|
||||||
console.log(`* rmignore: value ok`);
|
|
||||||
value = value.replace("@", "");
|
|
||||||
if (config.ignoredUsers.includes(value)) {
|
|
||||||
console.log(`* rmignore: value ignored`);
|
|
||||||
|
|
||||||
let index = config.ignoredUsers.indexOf(value);
|
|
||||||
config.ignoredUsers.splice(index, 1);
|
|
||||||
|
|
||||||
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
|
|
||||||
}
|
|
||||||
client.say(channel, "User " + value + " will no longer be ignored.");
|
|
||||||
} else {
|
|
||||||
client.say(channel, "Expected username, got empty value");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "limit":
|
|
||||||
if (isNumeric(value)) {
|
|
||||||
config.limit = parseInt(value);
|
|
||||||
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
|
|
||||||
client.say(channel, "Set new limit: 1 butt per " + value + " words");
|
|
||||||
} else {
|
|
||||||
client.say(channel, "Expected a number, got " + value);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
client.say(channel, "Action '" + action + "' not recognized.");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log(`* buttsbot: User not authorized`);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (self) return;
|
if (self) return;
|
||||||
|
|
||||||
if (trimmedMessage.startsWith("!"))
|
if (message.startsWith("!buttsbot")) {
|
||||||
return;
|
if (userstate['room-id'] === userstate['user-id'] || userstate['mod']) {
|
||||||
|
|
||||||
if (config.ignoredUsers.includes(userstate['display-name']) || config.ignoredUsers.includes(userstate['username'])) {
|
var parts = message.split(' ');
|
||||||
console.log(`* User ${userstate['username']} is on the ignore list.`);
|
var action = parts[1];
|
||||||
return;
|
var value = parts[2];
|
||||||
}
|
|
||||||
|
|
||||||
var number = Math.random() * 100;
|
switch (action) {
|
||||||
if (number <= config.chance) {
|
case "config":
|
||||||
|
client.say(channel, "Chance: " + config.chance + "% - Limit: " + config.limit + " - Word: " + config.word + " - 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 "chance":
|
||||||
|
if (isNumeric(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, "Chance set to " + value + "%");
|
||||||
|
} else {
|
||||||
|
client.say(channel, "Expected a number, got " + value);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "ignore":
|
||||||
|
if (value && value !== "") {
|
||||||
|
console.log(`* ignore: value ok`);
|
||||||
|
value = value.replace("@", "");
|
||||||
|
if (!config.ignoredUsers.includes(value)) {
|
||||||
|
console.log(`* ignore: value not already ignored`);
|
||||||
|
config.ignoredUsers.push(value);
|
||||||
|
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
|
||||||
|
}
|
||||||
|
client.say(channel, "User " + value + " will be ignored from now on.");
|
||||||
|
} else {
|
||||||
|
client.say(channel, "Excepted username, got empty value");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "rmignore":
|
||||||
|
if (value && value !== "") {
|
||||||
|
console.log(`* rmignore: value ok`);
|
||||||
|
value = value.replace("@", "");
|
||||||
|
if (config.ignoredUsers.includes(value)) {
|
||||||
|
console.log(`* rmignore: value ignored`);
|
||||||
|
|
||||||
var words = trimmedMessage.split(' ');
|
let index = config.ignoredUsers.indexOf(value);
|
||||||
var syllables = words.map(syllabify);
|
config.ignoredUsers.splice(index, 1);
|
||||||
|
|
||||||
var buttCount = Math.ceil(words.length / config.limit);
|
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
|
||||||
|
}
|
||||||
var randomNumbersUsed = [];
|
client.say(channel, "User " + value + " will no longer be ignored.");
|
||||||
|
} else {
|
||||||
for (var i = 0; i < buttCount; i++) {
|
client.say(channel, "Expected username, got empty value");
|
||||||
|
}
|
||||||
var random = randomIntFromInterval(1, words.length);
|
break;
|
||||||
|
case "limit":
|
||||||
while (randomNumbersUsed.includes(random)) random = randomIntFromInterval(1, words.length);
|
if (isNumeric(value)) {
|
||||||
|
config.limit = parseInt(value);
|
||||||
randomNumbersUsed.push(random);
|
fs.writeFileSync('buttsbot.config', JSON.stringify(config));
|
||||||
|
client.say(channel, "Set new limit: 1 butt per " + value + " words");
|
||||||
var word = syllables[random - 1];
|
} else {
|
||||||
|
client.say(channel, "Expected a number, got " + value);
|
||||||
if (word) {
|
}
|
||||||
var randomSyllable = randomIntFromInterval(1, word.length);
|
break;
|
||||||
|
default:
|
||||||
var firstLetterOfSyllable = word[randomSyllable - 1][0];
|
client.say(channel, "Action '" + action + "' not recognized.");
|
||||||
|
|
||||||
word[randomSyllable - 1] = firstLetterOfSyllable == firstLetterOfSyllable.toUpperCase() ? config.word.toUpperCase() : config.word;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
console.log(`* buttsbot: User not authorized`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (message.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 newMessage = "";
|
var number = Math.random() * 100;
|
||||||
|
if (number <= config.chance) {
|
||||||
|
|
||||||
syllables.forEach((s) => {
|
var words = message.split(' ');
|
||||||
if (s != null)
|
var syllables = words.map(syllabify);
|
||||||
newMessage += s.join('') + ' ';
|
|
||||||
});
|
|
||||||
|
|
||||||
client.say(channel, newMessage.trim());
|
var buttCount = Math.ceil(words.length / config.limit);
|
||||||
|
|
||||||
|
var randomNumbersUsed = [];
|
||||||
|
|
||||||
|
for (var i = 0; i < buttCount; i++) {
|
||||||
|
|
||||||
|
var random = randomIntFromInterval(1, words.length);
|
||||||
|
|
||||||
|
while (randomNumbersUsed.includes(random)) random = randomIntFromInterval(1, words.length);
|
||||||
|
|
||||||
|
randomNumbersUsed.push(random);
|
||||||
|
|
||||||
|
var word = syllables[random - 1];
|
||||||
|
|
||||||
|
if (word) {
|
||||||
|
var randomSyllable = randomIntFromInterval(1, word.length);
|
||||||
|
|
||||||
|
var firstLetterOfSyllable = word[randomSyllable - 1][0];
|
||||||
|
|
||||||
|
word[randomSyllable - 1] = firstLetterOfSyllable == firstLetterOfSyllable.toUpperCase() ? config.word.toUpperCase() : config.word;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var newMessage = "";
|
||||||
|
|
||||||
|
syllables.forEach((s) => {
|
||||||
|
if (s != null)
|
||||||
|
newMessage += s.join('') + ' ';
|
||||||
|
});
|
||||||
|
|
||||||
|
client.say(channel, newMessage.trim());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user