Joaquin Parada
Joaquin Parada

Reputation: 47

Change message with reactions Discord.js

Im a bit new creating Discord bots, my plan with this is to have like a book with the help command that if you press the arrow it changes to the next page, and if you press another arrow it changes to the previus page, I'll simpifly my code to make it more easier to understand.

async execute(message, client){
    var msg1 = await message.channel.send('how are')
    msg1.react('▶') //send the message

    client.on('messageReactionAdd', async (reaction, user) =>{ //wait for the reaction
        if(user.bot) return
        if (message.reaction.id === msg1.id){
            message.channel.messages.fetch(msg1.id).then(msg => msg.delete())
            var msg2 = await message.channel.send('you?')
            msg2.react('◀') //delete the previus message, send the second and react to it
        }
        else if (message.reaction.id === msg2.id){
            message.channel.messages.fetch(msg2.id).then(msg => msg.delete())
            var msg1 = await message.channel.send('how are')
            msg1.react('▶') //delete the second message, send the first and react
        }
    })
}

I know this isnt going to work but i hope you understand the major idea, how can i do this so it actually works?

Upvotes: 1

Views: 372

Answers (1)

JaeDeloper
JaeDeloper

Reputation: 46

If you want to listen for reactions to old messages you need to fetch the messageId, etc. This is simple to understand. Here is a Guide.

If you only want it to send a message, react to it, and listen to the next reaction it's easier. There is also a Guide for it.

In your case with the book, I would use the second method and then loop it. So if someone presses the button it starts the function from the beginning.

This is only an example I never tested it and can't say if it works, It's only made to show you what I meant:

const reactionName1 = '▶';
const reactionName2 = '◀';
const text1= 'How are';
const text2 = 'you?';

module.exports = {
    async execute(message, client) {
        if(message.author.user.bot) return;
        message.channel.send('Hello!').then(msg => {
            nextMessage(msg, message.author, reactionName1, text1)
        });
    },
}

function nextMessage(message, author, reactionName, text) {
    message.edit(text).then(() => {
        message.react(reactionName);

        const filter = (reaction, user) => {
            return [reactionName].includes(reaction.emoji.name) && user.id === author.id;
        };

        message.awaitReactions({ filter, max: 1, time: 60000, errors: ['time'] })
            .then(collected => {
                const reaction = collected.first();
                message.reactions.removeAll();

                if(reaction.emoji.name === reactionName1) return nextMessage(message, author, reactionName2, text2);
                if(reaction.emoji.name === reactionName2) return nextMessage(message, author, reactionName1, text1);
            })
            .catch(collected => {
                message.reactions.removeAll();
                return message.edit('You had to press the reaction').then(() => message.delete({ timeout: 4000 })).catch(o_O=>{});
            });
    });
};

I hope that it was a bit helpful.

Upvotes: 1

Related Questions