Reputation: 25
I'm currently looking at getting a soundboard in Discord.js by using emojis to act as the controls to the soundboard, it should then respond with an appropriate sound based on the emoji sent. However, I'm having issues with at least testing if I can receive different types of emojis based on the emojis
array.
main.js
var soundBoard;
client.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
switch (command) {
...
case 'soundboard':
client.commands.get('soundboard').execute(message, args, soundBoard);
break;
...
}
}
soundboard.js
module.exports = {
name: 'soundboard',
description: "Soundboard",
async execute(message, args, sb) {
const emojis = [
'😄',
'✋'
];
if (!sb) {
sb = await message.channel.send('Soundboard Active');
const filter = (reaction, user) => {
return emojis.includes(reaction.emoji.name);
};
sb.awaitReactions(filter)
.then(collected => {
console.log("Collected: " + collected);
sb.react('✅');
})
.catch(console.error);
for (let e of emojis) {
sb.react(e);
}
}
else {
console.log(sb);
}
}
}
sb
is passed from a global variable declared in my main.js
. Unfortunately at the moment all that seems to happen is the message appears but whenever I click the emojis to respond the expected result of adding a ✅ to the original message fails.
Upvotes: 0
Views: 118
Reputation: 4520
First of all, I would recommend limiting your reaction collector by a specific amount of time and by only allowing the user who sent the message to interact with it. I would also recommend using a reactionCollector
instead of awaitReactions
here.
I believe the problem you are having is that .then()
is only called on awaitReactions()
after the time limit of awaitReactions()
has ended. In other words, your current reaction collector is attempting to react with a check emoji after a certain period of time has passed, instead of attempting to do so after one reaction has been collected. You are essentially attempting to listen to the equivalent of a reactionCollector
's "end" event instead of its "collect" event.
By using a reactionCollector
, you can specifically run code every time an individual emoji is clicked on by listening to the "collect" event. Here's an example:
let collector = sb.createReactionCollector((r, user) => emojis.includes(r.emoji.name) && user.id === message.author.id, { time: 120000 });
//This is the event you need to use
collector.on("collect", r => {
console.log("This message is logged when we receive a reaction.");
sb.react('✅');
});
//This is the equivalent of the event you are currently using
collector.on("end", collected => {
console.log("This is where your current code is trying to add ✅");
console.log("This would only be logged after 120 secs in this example.")
})
Relevant resources:
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=createReactionCollector
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=awaitReactions
Upvotes: 1