Cuu.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

  1. /* eslint-disable import/prefer-default-export */
  2. import { GuildEmoji, Message } from 'discord.js';
  3. import { readFile } from 'fs/promises';
  4. type TextReply = {
  5. keywords: (string | string[])[],
  6. emojiName?: string[],
  7. content?: string,
  8. }
  9. export const listenForKeywordsAndReply = async (message: Message): Promise<void> => {
  10. const repliesBuffer = await readFile('.text-replies.json');
  11. const repliesString = repliesBuffer.toString('utf-8');
  12. const replies = JSON.parse(repliesString) as TextReply[];
  13. const theReply = replies.reduce<TextReply | null>(
  14. (chosenReply, reply) => {
  15. if (chosenReply === null && reply.keywords.some((t) => {
  16. if (Array.isArray(t)) {
  17. return t.every((tt) => message.content === tt);
  18. }
  19. return message.content === t;
  20. })) {
  21. return reply;
  22. }
  23. return chosenReply;
  24. },
  25. null,
  26. );
  27. if (theReply) {
  28. const emojiNames = theReply.emojiName;
  29. if (emojiNames) {
  30. const emojis = emojiNames
  31. .map((emName) => (
  32. message.guild?.emojis.cache.find((em) => em.name === emName)
  33. ))
  34. .filter((em) => Boolean(em)) as GuildEmoji[];
  35. await Promise.all(emojis.map((em) => message.react(em)));
  36. }
  37. if (theReply.content) {
  38. await message.channel.send(theReply.content);
  39. }
  40. }
  41. };