izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

Removing Event Listeners #280

Open macharborguy opened 6 years ago

macharborguy commented 6 years ago

I am implementing Discord.io into our Twitch channel bot to work both in Twitch IRC and our Discord server. Part of my workflow involves an initial connection to the server via an Async.js "series" control flow.

With the Twitch library I use, I have the connection event on a .once() listener, and after a successful connection I have an .on() handler for the connection event to deal with reconnections.

With Discord.io, I want to have two .once() handlers, one for the successful initial connection, and one for a potential connection error. However, I would also like to remove these listeners after a successful connection or failed connection (on success, remove disconnect listener, on failure, remove connection listener). After removal and successful connection, i would create a new disconnect handler to deal with reconnections and other web socket errors.

However, it appears that while .on() and .once() ARE supported, .off() and .removeEventListener() are not supported. They both throw an error that "client.off / client.removeEventListener are not functions".

Is there a way with the client object to remove listeners as needed?

macharborguy commented 6 years ago

After doing what I should have done and looked at the source code, i found 'removeListener'. I forgot that Node's 'Events' module uses removeListener and not removeEventListener. Although, could the 'off' method be added as an alias to removeListener?