gpt4 book ai didi

javascript - if 条件不会停止启用

转载 作者:行者123 更新时间:2023-12-01 15:42:08 24 4
gpt4 key购买 nike

我正在为我的学校编写一个机器人,我目前正在开发一个功能,如果学生发送了特定的消息并且如果老师用特定的表情符号对其使用react,学生可以在语音 channel 中交谈。这是代码。

client.on('message', handMsg => {
if (!handRaiseModeActive) return;
if ((handMsg.content.includes(PREFIX + 'talk')) && handMsg.channel.id === hgCID) {
superConsole(`**@${handMsg.author.tag} asked to speak ||\`${handMsg.author.id}\`||**`)
handMsg.react('✅')
handMsg.react('❎')
client.on('messageReactionAdd', (reaction, user) => {

if (reaction._emoji.name == '✅' && user.id !== botID && (user.id == teacherID || devID.includes(user.id))) {
handMsg.member.voice.setMute(false)
handMsg.reactions.removeAll();
superConsole(`handRaiseMode | permission to speak given to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
} else if (reaction._emoji.name == '❎' && user.id !== botID && (user.id == teacherID || devID.includes(user.id))) {
handMsg.reactions.removeAll()
superConsole(`handRaiseMode | permission to speak denied to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
}

});
}
})
teacherID 是老师的 ID, devID 是一个包含所有开发者 ID 的数组,而 botID ... 机器人 ID。一条命令将 handRaiseModeActive 设置为真或假。 superConsole 是在 channel 和控制台中发送事件的函数。
这是我的问题:
学生第一次请求发言权限时,一切正常,但如果之后,另一个学生通过 handRaiseMode 获得发言权限,则之前要求发言的所有学生都被取消静音......似乎 l.3 是尽管它应该已经结束,但它仍在工作。我真的不明白它是如何工作的。需要帮忙!
提前感谢您的回答。

最佳答案

我认为您的问题可能是因为您正在嵌套事件,这是一个坏习惯,因为它可能导致内存泄漏等等。

我认为您的问题可以使用 awaitReactions() 轻松解决反而:

const filter = (reaction, user) => {
return ['✅', '❎'].includes(reaction.emoji.name) && (user.id == teacherID || devID.includes(user.id))
};

handMsg.awaitReactions(filter, {
max: 1,
time: 60000,
errors: ['time']
})
.then(collected => {
const reaction = collected.first();

if (reaction.emoji.name === '✅') {
handMsg.member.voice.setMute(false)
handMsg.reactions.removeAll();
superConsole(`handRaiseMode | permission to speak given to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
} else {
handMsg.reactions.removeAll()
superConsole(`handRaiseMode | permission to speak denied to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
}
})
.catch(collected => {
handMsg.reply('the teacher did not give you permission in time!');
});

关于javascript - if 条件不会停止启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61590819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com