Why is message.awaitReactions always rejecting? - javascript

I am trying to create a command where if a user reacts to a message then the bot would send a message after a certain amount of time.
This is my code. it is supposed to send the 'you have reacted to the command' when I react with the emoji, but for some reason it always sends 'its been 10 seconds and no one has reacted'. Does anyone know what I did wrong in the code?
const filter = (reaction, user) => {
return ['💌'].includes(reaction.emoji.name)
&& user.id === message.author.id;
}
message.awaitReactions(filter, { max: 1, time: 10000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '💌') {
message.reply('you have reacted to this command');
}
})
.catch(collected => {
message.channel.send('its been 10 seconds and no one has reacted');
})

Related

How to make a discord bot do something after I react to an emoji in an image sent by itself

I'm making my first discord bot and I'm trying to make it send a message after I react to one of its emojis, problem is, when I click the thumbs up emoji, the bot simply does not send the message, it's been a few hours now and I can't find the problem, I'm sorry if this has been solved somewhere else, I couldn't find anything that works.
if (command === "ping") {
const attachment = new MessageAttachment('https://cdn.mcr.ea.com/3/images/ac394369-2801-4e09-87eb-82ca54e26254/1588018258-0x0-0-0.jpg');
const sentMessage = await message.channel.send({files: [attachment] })
sentMessage.react('👍');
//sentMessage.react('👎');
const filter = (reaction, user) => {
return reaction.emoji.name === '👍' && user.id === message.author.id;
};
const collector = sentMessage.createReactionCollector(filter, { time: 15000 });
collector.on('collect', (reaction, user) => {
message.channel.send(`Collected ${reaction.emoji.name} from ${user.tag}`);
});
collector.on('end', collected => {
message.channel.send(`Collected ${collected.size} items`);
console.log(`Collected ${collected.size} items`);
});
EDIT
I found that I was missing the "GUILD_MESSAGE_REACTIONS" intent, now i have these 3 "GUILDS", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS", still not working sadly!
Here's the full code:
https://pastebin.com/Q0eZ6VSz
Hi, this code is working except a small thing...
sentMessage.createReactionCollector(filter, { time: 15000 });
change this to:
sentMessage.createReactionCollector({filter, time: 15000 });
also I suggest you to add max: 1 option too.
so final code:
...
sentMessage.createReactionCollector({filter, max: 1, time: 15000 });
in this situation only gets first react by you.

How do I count the amount of reactions made on a specific emoji from a message that was sent? | discord.js v13

I'm trying to make a bot where a user makes a poll and if a certain emoji that the bot has reacted with reaches a number, say 10, then it does other actions. However, that is not my issue. My issue is collecting the amount of reactions itself from that emoji.
My Code:
const embedSend = await message.channel.send({ embeds: [suggestionEmbed] }).then(async sEmbed => {
await sEmbed.react('⬆️');
await sEmbed.react('⬇️');
const filter = (reaction, user) => {
return ['⬆️', '⬇️'].includes(reaction.emoji.name) && user.id === message.author.id;
};
sEmbed.awaitReactions({ filter, max: 20, time: 50000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '⬆️') {
console.log('up');
} else if (reaction.emoji.name === '⬇️') {
console.log('down');
}
})
.catch(collected => {
console.log("didnt reach 20 reacts");
});
});
I tried using the previous method above. However, it doesn't seem to actually print up or down for every reaction after reaching 20 reacts but instead it prints nothing or just up or just down. I've tried using the line of code below to count the emojis but no luck either:
console.log(sEmbed.reactions.find(reaction => reaction.emoji.name === '⬆️').count)
Placed this inside of the .then statement with the awaitReactions right under the If-else block of code and got no output.
embedSend.reactions.cache.get('⬆️').count;
This I've placed after the embedSend and it returns me this error:
TypeError: Cannot read property 'reactions' of undefined
and if i place it within the block of code tiehr the embedSend or awaitReactions and change it to sEmbed it returns nothing.

I need get how much reactions has a message in discord.js

I was thinking of code like this to get the number of reactions a message has received after a set time:
if (message.content == "test") {
message.channel.send("Hi").then(msg => {
msg.react('🏠').then(r => {
const react = (reaction, user) => reaction.emoji.name === '🏠'
const collector = msg.createReactionCollector(react)
collector.on('collect', (r, u) => {
setTimeout(() => u.send(r.length), 60000 * 5);
})
})
})
}
});
But rightly .lenght is not the correct method to obtain the number of reactions, consequently the error is that the "r.length" message is empty and the bot cannot send it.
The goal is to send a message, as soon as you react to that message, a setTimeOut starts and at the end of the time it returns (in this case in private) the number of reactions that that message has received.
collected.size is the right way of getting the number of collected reactions a message has.
You can read about reaction collectors here
Here is a basic reaction collector that uses collected.size:
const filter = (reaction, user) => {
return reaction.emoji.name === '👍' && user.id === message.author.id;
};
const collector = message.createReactionCollector(filter, { time: 15000 });
collector.on('collect', (reaction, user) => {
console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
});
collector.on('end', collected => {
console.log(`Collected ${collected.size} items`);
});

How do I use callbacks to order the chain of events in a command?

