#How to Make The Bot Leave Voice Channel After Playing Sound

15 messages · Page 1 of 1 (latest)

hybrid torrent
#

I can't seem to find anywhere in the docs of how to leave the channel after the sound has been played. Any ideas?

hard stump
#

• 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.

sudden zodiac
#

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

reef pastureBOT
sudden zodiac
#

you can also listen to the event

#

audioplayer.on(AudioPlayerState.Idle, () => connection.disconnect())

hybrid torrent
#

what would be connection be defined as

sudden zodiac
#

your voice conection

hybrid torrent
#
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?

sudden zodiac
#

yea, that is what you want right? you have to "tell it" to do that, you can use getVoiceConnection(guildId).disconnect() I think

reef pastureBOT
#

method getVoiceConnection
Finds a voice connection with the given guild id and group. Defaults to the 'default' group.