Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // React with :star: to any message and it will send this message in a special channel.
- // When a reaction is removed or added, it will update the message (or delete it if the last :star: was removed)
- const starboardChannelID = '696276355893035008';
- const star = '⭐';
- async function handleReaction(
- data: discord.Event.IMessageReactionAdd | discord.Event.IMessageReactionRemove
- ) {
- // Ignore event if reaction emoji is not a star
- if (data.emoji.name !== star) return;
- // Get the message ID of starboard message
- const starboardMessageID = <string | undefined>(
- await pylon.kv.get(data.messageId)
- );
- // Get the original channel and check if it is a text channel, so we can get the originalMessage
- const originalChannel = await discord.getChannel(data.channelId);
- if (!(originalChannel instanceof discord.GuildTextChannel)) {
- return;
- }
- // Get the original message and check if it exists
- const originalMessage = await originalChannel.getMessage(data.messageId);
- if (!originalMessage) {
- return;
- }
- // Get the starboard channel and check if it is a text channel, so we can get or send the starboard message
- const starboardChannel = await discord.getChannel(starboardChannelID);
- if (!(starboardChannel instanceof discord.GuildTextChannel)) {
- return;
- }
- const stars = originalMessage.reactions.filter((v) => v.emoji.name === star)
- .length;
- // `messageData` will be sent in the starboard channel
- const messageData = {
- content: `${stars + star} (<#${data.channelId}>)`,
- embed: {
- color: 0xffbe76,
- description: originalMessage.content,
- fields: [
- {
- name: 'Original message',
- value: `[Jump](https://discordapp.com/channels/${data.guildId}/${data.channelId}/${data.messageId})`,
- inline: false
- }
- ]
- }
- };
- if (!starboardMessageID) {
- // First star
- // Send starboard message and store the message ID in KV
- const msg = await starboardChannel.sendMessage(messageData);
- await pylon.kv.put(data.messageId, msg.id);
- } else {
- // Not the first star
- // Just get and edit the starboard message
- const starboardMessage = await starboardChannel.getMessage(
- starboardMessageID
- );
- // Check if the message still exists...
- if (!starboardMessage) {
- return;
- }
- // Delete the starboard message if there are no stars
- if (stars === 0) {
- await starboardMessage.delete();
- await pylon.kv.delete(data.messageId);
- } else {
- // Update the starboard message
- await starboardMessage.edit(messageData);
- }
- }
- }
- // Register events
- discord.registerEventHandler('MESSAGE_REACTION_ADD', handleReaction);
- discord.registerEventHandler('MESSAGE_REACTION_REMOVE', handleReaction);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement