Beginner to Discord JS, Audio Not Playing

Hello, I just started learning discord js, i wrote a simple connect and playcommand using the guide but didnt work, asked gpt to add some state logs and here is the code. The bot joins the channel but no sound is played
const { SlashCommandBuilder } = require('discord.js');
const {
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
AudioPlayerStatus,
VoiceConnectionStatus,
entersState,
} = require('@discordjs/voice');
const { join } = require('node:path');
const fs = require('node:fs');

module.exports = {
data: new SlashCommandBuilder()
.setName('nya')
.setDescription('test me nya!'),

async execute(interaction) {
const channel = interaction.member.voice.channel;

if (!channel) {
return interaction.reply({ content: 'āŒ You must be in a voice channel!', ephemeral: true });
}

await interaction.reply('Nyaa~ 🐱');

// Join voice channel
console.log('šŸ”§ Connecting to voice channel...');
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
// selfDeaf: false,
});

connection.on('stateChange', (oldState, newState) => {
console.log(`šŸ”„ Voice connection: ${oldState.status} -> ${newState.status}`);
});

// Try to wait until connection is ready (handle timeout gracefully)
try {
await entersState(connection, VoiceConnectionStatus.Ready, 15_000);
console.log('āœ… Voice connection is ready!');
} catch (error) {
console.warn('āš ļø Voice connection is taking too long. Continuing anyway...');
}

// Prepare audio file
const filePath = join(__dirname, 'nya.wav');
console.log('šŸ“ Resolved audio path:', filePath);

if (!fs.existsSync(filePath)) {
console.error('āŒ Audio file not found:', filePath);
return interaction.followUp({ content: 'āŒ Audio file not found.', ephemeral: true });
}

const resource = createAudioResource(filePath, { inlineVolume: true });
resource.volume.setVolume(0.5);

// Create player
const player = createAudioPlayer();

player.on('stateChange', (oldState, newState) => {
console.log(`šŸŽµ Audio player: ${oldState.status} -> ${newState.status}`);

if (newState.status === AudioPlayerStatus.AutoPaused) {
console.warn('āš ļø Audio auto-paused! Probably alone in voice channel.');
const members = channel.members.filter(member => !member.user.bot);
console.log(`šŸ‘„ Non-bot members in VC: ${members.size}`);
}

if (newState.status === AudioPlayerStatus.Idle && oldState.status !== AudioPlayerStatus.Idle) {
console.log('šŸ›‘ Playback finished. Leaving voice channel.');
connection.destroy();
}
});

player.on(AudioPlayerStatus.Playing, () => {
console.log('āœ… Audio is playing!');
});

player.on('error', error => {
console.error('āŒ Audio player error:', error.message);
});

connection.subscribe(player);
console.log('ā–¶ļø Subscribed to player. Starting playback...');
player.play(resource);
},
};
const { SlashCommandBuilder } = require('discord.js');
const {
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
AudioPlayerStatus,
VoiceConnectionStatus,
entersState,
} = require('@discordjs/voice');
const { join } = require('node:path');
const fs = require('node:fs');

module.exports = {
data: new SlashCommandBuilder()
.setName('nya')
.setDescription('test me nya!'),

async execute(interaction) {
const channel = interaction.member.voice.channel;

if (!channel) {
return interaction.reply({ content: 'āŒ You must be in a voice channel!', ephemeral: true });
}

await interaction.reply('Nyaa~ 🐱');

// Join voice channel
console.log('šŸ”§ Connecting to voice channel...');
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
// selfDeaf: false,
});

connection.on('stateChange', (oldState, newState) => {
console.log(`šŸ”„ Voice connection: ${oldState.status} -> ${newState.status}`);
});

// Try to wait until connection is ready (handle timeout gracefully)
try {
await entersState(connection, VoiceConnectionStatus.Ready, 15_000);
console.log('āœ… Voice connection is ready!');
} catch (error) {
console.warn('āš ļø Voice connection is taking too long. Continuing anyway...');
}

// Prepare audio file
const filePath = join(__dirname, 'nya.wav');
console.log('šŸ“ Resolved audio path:', filePath);

if (!fs.existsSync(filePath)) {
console.error('āŒ Audio file not found:', filePath);
return interaction.followUp({ content: 'āŒ Audio file not found.', ephemeral: true });
}

const resource = createAudioResource(filePath, { inlineVolume: true });
resource.volume.setVolume(0.5);

// Create player
const player = createAudioPlayer();

player.on('stateChange', (oldState, newState) => {
console.log(`šŸŽµ Audio player: ${oldState.status} -> ${newState.status}`);

if (newState.status === AudioPlayerStatus.AutoPaused) {
console.warn('āš ļø Audio auto-paused! Probably alone in voice channel.');
const members = channel.members.filter(member => !member.user.bot);
console.log(`šŸ‘„ Non-bot members in VC: ${members.size}`);
}

if (newState.status === AudioPlayerStatus.Idle && oldState.status !== AudioPlayerStatus.Idle) {
console.log('šŸ›‘ Playback finished. Leaving voice channel.');
connection.destroy();
}
});

player.on(AudioPlayerStatus.Playing, () => {
console.log('āœ… Audio is playing!');
});

player.on('error', error => {
console.error('āŒ Audio player error:', error.message);
});

connection.subscribe(player);
console.log('ā–¶ļø Subscribed to player. Starting playback...');
player.play(resource);
},
};
Here is what gets logged: šŸ”§ Connecting to voice channel... āš ļø Voice connection is taking too long. Continuing anyway... šŸ“ Resolved audio path: h:\Programming\Discord Bot Practice\discord-bot\commands\voice\nya.wav šŸ”„ Voice connection: signalling -> signalling ā–¶ļø Subscribed to player. Starting playback... šŸŽµ Audio player: idle -> buffering šŸŽµ Audio player: buffering -> playing āœ… Audio is playing! šŸŽµ Audio player: playing -> autopaused āš ļø Audio auto-paused! Probably alone in voice channel. šŸ‘„ Non-bot members in VC: 1 any guidance will be appreciated <3
2 Replies
d.js toolkit
d.js toolkit•4mo ago
- What are your intents? GuildVoiceStates is required to receive voice data! - Show what dependencies you are using -- generateDependencyReport() is exported from @discordjs/voice. - Try looking at common examples: https://github.com/discordjs/voice-examples. - 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! - āœ… Marked as resolved by staff
Zash Shadow
Zash ShadowOP•4mo ago
thank you scripted bot, it works now it was the intents i really think the guide should tell you that tho

Did you find this page helpful?