TehPirate TehPirate - 3 months ago 16
Node.js Question

What's the appropriate use of removeListener in this case?

Bot library for Discord,

bot.on('messageCreate', msg => {
// do some stuff
bot.removeListener('messageCreate', msg);
});


An error I get often is
listener must be a function
.
messageCreate is fired when a message is received, msg is a reference to the message class, which contains the message id, author, etc.

What I'm trying to get is the bot waiting for a specific reply back from the user, such as Yes or No.

Answer

If bot is a Node's EventEmitter, you can use once method instead of on so that it automatically removes the listener after the listener is called once.

bot.once('messageCreate', msg => {
  // do some stuff
  // you don't need to remove the listener by yourself!
});

To straightly answer your question, the second argument of removeListener must be the listener function that you passed to on.

var listener = msg => {
  // do some stuff
  bot.removeListener('messageCreate', listener);
};
bot.on('messageCreate', listener);