I am making a setup command, using the #awaitMessages listener 2 times in a row to set up a server correctly from a user input, but try as I might, I cannot achieve the effect of each message being sent, then collecting data, then sending the next message etc. Here is my code (I have removed lots of clutter you dont need)
message.channel.send("Please enter the role ID of admin:").then(() => {
const filter = m => m.content
message.channel.awaitMessages(filter, { max: 1, time: 10000, errors: ['time'] })
.then(collected => {
message.channel.send(':white_check_mark: Admin role set up correctly')
}).catch(collected => {
message.channel.send(`:x: Setup cancelled - 0 messages were collected in the time limit, please try again`).then(m => m.delete({ timeout: 4000 }));
})
});
message.delete().then(async () => {
await message.channel.send("Please enter the role ID of moderator:").then(() => {
const filter = m => m.content
message.channel.awaitMessages(filter, { max: 1, time: 10000, errors: ['time'] })
.then(collected => {
message.channel.send(':white_check_mark: Mod role set up correctly')
}).catch(collected => {
message.channel.send(`:x: Setup cancelled - 0 messages were collected in the time limit, please try again`).then(m => m.delete({ timeout: 4000 }));
})
});
})
What happens is the bot does not wait for my collect event, and just moves on to sending the next message e.g.
Please enter the role ID of administrator
Please enter the role ID of moderator
What am I doing wrong? - there is no error thrown (since I have not made a mistake in my code - it just does not do what I need)
Edit:
message.channel.awaitMessages(filter, { max: 1, time: 10000, errors: ['time'] })
.then(collected => {
if (!collected.content === "test") return;
message.channel.send(':white_check_mark: Admin role set up correctly')
}).catch(collected => {
message.channel.send(`:x: Setup cancelled - 0 messages were collected in the time limit, please try again`).then(m => m.delete({ timeout: 4000 }));
})
message.channel.send("Please enter the role ID of moderator:").then(() => {
const filter = m => m.content
message.channel.awaitMessages(filter, { max: 1, time: 10000, errors: ['time'] })
.then(collected => {
if (!collected.content === "test") return;
message.channel.send(':white_check_mark: Mod role set up correctly')
}).catch(collected => {
message.channel.send(`:x: Setup cancelled - 0 messages were collected in the time limit, please try again`).then(m => m.delete({ timeout: 4000 }));
})
});
});
message.delete()
First off, I would avoid mixing await and using a promise (.then()) on the same thing.
Also your filters aren't really serving much purpose as you just use an arrow function without doing anything with the result. You could fix this by limiting the filter so that only a specific user can trigger the #awaitMessages events by using the follow filter instead:
const filter = (reaction, user) => {
return user.id === message.author.id;
};
You also now only need to define this once, as it will be in the scope for the rest of the code now too
To fix the problem you're having, you can simply just chain everything together using .then(), whilst it might not be pretty, it works.
message.channel.send("Please enter the role ID of admin:").then(() => {
const filter = (reaction, user) => {
return user.id === message.author.id;
};
message.channel.awaitMessages(filter, { max: 1, time: 10000, errors: ['time'] }).then(collected => {
if (!collected.content === "test") return;
message.channel.send(':white_check_mark: Admin role set up correctly').then(() => {
message.channel.send("Please enter the role ID of moderator:").then(() => {
message.channel.awaitMessages(filter, { max: 1, time: 10000, errors: ['time'] }).then(collected => {
if (!collected.content === "test") return;
message.channel.send(':white_check_mark: Mod role set up correctly');
}).catch(collected => {
message.channel.send(`:x: Setup cancelled - 0 messages were collected in the time limit, please try again`).then(m => m.delete({ timeout: 4000 }));
});
});
});
}).catch(collected => {
message.channel.send(`:x: Setup cancelled - 0 messages were collected in the time limit, please try again`).then(m => m.delete({ timeout: 4000 }));
});
});
Note: I changed your filter to make sure the same user is always entering the commands.

Await reaction by a different user

I'm making a verification system that if you send a message. In a different channel, an embed shows up with 2 emoji's: 1 to accept, and 1 to deny. The .awaitReaction has to be triggered by a different user then the author. But when I change the filter. It triggers the message if the bot reacts to it. How can I fix this?
Here is my code:
let register = args.join(" ").slice(7)
const filter = (reaction, user) => ["✅", "❌"].includes(reaction.emoji.name) && !bot.user;
let test = new Discord.RichEmbed()
.addField("User:", message.author.username && message.author.tag, true)
.addField("Requested nickname:", register)
.setColor("#ed0c75")
.setImage(message.author.displayAvatarURL)
let acceptordeny = message.guild.channels.find(`name`, "accept-or-deny");
if(!acceptordeny) return message.channel.send("Can't find accept or deny channel.");
acceptordeny.send(test).then(async message => {
await message.react("✅")
await message.react("❌")
message.awaitReactions(filter, {
max: 1,
time: 60000,
errors: ["time"]
}).then(collected => {
const reaction = collected.first();
switch (reaction.emoji.name) {
case "✅":
console.log("Accepted")
break;
case '❌':
console.log("Denied")
break;
}
}).catch(collected => {
return acceptordeny.send("Failed")
})
})
Hope someone can help me with this.
Update the filter with this one:
const filter = (reaction, user) => ["✅", "❌"].includes(reaction.emoji.name) && user.id !== message.client.user.id;
It will check if the id of the user who reacted to the message is the same as the id and if, cancel.

Categories

Resources