tardis-bot/modules/wordbyword/index.js

47 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2021-11-05 01:15:50 +00:00
const printf = require('printf')
const isStringInt = require('is-string-int');
2021-11-06 00:42:07 +00:00
const levenshtein = require('fast-levenshtein')
2021-11-05 01:15:50 +00:00
module.exports = function (bot, api) {
bot.currentWBW = "";
api.registerCommand('wbw', async (args, message) => {
if (args.length == 1) {
return api.respond(message, bot.currentWBW ? printf(api.strings.CURRENT_STORY, bot.currentWBW) : printf(api.strings.NO_STORY));
}
if (args.length > 2) {
return api.respond(message, printf(api.strings.TOO_MANY_ARGUMENTS));
} else {
if (isStringInt(args[1])) {
let story = await api.db.get('select * from WBWStories where story_id=?', parseInt(args[1]))
2021-11-06 00:42:07 +00:00
if (!story) {
2021-11-05 01:15:50 +00:00
return api.respond(message, api.strings.WBW_INVALID_ID)
} else {
return api.respond(message, story.story_text)
}
} else {
let lastUser = await api.db.get('select value from BotState where key="last_wbw"');
2021-11-06 00:42:07 +00:00
if (message.author.id == lastUser.value && bot.currentWBW != "") {
let lastWord = (bot.currentWBW.indexOf(" ") == bot.currentWBW.lastIndexOf(" ")) ? bot.currentWBW : bot.currentWBW.slice(bot.currentWBW.slice(0,-1).lastIndexOf(' ') + 1);
console.log(args[1], lastWord, levenshtein.get(args[1], lastWord))
if (levenshtein.get(args[1], lastWord) <= 3) {
bot.currentWBW = bot.currentWBW.replace(new RegExp(lastWord + "([^" + lastWord + "]*)$"), args[1] + "$1 ");
api.respond(message, printf(api.strings.WBW_REPLACED, lastWord, args[1]))
} else {
return api.respond(message, printf(api.strings.WBW_TOO_DIFFERENT))
}
2021-11-05 01:15:50 +00:00
} else {
bot.currentWBW += args[1] + ' ';
2021-11-05 01:32:33 +00:00
api.respond(message, printf(api.strings.WBW_NEW_WORD))
2021-11-06 00:42:07 +00:00
let toSay = bot.currentWBW.indexOf(".") == -1 ? bot.currentWBW : bot.currentWBW.slice(bot.currentWBW.lastIndexOf('.') + 2);
api.speak(message.member.voice.channel, toSay)
2021-11-05 01:15:50 +00:00
await api.db.run('update BotState set value=? where key="last_wbw"', message.author.id);
}
}
}
})
api.registerCommand('newwbw', async (args, message) => {
await api.db.run('insert into WBWStories (story_text) values(?)', bot.currentWBW);
bot.currentWBW = '';
api.respond(message, printf(api.strings.WBW_RESET))
})
}