Getting existing audio player

how do i get the audio player in a guild if any?
23 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Inky
Inky3y ago
Audio players are not specific to guilds, you can have many connections (across several guilds) subscribed to the same player
Shaurya
ShauryaOP3y ago
like i'm making a djs voice events handler but i create the player in the file where it is used, could i just do like
const djsvoice = require('@discordjs/voice');
const player = djsvoice.createAudioPlayer();
const djsvoice = require('@discordjs/voice');
const player = djsvoice.createAudioPlayer();
and it'll be good to go for handling the events?
Inky
Inky3y ago
Why would you need a handler for player events?
Shaurya
ShauryaOP3y ago
like for the queue system when the player gets idle i check if there's any song in the queue and then play it
Inky
Inky3y ago
I just register the events the normal way after creating the player Are you registering 20 events per player or smth?
Shaurya
ShauryaOP3y ago
wdym? thunk
Inky
Inky3y ago
player.on("idle", …).on("error", …)
Shaurya
ShauryaOP3y ago
can i like create the player initially and export it and can use it?
Inky
Inky3y ago
Why would you do that? You want the same thing to play in every guild?
Shaurya
ShauryaOP3y ago
no no lol
Inky
Inky3y ago
Then no, you can’t do that
Shaurya
ShauryaOP3y ago
like i just need to use the events to do different things but like i create the player in the play.js file then how would i use the player in other files?
Inky
Inky3y ago
Either store it separately or use connection.state.player connection is obtainable by getVoiceConnection(guildId)
Shaurya
ShauryaOP3y ago
oh that returns the player?
Inky
Inky3y ago
Assuming the connection is subscribed to the player
Shaurya
ShauryaOP3y ago
hmm got it, i subscribe the player like this:
connection.subscribe(player);
connection.subscribe(player);
it's ok right
Inky
Inky3y ago
Yea Oop, forgot .subscription connection.state.subscription.player
Shaurya
ShauryaOP3y ago
oh ._.
Inky
Inky3y ago
Yea, not rly a big fan of it tbh
Shaurya
ShauryaOP3y ago
hmm
Inky
Inky3y ago
mmh
Shaurya
ShauryaOP3y ago
hey btw, what event should i use for getting the guild id

Did you find this page helpful?