How to remove this listener in another file?

Hello, So I have set up a listener like this:
const notify = require(`${process.cwd()}/utils/notify`);

user.client.addListener("voiceStateUpdate", (oldState, newState) => notify(oldState, newState));
const notify = require(`${process.cwd()}/utils/notify`);

user.client.addListener("voiceStateUpdate", (oldState, newState) => notify(oldState, newState));
notify.js obviously includes the function that should get triggered once the voice state of the specific user updates. Now I want to remove that listener again in another file later on. Issue is that I have no idea how to do that. Can someone help?
21 Replies
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs•3y ago
node nodeEventTarget.removeListener(type, listener) Node.js-specific extension to the EventTarget class that removes the listener for the given type. The only difference between removeListener() and removeEventListener() is that removeListener() will return a reference to the EventTarget.
ncls.
ncls.OP•3y ago
@jaw0r3k I know that function but it doesn't work. Or at least I'm applying it incorrectly. That's what I have:
user.client.removeListener("voiceStateUpdate", (oldState, newState) => notify(oldState, newState));
user.client.removeListener("voiceStateUpdate", (oldState, newState) => notify(oldState, newState));
So, I just went the brutal way and used removeAllListeners. But still have an issue. The notify-function uses the client variable but when I try to pass it like this:
user.client.addListener("voiceStateUpdate", (oldState, newState, client) => notify(oldState, newState, client));
user.client.addListener("voiceStateUpdate", (oldState, newState, client) => notify(oldState, newState, client));
It will be undefined in the notify-function
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
ncls.
ncls.OP•3y ago
Means?
monbrey
monbrey•3y ago
oldState.client and newState.client both exist I dont think you need to call the function to remove it You would do client.removeListener("voiceStateUpdate", notify)
const notify = (oldState, newState) => {...}
<Client>.addListener("voiceStateUpdate", notify);
<Client>.removeListener("voiceStateUpdate", notify);
const notify = (oldState, newState) => {...}
<Client>.addListener("voiceStateUpdate", notify);
<Client>.removeListener("voiceStateUpdate", notify);
ncls.
ncls.OP•3y ago
Doesn't seem to work
monbrey
monbrey•3y ago
That should, because you're maintaining a single reference to the function So it knows exactly what its adding/removing
ncls.
ncls.OP•3y ago
That's how I remove it:
const notify = require(`${process.cwd()}/utils/notify`);

// user.client.removeAllListeners("voiceStateUpdate");
user.client.removeListener("voiceStateUpdate", notify);
const notify = require(`${process.cwd()}/utils/notify`);

// user.client.removeAllListeners("voiceStateUpdate");
user.client.removeListener("voiceStateUpdate", notify);
The bot still reacts to my voiceStateUpdate-event. When I use the line above, it doesn't
monbrey
monbrey•3y ago
And how is it added?
ncls.
ncls.OP•3y ago
const notify = require(`${process.cwd()}/utils/notify`);

user.client.addListener("voiceStateUpdate", (oldState, newState) => notify(oldState, newState, client, user));
const notify = require(`${process.cwd()}/utils/notify`);

user.client.addListener("voiceStateUpdate", (oldState, newState) => notify(oldState, newState, client, user));
monbrey
monbrey•3y ago
yeah, that isnt the same function As soon as you do () => notify, you're declaring a new anonymous function There's really no need to add client and user, you can access those via newState.client and newState.member.user
ncls.
ncls.OP•3y ago
Ok, I changed that But when I add it like that:
user.client.addListener("voiceStateUpdate", notify(oldState, newState));
user.client.addListener("voiceStateUpdate", notify(oldState, newState));
It trows an error of oldState is not defined
monbrey
monbrey•3y ago
Please, look at the code I provided You do not need to call the function
ncls.
ncls.OP•3y ago
Ah, got it
monbrey
monbrey•3y ago
By passing the variable it calls it for you
ncls.
ncls.OP•3y ago
Yeah, makes sense...😅 Thank you a lot!
monbrey
monbrey•3y ago
No worries
ncls.
ncls.OP•3y ago
Another question: Do you know why it calls the function on voiceStateUpdate once for each user that the bot listens to? E.g.: This bot sends a message each time I start to stream or turn on the camera. Now, when I register someone else and the bot starts to listen to it, it triggers the notify-function twice, 3 times if I add another one and so on, but always for just that one user that initially triggered the event
monbrey
monbrey•3y ago
Thats just how Discord works And listeners As soon as you bind one voiceStateUpdate listener it will emit for every user that updates Theres no such thing a single-user listeners
ncls.
ncls.OP•3y ago
Hmm, ok. I have to implement one other thing anyway. I guess that'll fix this issue aswell. Thanks Just to let you know how I fixed it all: When the person first started the stream, I set their "Live State" to true in a Collection and then checked that if the person's Live State is set to true, I just returned:
const user = newState.member.user;

if (client.streamersLive.get(user.id)) return;

if (newState.streaming === true || newState.selfVideo === true) {
.streamersLive.set(user.id, true);
}
const user = newState.member.user;

if (client.streamersLive.get(user.id)) return;

if (newState.streaming === true || newState.selfVideo === true) {
.streamersLive.set(user.id, true);
}
Then I reset that value once the user left or switched the Voice Channel:
if (newState.channelId !== oldState.channelId && client.streamersLive.get(user.id)) {
client.streamersLive.delete(user.id);
}
if (newState.channelId !== oldState.channelId && client.streamersLive.get(user.id)) {
client.streamersLive.delete(user.id);
}
Want results from more Discord servers?
Add your server