MrRob
MrRob

Reputation: 11

How to transfer reaction to original message, discord.js

I am trying to make a suggestion feature for one of my bots. I have searched online but nothing really helps with it. The suggestion part is working but I want to add a feature where if i react to the message in the log channel it sends the reaction to the original message. Here is my code:

bot.on('message', message => {
  if (message.content.startsWith("/suggest")){
    message.reply(`Your response has been recorded.`)
   var yes = message.content
   const channel1 = bot.channels.cache.get('722590953017442335');
channel1.send(`${message.author} suggests: ${yes.slice(9)}`)
if (chanell1.reaction.emoji.name === '✅'){
  const channel2 = bot.channels.cache.get('722595812462297139');
  channell2.react.author.message('✅')
}
  }
}) 

I am using v12 of node.

Upvotes: 1

Views: 688

Answers (2)

PiggyPlex
PiggyPlex

Reputation: 703

Please read the official documentation at https://discord.js.org/#/docs/main/v12/general/welcome for v12 help. You ought to use the Client#messageReactionAdd event to track reactions - your code isn't too far off, however it is missing that key event. Please note that to track reactions you'll need persistent storage if you want the reactions to work after restart. Alternatively, you could try awaiting the reactions or using a reaction collector if only short term. Try this instead:

const { Client } = require('discord.js');
const bot = new Client({ partials: ['REACTION', 'USER'] });
const prefix = '/';
const suggestionsCache = {};

bot.on('message', async message => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;
  const args = message.content.split(' '),
        command = args.shift().slice(prefix.length);
  if (command == 'suggest') {
    const suggestion = args.join(' '),
          suggestionMessage = `${message.author} suggests: ${suggestion}`,
          suggestionChannel = bot.channels.cache.get('722590953017442335'),
          logChannel = bot.channels.cache.get('722595812462297139');
    if (!suggestionChannel || !logChannel) return message.reply('Oops! Something went wrong.');
    try {
      const suggestionMessage = await suggestionChannel.send(suggestionMessage);
      const logMessage = await logChannel.send(suggestionMessage);
      suggestionsCache[logMessage.id] = suggestionMessage.id; // Save the ID for later.
      message.reply('Your response has been recorded.');
    } catch {
      message.reply('Oops! Something went wrong.');
    };
  };
});

bot.on('messageReactionAdd', async (reaction, user) => {
  if (reaction.partial) {
    try {
      await reaction.fetch();
    } catch {}
  }
  const messageID = suggestionsCache[reaction.message.id];
  if (!messageID || reaction.emoji.name !== '✅') return; // If not found in cache, ignore and if the emoji isn't the check, ignore it.
  try {
    const channel = await client.channels.fetch('722590953017442335');
    if (!channel) return;
    const message = channel.messages.fetch(messageID);
    if (!message) return; // Message deleted.
    message.react('✅');
  } catch {
    return;
  };
});

Please note that I am new to v12 and normally use v11! The code above is not tested and may contain bugs as a result. Please feel free to edit/update the code.

Upvotes: 1

Lioness100
Lioness100

Reputation: 8402

You can use the awaitReactions() function:

bot.on("message", (message) => {
  if (message.content.startsWith("/suggest")) {
    message.reply(`Your response has been recorded.`);
    var yes = message.content;
    bot.channels.cache
      .get("722590953017442335")
      .send(`${message.author} suggests: ${yes.slice(9)}`)
      .then(async (msg) => {
        msg
          .awaitReactions((reaction) => reaction.emoji.name === "✅", {
            time: 15000,
          })
          .then((collected) => message.react("✅"))
          .catch(console.error);
      });
  }
});

Upvotes: 1

Related Questions