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"));