diff --git a/.DS_Store b/.DS_Store index 22e958e..6165530 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/modules/.DS_Store b/modules/.DS_Store index d00d811..7447c90 100644 Binary files a/modules/.DS_Store and b/modules/.DS_Store differ diff --git a/modules/wordbyword/index.js b/modules/wordbyword/index.js index b63568d..bb798c0 100644 --- a/modules/wordbyword/index.js +++ b/modules/wordbyword/index.js @@ -1,5 +1,6 @@ const printf = require('printf') const isStringInt = require('is-string-int'); +const levenshtein = require('fast-levenshtein') module.exports = function (bot, api) { bot.currentWBW = ""; @@ -12,19 +13,27 @@ module.exports = function (bot, api) { } else { if (isStringInt(args[1])) { let story = await api.db.get('select * from WBWStories where story_id=?', parseInt(args[1])) - if(!story) { + if (!story) { 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"'); - if (message.author.id == lastUser.value) { - return api.respond(message, printf(api.strings.WBW_LAST_USER)) + 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)) + } } else { bot.currentWBW += args[1] + ' '; api.respond(message, printf(api.strings.WBW_NEW_WORD)) - api.speak(message.member.voice.channel, bot.currentWBW) + let toSay = bot.currentWBW.indexOf(".") == -1 ? bot.currentWBW : bot.currentWBW.slice(bot.currentWBW.lastIndexOf('.') + 2); + api.speak(message.member.voice.channel, toSay) await api.db.run('update BotState set value=? where key="last_wbw"', message.author.id); } } diff --git a/package-lock.json b/package-lock.json index d24b5b3..4b570ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@google-cloud/text-to-speech": "^3.1.3", "discord.js": "^12.5.3", "dotenv": "^8.2.0", + "fast-levenshtein": "^3.0.0", "google-tts-api": "^2.0.2", "is-string-int": "^1.0.1", "microsoft-cognitiveservices-speech-sdk": "^1.16.0", @@ -730,11 +731,24 @@ "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==" }, + "node_modules/fast-levenshtein": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-3.0.0.tgz", + "integrity": "sha512-hKKNajm46uNmTlhHSyZkmToAc56uZJwYq7yrciZjqOxnlfQwERDQJmHPUp7m1m9wx8vgOe8IaCKZ5Kv2k1DdCQ==", + "dependencies": { + "fastest-levenshtein": "^1.0.7" + } + }, "node_modules/fast-text-encoding": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/fast-text-encoding/-/fast-text-encoding-1.0.3.tgz", "integrity": "sha512-dtm4QZH9nZtcDt8qJiOH9fcQd1NAgi+K1O2DbE6GG1PPCK/BWfOH3idCTRQ4ImXRUOyopDEgDEnVEE7Y/2Wrig==" }, + "node_modules/fastest-levenshtein": { + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/fastest-levenshtein/-/fastest-levenshtein-1.0.12.tgz", + "integrity": "sha512-On2N+BpYJ15xIC974QNVuYGMOlEVt4s0EOI3wwMqOmK1fdDY+FN/zltPV8vosq4ad4c/gJ1KHScUn/6AWIgiow==" + }, "node_modules/follow-redirects": { "version": "1.13.3", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", @@ -2760,11 +2774,24 @@ "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==" }, + "fast-levenshtein": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-3.0.0.tgz", + "integrity": "sha512-hKKNajm46uNmTlhHSyZkmToAc56uZJwYq7yrciZjqOxnlfQwERDQJmHPUp7m1m9wx8vgOe8IaCKZ5Kv2k1DdCQ==", + "requires": { + "fastest-levenshtein": "^1.0.7" + } + }, "fast-text-encoding": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/fast-text-encoding/-/fast-text-encoding-1.0.3.tgz", "integrity": "sha512-dtm4QZH9nZtcDt8qJiOH9fcQd1NAgi+K1O2DbE6GG1PPCK/BWfOH3idCTRQ4ImXRUOyopDEgDEnVEE7Y/2Wrig==" }, + "fastest-levenshtein": { + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/fastest-levenshtein/-/fastest-levenshtein-1.0.12.tgz", + "integrity": "sha512-On2N+BpYJ15xIC974QNVuYGMOlEVt4s0EOI3wwMqOmK1fdDY+FN/zltPV8vosq4ad4c/gJ1KHScUn/6AWIgiow==" + }, "follow-redirects": { "version": "1.13.3", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", diff --git a/package.json b/package.json index eb9123c..b896bdd 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@google-cloud/text-to-speech": "^3.1.3", "discord.js": "^12.5.3", "dotenv": "^8.2.0", + "fast-levenshtein": "^3.0.0", "google-tts-api": "^2.0.2", "is-string-int": "^1.0.1", "microsoft-cognitiveservices-speech-sdk": "^1.16.0", diff --git a/strings/en.json b/strings/en.json index a7e0db2..5d0b450 100644 --- a/strings/en.json +++ b/strings/en.json @@ -9,8 +9,9 @@ "TOO_MANY_ARGUMENTS": "too many arguments for command.", "CURRENT_STORY": "Here's the current story: %s", "NO_STORY": "No story in progress at the moment.", - "WBW_LAST_USER": "You were the last user to add a word to the story. Wait for someone else to add another one.", - "WBW_NEW_WORD": "Added to story. It now reads:", + "WBW_REPLACED": "Replaced %s with %s", + "WBW_TOO_DIFFERENT": "This word is too different from the last word.", + "WBW_NEW_WORD": "Added to story.", "WBW_RESET": "The story has been reset.", "WBW_INVALID_ID": "No story with that ID." } \ No newline at end of file