qeled / discordie

Predictable JavaScript abstractions for Discord API.
https://qeled.github.io/discordie/
BSD 2-Clause "Simplified" License
190 stars 45 forks source link

How to close a MESSAGE_CREATE event listener? [QUESTION] #113

Closed Hacksawfred3232 closed 6 years ago

Hacksawfred3232 commented 6 years ago

Okay, so complete noob here to JavaScript (And this API in general >_<) and i got a question to ask. How does one kill the MESSAGE_CREATE event listener - "i" - so i don't get this occurrence?: https://cdn.discordapp.com/attachments/214858024815296512/382568228704223233/unknown.png because it works perfectly fine, it just does not terminate the handler after its done. Here is a snippet of the code: ` case 'Tri#DNSlookup': var OrignSender = e.message.author.username e.message.channel.sendMessage("Handler started! Please post the URL in chat now."); client.Dispatcher.on("MESSAGE_CREATE", i => { if (i.message.author.username != OrignSender) {return;} else { dns.lookup(i.message.content, (err, address, family) =>{ var IP = address var IPV = family console.log("Dns lookup was requested. Requested IP of " + i.message.content + ". The result from lookup was " + IP + " IPV" + IPV); console.log("Completed Lookup at " + getDateTime()); i.message.channel.sendMessage("Request completed, Results as following:"); i.message.channel.sendMessage("Address of " + i.message.content + ": " + IP); i.message.channel.sendMessage("IP version: IPv" + IPV); i.message.channel.sendMessage("Handler stopped!");

client.Dispatcher.removeListener("MESSAGE_CREATE", i);<< }); }}); break; ` I know, horrible code. Using a switch statement to provide commands and a dirty way to get input but first attempt, why the heck not? However, i get this error when it gets to the removerListener function: https://cdn.discordapp.com/attachments/345579787965825025/382636076168380426/unknown.png Anyway to handle this correctly?

Dougley commented 6 years ago
  1. This is not an issue related to Discord or Discordie, rather a general JS question.
  2. You could've just Googled and found the Node.js API docs.
Hacksawfred3232 commented 6 years ago

Yeah..... Heres the problem. according to the API docs, the removeListener command IS the only way to close the listener. And Discordie API mentions nothing about closing dispatcher listeners. so how does one get around this problem? Also that is the same page i used to try and fix this myself. @Dougley You see the problem here?

Dougley commented 6 years ago

Fully closing the connection to Discord, and "closing the dispatcher" at the same time can be done with <Client>.disconnect().


Removing a listener from the dispatcher without terminating the entire connection can be done via <EventEmitter>.removeListener(), as you've found. Keep in mind that the function must be named to remove it properly.

Client.Dispatcher.on('MESSAGE_CREATE', function onMessage (c) {
  console.log('Got message!')
  Client.Dispatcher.removeListener(onMessage)
})
Hacksawfred3232 commented 6 years ago

Oh, so my problem was that i was not running it as a function. Okay, that is something i need to keep in mind. Thanks for the heads up! @Dougley