#How to Make The Bot Leave Voice Channel After Playing Sound
15 messages · Page 1 of 1 (latest)
• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.
check the status of the player
once it gets into the idle state after being playing
you disconnect the voice conncection
audioplayer.state.status === AudioPlayerState.Idle i think is the enum
AudioPlayerState
The various states that the player can be in.
you can also listen to the event
audioplayer.on(AudioPlayerState.Idle, () => connection.disconnect())
what would be connection be defined as
your voice conection
const guild = client.guilds.cache.get(interaction.guild.id)
const member = guild.members.cache.get(interaction.member.user.id);
const channelId = member.voice.channel;
const rndInt = Math.floor(Math.random() * 1000) + 1
let specialChar = ""
if(rndInt == 500){
specialChar = "-s"
}
if(!channelId) return interaction.reply({content: 'Please join a voice channel', ephemeral: true })
const resource = createAudioResource('sounds/' + interaction.options.get("sound").value + specialChar + '.m4a', {
metadata: {
title: 'A good song!',
},
});
const player = createAudioPlayer()
joinVoiceChannel({
channelId: channelId.id,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator
}).subscribe(player)
player.play(resource)
player.on(AudioPlayerStatus.Idle, () => {
console.log("IDLE")
player.stop()
});
return interaction.reply({content: `I am playing ${interaction.options.get("sound").value} in ${channelId}`, ephemeral: true })
}
}```
I have this code, which does play the sound, and it prints idle however it doesnt seems to leave the channel
or do i need to tell it to leave the channel?
yea, that is what you want right? you have to "tell it" to do that, you can use getVoiceConnection(guildId).disconnect() I think
getVoiceConnection
Finds a voice connection with the given guild id and group. Defaults to the 'default' group.