Discord.js V12 messageReactionAdd仅在机器人做出反应时触发

时间:2020-04-25 17:16:31

标签: javascript discord.js

我的机器人从V11升级到V12.1.1,我有一个messageReactionAdd作为

client.on("messageReactionAdd", (reaction, user) => {
  console.log("Reaction added");
  if (user.id == client.user.id){return console.log("Reaction is from Lillette");}
  const chanid = reaction.message.channel.id;
  console.log(chanid);
  let chanchk = (reactchannels.indexOf(chanid) > -1);
  console.log(chanchk);
  if(chanchk === false){return console.log("Reaction not in a valid channel");}


  for (var i = 0; i < reactchannels.length; i++){
    if (reactchannels[i] == chanid && i == 0){arrayn = reactnames.one, arrayr = reactroles.one, console.log(`Success at i=${i}`);}
    else if (reactchannels[i] == chanid && i == 1){arrayn = reactnames.two, arrayr = reactroles.two, console.log(`Success at i=${i}`);}
    else {arrayn = [], arrayr = [];}
    if(arrayn.length !== arrayr.length){/*return*/ console.log("Arrays not equal length");}

    const emoji = reaction.emoji.name; //"cat";
    const guildmem = reaction.message.guild.members.cache.get(user.id);
    for (var e = 0; e < arrayn.length; e++) {
      if (emoji == arrayn[e]) {
        console.log(`Found emoji named ${arrayn[e]}, corresponding role = ${arrayr[e]}`);
        var findrole = reaction.message.guild.roles.cache.find(g => g.name === arrayr[e]);
        if (findrole == null) {return console.log("User has reacted with a valid emoji, but the role does not exist in the guild");}//End if no roles found
        guildmem.roles.add(`${findrole.id}`);//assign role
        return; //no need to continue loop if role found
      }//End of if
      //else {console.log(`No roles found for ${arrayn[e]}`);}
    }//End of for e
  }//end of for i
  if (arrayn.length == 0 || arrayr.length == 0){/*return*/ console.log("END -- arrayn or arrayr lengths are equal to 0");}
});

是的,它并不整洁,但它不会通过第2行console.log("Reaction added");,除非机器人对消息作出反应(在这种情况下,它会由于第3行而返回)。Channel / emoji / emojinames / arrayn / arrayr在代码,但目前似乎并不相关。

有人能指出我为什么在与机器人相同的频道/消息中使用相同表情符号时,用户(我自己和alt帐户)未注册反应事件(第2行)吗?

编辑:在两种情况下,打开机器人后都会发送对消息作出反应的消息。同样,下面的代码中包含一个原始反应事件,用于抓取和处理未缓存的消息(但由于更简单,MessageReactionAdd尚无法调试,因此尚未调试)。

1 个答案:

答案 0 :(得分:0)

似乎与以下原始事件发生冲突。上面的代码应该独立运行。