if 条件不会停止启用
Posted
技术标签:
【中文标题】if 条件不会停止启用【英文标题】:The if condition doesn't stop being enabled 【发布时间】:2020-08-18 19:06:25 【问题描述】:我正在为我的学校编写一个机器人,我目前正在开发一项功能,如果学生发送特定消息并且如果老师使用特定表情符号对其做出反应,则该功能使学生能够在语音频道中交谈。这是代码。
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
是一个函数,其中事件在通道和控制台中发送。
这是我的问题: 学生第一次请求发言权限时,一切正常,但如果之后,另一个学生通过 handRaiseMode 获得发言权限,则之前请求发言的所有学生都将被取消静音......看起来像 @987654327 @ 仍在工作,尽管它应该已经结束。我真的不明白它是如何工作的。需要帮助!
提前感谢您的回答。
【问题讨论】:
如果我的回答解决了您的问题,请接受它,让其他人知道什么有效 【参考方案1】:我认为你的问题可能是因为你嵌套了你的事件,这是一个坏习惯,因为它会导致内存泄漏等等。
我认为您的问题可以使用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!');
);
【讨论】:
【参考方案2】:这段代码应该可以工作,你的想法真的很酷!希望我的学校使用 discord 为他们编写机器人代码会很有趣:)
如果您对此代码有任何问题,请随时发表评论,我会修复它
注意:我已经测试了这段代码,它似乎对我有用,我使用这些值/函数进行测试
handRaiseModeActive = true;
function superConsole(str)
console.log(str)
// I will use this function, it makes it really easy to change later on.ah
async function addEmoji(message, validReactions)
// Valid reactions = ["emoji", "emoji"]
for (const reaction of validReactions) await message.react(reaction)
// Define the filter. (const filter = $condition)
const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && (!user.bot) // && (user.id == teacherID || devID.includes(user.id))
// return the name of the emoji that the user reacted (with filters applied)
return message
.awaitReactions(filter,
max: 1 // max amount of reactions it will read, (1 = waits for one reaction so forth)
/* if you wanted to you could also add a time limit
* time: $time (int)
*/
)
// grab the first emoji that was reacted
.then(collected => collected.first() && collected.first().emoji.name);
async function putYourHandsToTheSky(handMsg)
// Better to use this than what you have, if someone tries to explain the !talk command to someone it will run
superConsole(`**@$handMsg.author.tag asked to speak ||\`$handMsg.author.id\`||**`)
/* run the addEmoji function passing in :
* the message variable,
* and a array with the valid reactions that it will await for
*/
const emoji = await addEmoji(handMsg, ["✅", "❎"])
let myHomie = handMsg.guild.members.get(handMsg.author.id);
if (emoji == "✅")
superConsole(`handRaiseMode | permission to speak given to @$handMsg.author.tag ||\`$handMsg.author.id\`||`)
await myHomie.setMute(false)
if (handMsg.deletable == true)
handMsg.delete() // deletes message, you can change this if you want. just cleans up my chat while testing this
else
superConsole(`I cannot delete this message! Role hierarchy I suppose...`)
else if (emoji == "❎")
superConsole(`handRaiseMode | permission to speak denied to @$handMsg.author.tag ||\`$handMsg.author.id\`||`)
if (handMsg.deletable == true)
handMsg.delete() // deletes message, you can change this if you want. just cleans up my chat while testing this
else
superConsole(`I cannot delete this message! Role hierarchy I suppose...`)
client.on('message', (handMsg) =>
if (handMsg.content.startsWith(PREFIX + talk) && handMsg.channel.id == hgCID)
putYourHandsToTheSky(handMsg)
);
【讨论】:
以上是关于if 条件不会停止启用的主要内容,如果未能解决你的问题,请参考以下文章