diff --git a/commands/expand.js b/commands/expand.js new file mode 100644 index 0000000..74e307a --- /dev/null +++ b/commands/expand.js @@ -0,0 +1,48 @@ +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) => { + + 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).replace(/\s{2,}/g, " "); + +} + + +module.exports = (words = []) => (ctx) => { + + words = words.filter(i => ! i.match(/'s$/)); + 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 0921d55..5bfcc23 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'm Octanite. Sibling of @quadnite_bot. My creator @ceda_ei created me as 'another option' to users who want the bot in their groups to have privacy mode enabled. \n\nPrivacy mode? Wut is that?\n- Well basically disabling privacy mode enables a bot to read all the messages. @quadnite_bot has that disabled. Enabling privacy mode causes the bot to not recieve messages at some times. To circumvet that, you need to append @octanite_bot to your commands or simply use @quadnite_bot. \n\n[P.S. - My creator doesn't store any messages or personal data. It's safe to use any of the two bots.]\nTo give feedback, use /feedback")); }; diff --git a/commands/insult.js b/commands/insult.js index 707efe7..45feaf2 100644 --- a/commands/insult.js +++ b/commands/insult.js @@ -3,17 +3,17 @@ 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); } 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; 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