【发布时间】:2023-04-07 04:51:01
【问题描述】:
将我的机器人从 V11 升级到 V12.1.1 我有一个 messageReactionAdd as
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 行而返回。通道/表情符号/emojinames/arrayn/arrayr 在代码上方定义,但是在这一点上似乎不相关。
谁能指出为什么用户(我自己和 alt 帐户)在与机器人在同一频道/消息中使用相同的表情符号时没有注册反应事件(触发线 2)?
编辑:在这两种情况下,响应的消息都是在机器人打开后发送的。同样,在下面的代码中包含一个原始反应事件,以获取和处理未缓存的消息(但尚未调试为更简单,messageReactionAdd 不起作用)。
【问题讨论】:
-
确保在机器人启动后发送消息
-
感谢您指出这一点,已更新 OP。有问题的两个实例消息都是在机器人启动之后出现的,我有一个原始反应事件来获取未缓存的消息(但还没有看到它是否有效)。
标签: javascript discord.js