本文介绍了如何删除客户端事件侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<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?

推荐答案

我假设 botClient,它扩展了 Node.js 的 EventEmitter.您可以使用 message 或 ready)="nofollow noreferrer">on 并使用 关闭.addListenerremoveListener 也分别是 onoff 的别名.

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)

这篇关于如何删除客户端事件侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:04