random_react.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import { Message } from "discord.js";
  2. import { getRepository } from "typeorm";
  3. import { RandomMessageReaction } from "@shared/db/entity/RandomMesssageReaction";
  4. import { client } from "src/client";
  5. import { Event, BotEventData, Plugin } from "src/model/plugin";
  6. const timeout = (ms: number) => new Promise(r => setTimeout(r, ms));
  7. @Plugin
  8. export class RandomReact {
  9. @Event("message")
  10. async showHelp({ actionsDone }: BotEventData, msg: Message): Promise<void> {
  11. if(actionsDone)
  12. return;
  13. const repo = getRepository(RandomMessageReaction);
  14. const reactInfo = await repo.findOne({ where: { userId: msg.author.id } });
  15. if(!reactInfo)
  16. return;
  17. const emote = client.bot.emojis.resolve(reactInfo.reactionEmoteId);
  18. if(!emote)
  19. return;
  20. if(Math.random() < reactInfo.reactProbability) {
  21. await timeout(Math.random() * reactInfo.maxWaitMs);
  22. await msg.react(emote);
  23. }
  24. }
  25. }