Advertisement
Nimbi

modules/reaction-roles.ts

May 20th, 2021 (edited)
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { config } from './config';
  2. import { permissions } from './permissions';
  3.  
  4. const definitions = [
  5.   {
  6.     role: config.ROLE.REACTION.ONE,
  7.     message: 'xxxxxxxxxx', // DISCORD MESSAGE ID
  8.     emoji: config.EMOJI.REACTION.ONE,
  9.     type: 'toggle'
  10.   },
  11.   {
  12.     role: config.ROLE.REACTION.TWO,
  13.     message: 'xxxxxxxxxx', // DISCORD MESSAGE ID
  14.     emoji: config.EMOJI.REACTION.TWO,
  15.     type: 'toggle'
  16.   },
  17.   {
  18.     role: config.ROLE.REACTION.THREE,
  19.     message: 'xxxxxxxxxx', // DISCORD MESSAGE ID
  20.     emoji: config.EMOJI.REACTION.THREE,
  21.     type: 'toggle'
  22.   },
  23.   {
  24.     role: config.ROLE.REACTION.FOUR,
  25.     message: 'xxxxxxxxxx', // DISCORD MESSAGE ID
  26.     emoji: config.EMOJI.REACTION.FOUR,
  27.     type: 'toggle'
  28.   },
  29.   {
  30.     role: config.ROLE.REACTION.FIVE,
  31.     message: 'xxxxxxxxxx', // DISCORD MESSAGE ID
  32.     emoji: config.EMOJI.REACTION.FIVE,
  33.     type: 'toggle'
  34.   },
  35.   {
  36.     role: config.ROLE.REACTION.SIX,
  37.     message: 'xxxxxxxxxx', // DISCORD MESSAGE ID
  38.     emoji: config.EMOJI.REACTION.SIX
  39.     type: 'toggle'
  40.   }
  41. ];
  42. function isNumber(n: string) {
  43.   return /^-?[\d.]+(?:e-?\d+)?$/.test(n);
  44. }
  45.  
  46. const cooldowns: { [key: string]: number } = {};
  47. export async function handleReactRoles(
  48.   reaction:
  49.     | discord.Event.IMessageReactionAdd
  50.     | discord.Event.IMessageReactionRemove,
  51.   add: boolean
  52. ) {
  53.   if (!reaction.member) return;
  54.   const { member } = reaction;
  55.   if (member.user.bot === true) {
  56.     return;
  57.   }
  58.   const message = reaction.messageId;
  59.   const { emoji } = reaction;
  60.   const found = definitions.find((definitions) => {
  61.     if (
  62.       typeof definitions.message !== 'string' ||
  63.       typeof definitions.role !== 'string' ||
  64.       typeof definitions.emoji !== 'string' ||
  65.       typeof definitions.type !== 'string'
  66.     ) {
  67.       return false;
  68.     }
  69.     const type = definitions.type.toLowerCase();
  70.     if (type !== 'once' && type !== 'toggle' && type !== 'remove') {
  71.       return false;
  72.     }
  73.     if (definitions.message !== message) {
  74.       return false;
  75.     }
  76.     if (isNumber(definitions.emoji)) {
  77.       return typeof emoji.id === 'string' && definitions.emoji === emoji.id;
  78.     }
  79.     return typeof emoji.name === 'string' && emoji.name === definitions.emoji;
  80.   });
  81.   if (!found) {
  82.     return;
  83.   }
  84.  
  85.   const type = found.type.toLowerCase();
  86.   if (type === 'remove' && add === false) {
  87.     return;
  88.   }
  89.   if (type === 'once' && add === false) {
  90.     return;
  91.   }
  92.  
  93.   const channel = await discord.getChannel(reaction.channelId);
  94.   if (
  95.     !(channel instanceof discord.GuildTextChannel) &&
  96.     !(channel instanceof discord.GuildNewsChannel)
  97.   ) {
  98.     return;
  99.   }
  100.  
  101.   let msg: discord.Message | null;
  102.   try {
  103.     msg = await channel.getMessage(reaction.messageId);
  104.   } catch (e) {
  105.     return;
  106.   }
  107.   if (msg === null) {
  108.     return;
  109.   }
  110.  
  111.   const hasMyEmoji = msg.reactions.find((react) => {
  112.     if (react.me === false) {
  113.       return false;
  114.     }
  115.     if (emoji.type === discord.Emoji.Type.GUILD) {
  116.       return emoji.id === react.emoji.id;
  117.     }
  118.     return emoji.name === react.emoji.name;
  119.   });
  120.   if (
  121.     typeof hasMyEmoji !== 'undefined' &&
  122.     add === true &&
  123.     (type === 'once' || type === 'remove')
  124.   ) {
  125.     try {
  126.       msg.deleteReaction(
  127.         emoji.type === discord.Emoji.Type.GUILD
  128.           ? `${emoji.name}:${emoji.id}`
  129.           : `${emoji.name}`,
  130.         reaction.userId
  131.       );
  132.     } catch (e) {}
  133.   }
  134.   if (typeof cooldowns[reaction.userId] === 'number') {
  135.     const diff = Date.now() - cooldowns[reaction.userId];
  136.     if (diff < 500) {
  137.       return;
  138.     }
  139.   }
  140.   cooldowns[reaction.userId] = Date.now();
  141.  
  142.   if (!hasMyEmoji) {
  143.     const emjMention = found.emoji;
  144.     await msg.deleteAllReactionsForEmoji(
  145.       emoji.type === discord.Emoji.Type.GUILD
  146.         ? `${emoji.name}:${emoji.id}`
  147.         : `${emoji.name}`
  148.     );
  149.     await msg.addReaction(
  150.       emoji.type === discord.Emoji.Type.GUILD
  151.         ? `${emoji.name}:${emoji.id}`
  152.         : `${emoji.name}`
  153.     );
  154.     return;
  155.   }
  156.   const guild = await discord.getGuild();
  157.   const memNew = await guild.getMember(reaction.userId);
  158.   if (memNew === null) {
  159.     return;
  160.   }
  161.   let typeRole: undefined | boolean;
  162.   if (type === 'once' && !memNew.roles.includes(found.role)) {
  163.     await memNew.addRole(found.role);
  164.     typeRole = true;
  165.   } else if (type === 'remove' && memNew.roles.includes(found.role)) {
  166.     await memNew.removeRole(found.role);
  167.     typeRole = false;
  168.   } else if (type === 'toggle') {
  169.     if (memNew.roles.includes(found.role) && add === false) {
  170.       await memNew.removeRole(found.role);
  171.       typeRole = false;
  172.     } else if (!memNew.roles.includes(found.role) && add === true) {
  173.       await memNew.addRole(found.role);
  174.       typeRole = true;
  175.     }
  176.   }
  177. }
  178. discord.on(
  179.   discord.Event.MESSAGE_REACTION_ADD,
  180.   async (reaction: discord.Event.IMessageReactionAdd) => {
  181.     if (!config.modules.admin.reactRoles.enabled) return;
  182.     await handleReactRoles(reaction, true);
  183.   }
  184. );
  185.  
  186. discord.on(
  187.   discord.Event.MESSAGE_REACTION_REMOVE,
  188.   async (reaction: discord.Event.IMessageReactionRemove) => {
  189.     if (!config.modules.admin.reactRoles.enabled) return;
  190.     await handleReactRoles(reaction, false);
  191.   }
  192. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement