简体   繁体   中英

awaitReactions problems in Discord.js

Nothing happens when I react, can someone help me? After 1 minute, the message of the .catch is sent. (sorry for errors english is not my main language)

var start_embed = new Discord.MessageEmbed()
    .setAuthor('Yukyo', client.user.displayAvatarURL())
    .setColor("RANDOM")
    .setDescription("**Début du jeu MMO**")
    .setThumbnail("https://cdn.discordapp.com/attachments/794957118503124995/795677631277301790/Stand_Creation.gif")
    .setImage("https://cdn.discordapp.com/attachments/794957118503124995/795678149328502804/tumblr_n0dv0zKegy1ryfwiuo1_500.gif")
    .addField("Bienvenue **"+ message.author.tag +"**, tu commences l'aventure MMO", "Je voudrais te poser une question avant de commencer.")
    .addField("Quel pouvoir souhaiterais tu avoir au début de ton aventure ?", '<:gomuGomuNoMi:795660107090100234> **Un fruit du démon** (au hasard)\n<:standArrow:795658666681434112> **Un stand** (au hasard)\n<:dekuAllMight:795660876031459378> **Un alter** (au hasard, prochainement)\nTu as 60 secondes pour répondre.')

message.channel.send(start_embed)
    .then(function (message) {
        message.react("795660107090100234");
        message.react("795658666681434112");
        message.awaitReactions((reaction, users) => users.id == message.author.id && (reaction.emoji.name == '795660107090100234' || reaction.emoji.name == '795658666681434112'), { max: 1, time: 60000 })
            .then(collected => {
                if (collected.first().emoji.name == '795660107090100234') {
                    console.log("test")
                    message.reply("<:luffySmile:795058751421284382> **" + message.author.username + "** tu commences l'aventure MMO avec un Fruit du Démon au hasard !")}
                else{
                    message.reply("<:luffySmile:795058751421284382> **" + message.author.username + "** tu commences l'aventure MMO avec un Stand au hasard !")
                }
            })
            .catch(collected => {
                message.channel.send(`${message.author.toString()} n'a pas répondu après 1 minute.`);
            });
});

There are many mistakes in your code, and any of them could cause unexpected errors. Let's walk through them all and afterwards look at how you could debug such an error.

  1. Let's make sure every statement is terminated by a semicolon ; . Although this is unlikely to have been the cause of your issue, it's still better practice.
.addField("Quel pouvoir souhaiterais tu avoir au début de ton aventure ?", '<:gomuGomuNoMi:795660107090100234> **Un fruit du démon** (au hasard)\n<:standArrow:795658666681434112> **Un stand** (au hasard)\n<:dekuAllMight:795660876031459378> **Un alter** (au hasard, prochainement)\nTu as 60 secondes pour répondre.'); // <-- let's add one here
...
console.log("test"); // add one here
message.reply("<:luffySmile:795058751421284382> **" + message.author.username + "** tu commences l'aventure MMO avec un Fruit du Démon au hasard !"); // Add one here
...
message.reply("<:luffySmile:795058751421284382> **" + message.author.username + "** tu commences l'aventure MMO avec un Stand au hasard !"); // Last one here
  1. emoji.name here is the name of the emoji instead of the snowflake id that you are comparing them to, since you are using a custom emoji. Instead use emoji.id
  2. users.id == message.author.id , the message here is the message your bot sent and is awaiting reactions on, so that means when you compare to the users.id only your bot will trigger it.
  3. This is not an error, just a stylistic inaccuracy, try to either use ' or " , don't swap between them.

Lets move on to debugging.

First, I assume that you are not using a debugger since you are logging with console.log , so log collected in the catch block to see if awaitReactions has collected anything and just not processed it. Afterwards you can move your attention to the filter function for awaitReactions , I would move it out to its own function and log a few things to see if they are what I expect.

let filter = (reaction, user) => {
    console.log("user id:", user.id);
    console.log("message author id:", message.author.id);
    console.log("emoji name:", reaction.emoji.name);
    return user.id == message.author.id && (reaction.emoji.name == '795660107090100234' || reaction.emoji.name == '795658666681434112');
};

After logging these 3, you should notice error #2 and #3, and from there, it's a very simple fix.

let filter = (reaction, user) => {
    return user.id != message.author.id && (reaction.emoji.id == '795660107090100234' || reaction.emoji.id == '795658666681434112');
};

Hopefully, you've read this whole thing and not simply copy pasted my solution. I also speak french and think your idea is very interesting. Bonne chance!

EDIT: I've noticed the embed message has a .setAuthor , I think that it still doesn't set the author id to be the other person. But it may be something that I've overlooked, so tread lightly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM