Octanite-Bot/commands/expand.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-04-22 18:26:31 +02:00
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];
});
2020-04-22 18:26:31 +02:00
return letters.map((letter) => {
if (!(letter.toLowerCase() in dict))
return letter;
const wordsWithLetter = dict[letter.toLowerCase()];
2020-04-22 18:26:31 +02:00
const word = wordsWithLetter[Math.floor(Math.random() * wordsWithLetter.length)];
return word;
2020-04-23 17:03:48 +02:00
}).reduce((acc, cur) => acc + " " + cur).replace(/\s{2,}/g, " ");
2020-04-22 18:26:31 +02:00
}
module.exports = (words = []) => (ctx) => {
words = words.filter(i => ! i.match(/'s$/));
2020-04-22 18:26:31 +02:00
const message = ctx.message.text.replace(/^[^ ]+/, "");
if (message) {
return expand(words, message);
} else {
2024-01-24 20:08:35 +01:00
if (ctx.message.reply_to_message && !ctx.message.reply_to_message.is_topic_message)
2020-04-22 18:26:31 +02:00
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";
}
};