How to correctly make a VoiceState event using eventHandler
I created the voiceXP.js event, which was supposed to grant experience every minute while in the Voice Chat. However, for some reason, the added console.log statements are not working, and XP is not being awarded. It's as if the "VoiceState" event is not triggering at all
voiceXP.js:
If you add it to index.js, it produces messages normally
39 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!well, do you have the GuildVoiceStates intent enabled?
How are you importing and executing the exported function from voiceXP?
Using:
Of course, because if I'm directly from index.js I'm trying to trigger everything goes fine
I’m confused at how you’re expecting the voiceXP function to execute. Are you importing it like it’s an event?
Yep
Here is my other MessageCreate event, it works fine
What folder is the voiceXP.js in?
Full project
https://replit.com/@keymax32/Shiza
keymax32
replit
Shiza
Run Node.js (Beta) code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
I’m not sure if that event file is being imported. It looks like it should log 'work' to the console but isn’t. Id log
eventName, eventFiles
in your event handler outside the listener.Just "giveUserXp.js" is working fine
Is voiceXP being imported?
I'm not sure, but at least the first WORK output, which is declared at the very beginning of the code, works.
But the rest are gone
I don't understand, but only today he started giving an error. Before that, he was just silent and did not react
Thanks for the help, I really added to EventHandler.js 1 more argument for events and everything started to work stably. Thanks a lot ❤️
Spread syntax (...)
The spread (...) syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected. In an object literal, the spread syntax enumerates the properties of an object and adds the key-value pairs to the object being created.
💗
I have the same value displayed in the console,
do I not understand something?
I want to check the current state of the user and compare it, but it gives the same thing. How do I understand what is happening to a person right now
They could be doing something else besides joining or leaving. Such as muting deafening or other actions that modify voiceState.
You would have to check the differences and handle them.
Yes, I understand, but even if I output these values in the console, they are still always the same:
Mute and unmute:
Deaf and undeaf
I specified the variables correctly:
Something is changing to fire the event. Those are only two examples of state changes.
You could try to compare the entries of both old and new state objects and filter the ones that don’t match if you really want to see what changed.
I just thought if I join a voice chat, then:
But for some reason these values are always the same
oldState.channel should be null if joining.
Okay, I figured it out, because I tried to import this event as a separate file. However, if I prescribe directly from index.js works everything out fine
You still have a parameter issue when you call the execute() function in your handler.
You pass the same object as the second and third argument. So oldMember and newMember are the same object
I would review the event handler portion of the guide and see how the args are passed to the event listeners with separate files.
This was an attempt to resolve that issue.
I didn't really understand, do I need to declare in this way?
Could be
Use arrays in both cases.
You can pass single arguments as an array with one element. This ensures consistency but might require additional handling inside the function.
Inside eventFunction, you would handle the arguments as an array:
Check the type of arg before calling.
If arg is an array, you can use the spread syntax; otherwise, pass it as is.
Modify the structure of eventFunction to always accept an array.
This way, you won't have to worry about the number of arguments you're passing.
Depending on your specific scenario, one of these options might be more preferable. Choose the one that best fits your requirements and coding style.
<a:sweetpiano_happy:1051864341835698276>
I finally understood the incorrectly described import of events,
old
that's how it should be
new
Those are the same, just that one uses arrow functions instead
I don't know, but the first option didn't work for me
Why, everything works fine and works fine. It conveys oldState and newState well
keymax32
replit
Shiza
Run Node.js (Beta) code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
The arguments object
arguments is an array-like object accessible inside functions that contains the values of the arguments passed to that function.