The bot connects to the voice channel, but there is no sound. Here's my code status.
I've tried many ways, but it's the same reaction over and over again. I need your advice
const { createAudioResource, createAudioPlayer, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, generateDependencyReport, getVoiceConnection, VoiceConnectionStatus } = require('@discordjs/voice');
module.exports = {
name: "play",
async execute(message, args, client ) {
arguments = args.shift(2)
///const arguments = args.shift(1)
////console.log(arguments)
const connection = joinVoiceChannel({
channelId: message.member.voice.channelId,
guildId: message.guildId,
adapterCreator: message.guild.voiceAdapterCreator
})
const player = createAudioPlayer()
const resource = createAudioResource('../music/123.mp3')
player.play(resource, {seek: 0, volume: 1.0})
await wait(5000)
console.log("time")
connection.destroy();
}
}
and pakage
"dependencies": {
"@discordjs/builders": "^1.2.0",
"@discordjs/opus": "^0.8.0",
"@discordjs/rest": "^1.1.0",
"@discordjs/voice": "^0.11.0",
"discord-api-types": "^0.37.8",
"discord.js": "^14.3.0",
"ffmpeg-static": "^5.1.0",
"libsodium-wrappers": "^0.7.10",
"ms": "^2.1.3",
"node.js": "^0.0.1-security",
"redis": "^4.3.1"
The problem with your code is that you did not subscribe to the connection. You should do:
const connection = joinVoiceChannel({
channelId: message.member.voice.channelId,
guildId: message.guildId,
adapterCreator: message.guild.voiceAdapterCreator
})
const player = createAudioPlayer()
const resource = createAudioResource('../music/123.mp3')
player.play(resource, {seek: 0, volume: 1.0})
connection.subscribe(player);
And if you want to destroy the connection after the resource ended, You can use the idle
event:
const {AudioPlayerStatus} = require('@discordjs/voice');
player.on(AudioPlayerStatus.Idle, () => {
connection.destroy();
});