Skip to content
Advertisement

Discord.JS AwaitMessages

In the line of “message.channel.awaitmessages” i think the code is bad writted, the thing i want to do is to await for 1 message that is a number btw 0 and 23.

Here is the corresponding code :

client2.on("message", (message) => {
    if(message.bot){return;} //if its a bot - disregard
    if(message.content[0] !== prefix ){return;} //if its not a command by the user - disregard
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
if (command === 'collect') {
    const id = message.member.id;
    const filter = m => m.content.endsWith('h');
    message.channel.send(scheduler1).then(function(message) {
        message.react('1️⃣')
            .then(() => message.react('2️⃣'))
            .then(() => message.react('3️⃣'))
            .then(() => message.react('3️⃣'))
            .then(() =>message.awaitReactions((reaction, user) =>(reaction.emoji.name == '1️⃣' || reaction.emoji.name == '2️⃣' || reaction.emoji.name == '3️⃣' || reaction.emoji.name == '4️⃣'),
                { max: 1, time: 30000 }).then(collected => {
                if (collected.first().emoji.name == '1️⃣') {
                    message.edit(scheduler2);
                    message.clearReactions();
                    console.log("try");
                    message.channel.awaitMessages(filter, {max: 1, time: 6000, errors: ['time']})   
                        .then(message.channel.send(collected))
                        .catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));
                }
                if (collected.first().emoji.name == '2️⃣') {
                    message.edit(scheduler3);
                    message.clearReactions();
                }
                if (collected.first().emoji.name == '3️⃣') {
                    message.edit(scheduler4);
                    message.clearReactions();
                }
                }).catch(() => {
                    message.edit('No answer after 30 seconds, operation canceled.');
                }));
    })}

Advertisement

Answer

You need to update your filter for this to work:

const filter = m => Number(m.content) >= 1 && Number(m.content) <= 23;

Additionally the .then() needs a collected too.

 message.channel.awaitMessages(filter, {max: 1, time: 6000, errors: ['time']})   
                        .then(collected => message.channel.send(collected.first().content))
                        .catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));

It would also be worth trying out async / await. Wich would allow you to do:

client2.on('message', async (message) => {
    if(message.bot) return; // if its a bot - disregard
    if(message.content[0] !== prefix) return; // if its not a command by the user - disregard
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    if (command === 'collect') {
        const id = message.member.id;
        const msg = await message.channel.send(scheduler1);
        await msg.react('1️⃣');
        await msg.react('2️⃣');
        await msg.react('3️⃣');
        await msg.react('3️⃣');
        const filter = (reaction, user) => (reaction.emoji.name == '1️⃣' || reaction.emoji.name == '2️⃣' || reaction.emoji.name == '3️⃣' || reaction.emoji.name == '4️⃣');
        const collected = await msg.awaitReactions(filter, { max: 1, time: 30000 })
            .catch(() => {
                msg.edit('No answer after 30 seconds, operation canceled.');
                return;
            });
        if(!collected) return;
        if (collected.first().emoji.name == '1️⃣') {
            msg.edit(scheduler2);
            msg.clearReactions();
            console.log('try');
            const filter2 = m => Number(m.content) >= 1 && Number(m.content) <= 23;
            const collected2 = await msg.channel.awaitMessages(filter2, { max: 1, time: 6000, errors: ['time'] });
            msg.channel.send(collected2.first().content)
                .catch(collectedx => console.log(`After a minute, only ${collectedx.size} out of 4 voted.`));
        }
        if (collected.first().emoji.name == '2️⃣') {
            msg.edit(scheduler3);
            msg.clearReactions();
        }
        if (collected.first().emoji.name == '3️⃣') {
            msg.edit(scheduler4);
            msg.clearReactions();
        }
    }
});
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement