|
- /* eslint-disable import/prefer-default-export */
-
- import { GuildEmoji, Message } from 'discord.js';
- import { readFile } from 'fs/promises';
-
- type TextReply = {
- keywords: (string | string[])[],
- emojiName?: string[],
- content?: string,
- }
-
- export const listenForKeywordsAndReply = async (message: Message): Promise<void> => {
- const repliesBuffer = await readFile('.text-replies.json');
- const repliesString = repliesBuffer.toString('utf-8');
- const replies = JSON.parse(repliesString) as TextReply[];
- const theReply = replies.reduce<TextReply | null>(
- (chosenReply, reply) => {
- if (chosenReply === null && reply.keywords.some((t) => {
- if (Array.isArray(t)) {
- return t.every((tt) => message.content === tt);
- }
- return message.content === t;
- })) {
- return reply;
- }
- return chosenReply;
- },
- null,
- );
-
- if (theReply) {
- const emojiNames = theReply.emojiName;
- if (emojiNames) {
- const emojis = emojiNames
- .map((emName) => (
- message.guild?.emojis.cache.find((em) => em.name === emName)
- ))
- .filter((em) => Boolean(em)) as GuildEmoji[];
-
- await Promise.all(emojis.map((em) => message.react(em)));
- }
- if (theReply.content) {
- await message.channel.send(theReply.content);
- }
- }
- };
|