Creating a Discord Bot to Add Emoji Reactions to Specific Messages

I’m trying to develop a Discord bot that can add emoji reactions to particular messages using their message IDs. Here’s what I want to achieve:

When a user types a command like !react 😊 1234567890123456, the bot should find the message with the ID 1234567890123456 and add the :blush: emoji as a reaction to it.

I’ve been struggling to figure out how to implement this feature. Does anyone have experience with Discord bots and can point me in the right direction? I’d really appreciate some guidance on how to structure the code for this functionality.

Some specific questions I have:

  • How do I retrieve a message using its ID?
  • What’s the best way to parse the command to extract the emoji and message ID?
  • Are there any Discord.js methods I should be aware of for adding reactions?

Thanks in advance for any help or advice!

hey, i’ve done this before! u can use message.channel.messages.fetch(messageId) to get the message. then just do targetMessage.react(emoji). make sure to use try/catch cuz it might fail. also, u might wanna check if the user has permission to use the command. good luck with ur bot!

As someone who’s developed a few Discord bots, I can share some insights on implementing this feature.

First, to retrieve a message by ID, you’ll want to use the channel.messages.fetch() method. It’s asynchronous, so you’ll need to use async/await or promises.

For parsing the command, a simple regex or string split should do the trick. Something like const [command, emoji, messageId] = message.content.split(' ') would work for your example.

To add the reaction, you can use the message.react() method once you’ve fetched the target message. Here’s a rough outline of how the code might look:

client.on('messageCreate', async (message) => {
  if (message.content.startsWith('!react')) {
    const [, emoji, messageId] = message.content.split(' ');
    try {
      const targetMessage = await message.channel.messages.fetch(messageId);
      await targetMessage.react(emoji);
    } catch (error) {
      console.error('Error adding reaction:', error);
      message.reply('Failed to add reaction. Make sure the message ID is valid.');
    }
  }
});

Remember to handle errors, as the message might not exist or the bot might not have permissions to react. Hope this helps you get started!

I’ve implemented something similar in one of my Discord bots. Here’s a tip: use the getMessage() method from the channel object to fetch the message by ID; it’s more efficient than retrieving the entire message history.

For parsing, I’d suggest using a regular expression. This approach is flexible and accommodates a variety of input formats. Something like /!react\s+(\S+)\s+(\d+)/ should work well.

When adding the reaction, ensure you catch any potential errors. The target message might be missing or the bot might lack permissions. Always provide appropriate feedback to the user.

Additionally, consider implementing a cooldown system using a Map to track user command usage. This can help prevent spam and reduce hitting rate limits.