Nimbi

reactions.ts

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