Advertisement
y21

Pylon Starboard

y21
Apr 6th, 2020
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // React with :star: to any message and it will send this message in a special channel.
  2. // When a reaction is removed or added, it will update the message (or delete it if the last :star: was removed)
  3.  
  4. const starboardChannelID = '696276355893035008';
  5. const star = '⭐';
  6.  
  7. async function handleReaction(
  8.   data: discord.Event.IMessageReactionAdd | discord.Event.IMessageReactionRemove
  9. ) {
  10.   // Ignore event if reaction emoji is not a star
  11.   if (data.emoji.name !== star) return;
  12.  
  13.   // Get the message ID of starboard message
  14.   const starboardMessageID = <string | undefined>(
  15.     await pylon.kv.get(data.messageId)
  16.   );
  17.  
  18.   // Get the original channel and check if it is a text channel, so we can get the originalMessage
  19.   const originalChannel = await discord.getChannel(data.channelId);
  20.   if (!(originalChannel instanceof discord.GuildTextChannel)) {
  21.     return;
  22.   }
  23.  
  24.   // Get the original message and check if it exists
  25.   const originalMessage = await originalChannel.getMessage(data.messageId);
  26.   if (!originalMessage) {
  27.     return;
  28.   }
  29.  
  30.   // Get the starboard channel and check if it is a text channel, so we can get or send the starboard message
  31.   const starboardChannel = await discord.getChannel(starboardChannelID);
  32.   if (!(starboardChannel instanceof discord.GuildTextChannel)) {
  33.     return;
  34.   }
  35.  
  36.   const stars = originalMessage.reactions.filter((v) => v.emoji.name === star)
  37.     .length;
  38.  
  39.   // `messageData` will be sent in the starboard channel
  40.   const messageData = {
  41.     content: `${stars + star} (<#${data.channelId}>)`,
  42.     embed: {
  43.       color: 0xffbe76,
  44.       description: originalMessage.content,
  45.       fields: [
  46.         {
  47.           name: 'Original message',
  48.           value: `[Jump](https://discordapp.com/channels/${data.guildId}/${data.channelId}/${data.messageId})`,
  49.           inline: false
  50.         }
  51.       ]
  52.     }
  53.   };
  54.  
  55.   if (!starboardMessageID) {
  56.     // First star
  57.     // Send starboard message and store the message ID in KV
  58.  
  59.     const msg = await starboardChannel.sendMessage(messageData);
  60.     await pylon.kv.put(data.messageId, msg.id);
  61.   } else {
  62.     // Not the first star
  63.     // Just get and edit the starboard message
  64.  
  65.     const starboardMessage = await starboardChannel.getMessage(
  66.       starboardMessageID
  67.     );
  68.  
  69.     // Check if the message still exists...
  70.     if (!starboardMessage) {
  71.       return;
  72.     }
  73.  
  74.     // Delete the starboard message if there are no stars
  75.     if (stars === 0) {
  76.       await starboardMessage.delete();
  77.       await pylon.kv.delete(data.messageId);
  78.     } else {
  79.       // Update the starboard message
  80.       await starboardMessage.edit(messageData);
  81.     }
  82.   }
  83. }
  84.  
  85. // Register events
  86. discord.registerEventHandler('MESSAGE_REACTION_ADD', handleReaction);
  87.  
  88. discord.registerEventHandler('MESSAGE_REACTION_REMOVE', handleReaction);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement