random_react.ts 1.0 KB

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