我有一个<Client>.on函数,我想停止监听这个事件,例如:
I have a <Client>.on function and I want to stop the listening of this event, example:
bot.on('messageCreate', message => {
// some code
});
如何停止它并且不再接收此事件?
How to stop it and not receive this event anymore?
我假设 bot 是 Client,它扩展了 Node.js 的 EventEmitter.您可以使用 message 或 ready)="nofollow noreferrer">on 并使用 关闭.addListener 和 removeListener 也分别是 on 和 off 的别名.
I'm assuming bot is an instance of Client, which extends Node.js' EventEmitter. You can listen to an event (for example message or ready) using on and remove that listener using off. addListener and removeListener are also aliases of on and off respectively.
将监听器函数存储在一个变量中,当你需要移除那个监听器时使用 bot.off(eventName, listener):
Store the listener function in a variable, and when you need to remove that listener use bot.off(eventName, listener):
const listener = () => {
// your code here...
}
bot.on('event', listener)
// When you need to remove the listener:
bot.off('event', listener)
这篇关于如何删除客户端事件侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
discord.js v12:我如何等待 DM 频道中的消息?discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 频道中的消息?)
如何让我的机器人提及发出该机器人命令的人how to make my bot mention the person who gave that bot command(如何让我的机器人提及发出该机器人命令的人)
如何修复必须使用导入来加载 ES 模块 discord.jsHow to fix Must use import to load ES Module discord.js(如何修复必须使用导入来加载 ES 模块 discord.js)
如何列出来自特定服务器的所有成员?How to list all members from a specific server?(如何列出来自特定服务器的所有成员?)
Discord bot:修复“找不到 FFMPEG"Discord bot: Fix ‘FFMPEG not found’(Discord bot:修复“找不到 FFMPEG)
使用 discord.js 加入 discord 服务器时的欢迎消息Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服务器时的欢迎消息)