From 6f9113a463176497f6e14f9a0775d2bd37536f35 Mon Sep 17 00:00:00 2001 From: guilevi Date: Sat, 6 Nov 2021 01:42:07 +0100 Subject: [PATCH] New shit for wordbyword --- .DS_Store | Bin 12292 -> 10244 bytes modules/.DS_Store | Bin 10244 -> 10244 bytes modules/wordbyword/index.js | 17 +++++++++++++---- package-lock.json | 27 +++++++++++++++++++++++++++ package.json | 1 + strings/en.json | 5 +++-- 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/.DS_Store b/.DS_Store index 22e958e497d1665f51c2269e82ce1799e2690311..6165530f29c1ab1f1868ef97f53fa9407612efbf 100644 GIT binary patch delta 414 zcmYk2UnoOy6vxkZyKLOWC1o_Z+n7Bdf8&WIw6aA=LXEg|k;(4n&kHgkd*PxyNqCj{ zdGp_c7cZ12rNoOjFJ2^awUSfkd{3v&Ip03v?r@VxM5qb&6%+loMlF9;;EA|IA~PQq zOF7=;X_tHbBQ5fjq$X$e7yASu7?7H!;T}Z}NwqzS6z~bDAz2!4993lhNT5v`3utP0 z6=}z`U|*a8I#p54*_P>qwAk>kLi?LV4yq&XLLso^hB@pP9KN| z4*}V5q7Y@MLOtAQfCpZ*p&cFQL>D9sK*k`3pdf?^OkxUin8yMZv4m9=tYIA+*upk; zu!}wH;{Z_{;uxnm#|18Ng&W-C5zly0ujB1lW}mi9m^8zPnKtsw>P+WYp6Q8Snc)~O zI&|u-{e*qn+%S|?SzK^?mhy%e~7{TSRD^ibX^$_ch)9zj;0pBU{AwpV;*V zT`Czo(BFoajKr7v%?zrFvYPovd6A@i$PbW10TwlcX{3b?dg!H(eg+sM$}l6u7-fuc zCYWTFITl!Ci51pZXM;@=Y_r2Id+c+_5yzZz#yJ;Ua>X?_+;YbQPrUNRr--`omIox) zO`ARm{pQWW?;=vz^yz%F$k5gPXJ1KXidk+wX-}`|YTi96x3b%CZKjGJIZl0~DP-3*G{!@=(_9x0*$qwMcs$E2u8OpFMn|V-oZ0x?mKrK7!_86J Vg}M)PXeYSp(Zyz-p>6ZY^9MxTSLpx% diff --git a/modules/.DS_Store b/modules/.DS_Store index d00d811f7f56642ddc492f433ee911ec64fcb123..7447c900b8120d26e0f56995de8af0fe56edfc50 100644 GIT binary patch delta 509 zcmZn(XbIS$E1S|*{3mpX`W0P7Pg=$M9104ku3xnEPP7YCJee0n3?3~=Z{4NGCU}S{Q47^Yp zMs-gv6cn4xD|mV!?B2XcNR?$kES@M{#xb$Mcr&}gFP6y@gf~sTD*9<495Q*L_%Z;Q C$!z)n delta 55 zcmZn(XbIS$D=_()$l1xsg2I#k2%HA7H#Z9@v49zg;>8@B*%f}VY`!mYj1jCZQM>>E Dt%n#^ 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