From 3ed015b5841b7febe1dfc8af24c415e906274240 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sun, 19 Apr 2020 18:17:12 +0530 Subject: [PATCH 1/7] Fix typo firstName -> first_name --- commands/insult.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/insult.js b/commands/insult.js index 707efe7..6111643 100644 --- a/commands/insult.js +++ b/commands/insult.js @@ -3,7 +3,7 @@ module.exports = (random, kys, default_text, bot_text, excluded_names) => (ctx) if (ctx.message.reply_to_message) { const { from } = ctx.message.reply_to_message; - const name = from.username ? "@" + from.username : from.firstName; + const name = from.username ? "@" + from.username : from.first_name; if (name == excluded_names[0]) return bot_text; return random(kys)().replace(/##name##/g, name); From 72e69b7ac1cfa1f6c027ca389eb71e677146afe3 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 22 Apr 2020 21:56:31 +0530 Subject: [PATCH 2/7] Add /expand --- commands/expand.js | 32 ++++++++++++++++++++++++++++++++ commands/index.js | 5 +++-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 commands/expand.js diff --git a/commands/expand.js b/commands/expand.js new file mode 100644 index 0000000..80135a7 --- /dev/null +++ b/commands/expand.js @@ -0,0 +1,32 @@ +function expand(words, text) { + + const letters = text.trim().toLowerCase().split(""); + return letters.map((letter) => { + + const wordsWithLetter = words.filter(i => i.match(RegExp(`^${letter}`, "i"))); + const word = wordsWithLetter[Math.floor(Math.random() * wordsWithLetter.length)]; + return word; + + }).reduce((acc, cur) => acc + " " + cur); + +} + + +module.exports = (words = []) => (ctx) => { + + const message = ctx.message.text.replace(/^[^ ]+/, ""); + if (message) { + + return expand(words, message); + + } else { + + if (ctx.message.reply_to_message) + return expand(words, ctx.message.reply_to_message.text); + else + return "Need text to expand. Send /expand text or reply to a " + + "message with /expand"; + + } + +}; diff --git a/commands/index.js b/commands/index.js index 32affb6..d9f7c05 100644 --- a/commands/index.js +++ b/commands/index.js @@ -7,9 +7,9 @@ const absurdify = require("./absurdify"); const feedback = require("./feedback"); const media_wiki = require("./media_wiki"); const info = require("./info"); +const expand = require("./expand"); -module.exports = (bot, [ questions, kys, insults, commands_list, words ], - feedback_id, axios) => { +module.exports = (bot, [ questions, kys, insults, commands_list, words ], feedback_id, axios) => { bot.command("question", (ctx) => ctx.reply(random(questions)())); bot.command("word", (ctx) => ctx.reply(random(words)())); @@ -60,6 +60,7 @@ module.exports = (bot, [ questions, kys, insults, commands_list, words ], "https://wiki.archlinux.org/api.php")(ctx).then(x => ctx.reply(x, {parse_mode: "HTML"}))); bot.command("info", (ctx) => ctx.reply(info()(ctx), {parse_mode: "Markdown"})); + bot.command("expand", (ctx) => ctx.reply(expand(words)(ctx))); bot.command("start", (ctx) => ctx.reply("Hi, I am Quadnite. If you are " + "chatting with me in private, you are most likely doing it wrong. " + "Add me to a group for fun. To give feedback, use /feedback")); From 8f16112b2ed885d6f346fadd59a419d84edbb34e Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 22 Apr 2020 21:59:33 +0530 Subject: [PATCH 3/7] Add /expand to commands_list --- commands_list.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/commands_list.txt b/commands_list.txt index 825b8bf..9be7d5c 100644 --- a/commands_list.txt +++ b/commands_list.txt @@ -21,3 +21,4 @@ should - Should help - Need help? Go here feedback - Send feedback, suggestion for kys, insult text rate - Rate me on TGDR +expand - Expands a given abbreviation From 382579ba3fef2059b549f89f393a6fe8838c775c Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 23 Apr 2020 20:10:37 +0530 Subject: [PATCH 4/7] Ignore 's words in /expand. Return letter if nothing starting with it is found. --- commands/expand.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/commands/expand.js b/commands/expand.js index 80135a7..a9592c6 100644 --- a/commands/expand.js +++ b/commands/expand.js @@ -4,6 +4,8 @@ function expand(words, text) { return letters.map((letter) => { const wordsWithLetter = words.filter(i => i.match(RegExp(`^${letter}`, "i"))); + if (! wordsWithLetter.length) + return letter; const word = wordsWithLetter[Math.floor(Math.random() * wordsWithLetter.length)]; return word; @@ -14,6 +16,7 @@ function expand(words, text) { module.exports = (words = []) => (ctx) => { + words = words.filter(i => ! i.match(/'s$/)); const message = ctx.message.text.replace(/^[^ ]+/, ""); if (message) { From af56ffbbb362d5e33b0c3c27a9cf9ce57605b920 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 23 Apr 2020 20:30:19 +0530 Subject: [PATCH 5/7] Build dict for /expand. Replace multiple spaces with single space. --- commands/expand.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/commands/expand.js b/commands/expand.js index a9592c6..2165a38 100644 --- a/commands/expand.js +++ b/commands/expand.js @@ -1,15 +1,28 @@ function expand(words, text) { const letters = text.trim().toLowerCase().split(""); + // Build a dictionary with lowercase letters as keys + const dict = {}; + words.forEach(word => { + + if (word == "") + return; + const initial = word.split("")[0].toLowerCase(); + if (initial in dict) + dict[initial].push(word); + else + dict[initial] = [word]; + + }); return letters.map((letter) => { - const wordsWithLetter = words.filter(i => i.match(RegExp(`^${letter}`, "i"))); - if (! wordsWithLetter.length) + if (!(letter.toLowerCase() in dict)) return letter; + const wordsWithLetter = dict[letter.toLowerCase()]; const word = wordsWithLetter[Math.floor(Math.random() * wordsWithLetter.length)]; return word; - }).reduce((acc, cur) => acc + " " + cur); + }).reduce((acc, cur) => acc + " " + cur).replace(/\s{2,}/, " "); } From 53dda834f9c04aa19b2f3e2bf316ddf428793d69 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 23 Apr 2020 20:33:48 +0530 Subject: [PATCH 6/7] Add /g to replace multiple spaces. --- commands/expand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/expand.js b/commands/expand.js index 2165a38..74e307a 100644 --- a/commands/expand.js +++ b/commands/expand.js @@ -22,7 +22,7 @@ function expand(words, text) { const word = wordsWithLetter[Math.floor(Math.random() * wordsWithLetter.length)]; return word; - }).reduce((acc, cur) => acc + " " + cur).replace(/\s{2,}/, " "); + }).reduce((acc, cur) => acc + " " + cur).replace(/\s{2,}/g, " "); } From 9024ec65f97e8b9cb98adf6aa82505cc39447ca5 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Fri, 20 Nov 2020 21:44:14 +0530 Subject: [PATCH 7/7] Use all args of /insult and /kys as name --- commands/insult.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/insult.js b/commands/insult.js index 6111643..45feaf2 100644 --- a/commands/insult.js +++ b/commands/insult.js @@ -10,10 +10,10 @@ module.exports = (random, kys, default_text, bot_text, excluded_names) => (ctx) } else { - const text_array = ctx.message.text.split(" "); + const text_array = ctx.message.text.split(" ").trim(); if (text_array.length > 1) { - const name = text_array[1]; + const name = text_array.slice(1).reduce((i, j) => i + " " + j); if (excluded_names.includes(name) || excluded_names.includes("@" + name)) return bot_text;