0
votes

So in my discord bot, I want to use a command to display all available functions in the game with an embed. And then when the player reacts to the message with arrow reactions, I want the bot to edit the embed and send a next/previous page and vice versa.

My code (the bot work fines but only with this):

const list = [first,second];


        const emojiNext = '➡'; 
        const emojiPrevious = '⬅';
        const reactionArrow = [emojiPrevious, emojiNext];

        const time = 60000; // time limit: 1 min

        function getList(i) {
          return list[i]() 
          }

          function filter(reaction, user){
            return (!user.bot) && (reactionArrow.includes(reaction.emoji.name)); 
          }
          
           function onCollect(emoji, message, i, getList,reaction) { 
  if (reaction.emoji.name === emojiPrevious) {
             const embed = getList(i-1);
            if (embed !== undefined) {
              message.edit(embed);
              i--;
            }
           } else if (reaction.emoji.name === emojiNext) {
             const embed = getList(i+1);
             if (embed !== undefined) {
               message.edit(embed);
               i++;
             }
           }
           return i;

          function createCollectorMessage(message, getList) {
            let i = 0;
            const collector = message.createReactionCollector(filter, { time });
            collector.on('collect', reaction => {
              i = onCollect(reaction.emoji, message, i, getList);
            });
            collector.on('end', collected => message.clearReactions());
          }

          function sendList(channel, getList){
            message.channel.send(getList(0))
              .then(msg => msg.react(emojiPrevious))
              .then(msgReaction => msgReaction.message.react(emojiNext))
              .then(msgReaction => createCollectorMessage(msgReaction.message, getList));
          }

The first and second are the embeds created using const = new Discord.MessageEmbed method. The problem is whenever I run the command, the bot would send nothing - and yet the terminal doesn't show any error. I tried all other commands in my bot and all of them seem to work fine except for this. I am very basic in javascript or discord.js so help is very appreciated!

1

1 Answers

0
votes

here is how you would use the awaitMessage listener:

message.channel.send(embed).then((m) => {
     m.react('⬅️'); //reacts with ⬅️
     m.react('➡️');

     const filter = (reaction, user) => {
         return user.id != 'put bot id here' || user.id === message.author.id && reaction.emoji.name === '⬅️' && reaction.emoji.name === '➡️';
     };
     //this filter is to make sure only the user that called the command can react, and the only emojis collected are ⬅️ and ➡️

     m.awaitReactions(filter, { max: 1, time: 15000, errors: ['time'] }) //you can change maximum messages collected in the time, and the amount of time in ms you allow
     .then(collected => {
         if (collected.first().emoji.name === '⬅️') {
             //when the user reacts with ⬅️ - this code is executed    
             m.edit(previousEmbed); //replace with declared embed etc
         } elseif (collected.first().emoji.name === '➡️') {
             m.edit(nextEmbed); //replace with declared embed etc
         } else {
             //if the user reacts with any other emoji - remove if you dont want this
             m.channel.send(":x: Command cancelled").then(m => m.delete({ timeout: 3000 }));
         };
     })
        .catch(collected => {
            //if the user does not react in time
            m.channel.send(":x: Command cancelled").then(m => m.delete({ timeout: 3000 }));
     });
});

where I have used previousEmbed and nextEmbed - replace with the names of your next and previous embeds.

Let me know if you have any problems.