Add /expand

This commit is contained in:
Ceda EI 2020-04-22 21:56:31 +05:30
parent 3ed015b584
commit 72e69b7ac1
2 changed files with 35 additions and 2 deletions

32
commands/expand.js Normal file
View File

@ -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";
}
};

View File

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