how do i play audio with my discord bot?

i have this code
const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel } = require('@discordjs/voice');

module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('Plays music'),
async execute(interaction) {
const guild = interaction.guild;
const member = interaction.member;

// Check if the member is in a voice channel
if (member.voice.channel) {
interaction.reply('Joining: ' + member.voice.channel.name);
// The member is in a voice channel
const connection = joinVoiceChannel({
channelId: member.voice.channel.id,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator,
});

//use file: './sound/AAAAUUUGHHHH.mp3'

} else {
interaction.reply('You need to be in a voice channel to use this command!');
}
},
};
const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel } = require('@discordjs/voice');

module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('Plays music'),
async execute(interaction) {
const guild = interaction.guild;
const member = interaction.member;

// Check if the member is in a voice channel
if (member.voice.channel) {
interaction.reply('Joining: ' + member.voice.channel.name);
// The member is in a voice channel
const connection = joinVoiceChannel({
channelId: member.voice.channel.id,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator,
});

//use file: './sound/AAAAUUUGHHHH.mp3'

} else {
interaction.reply('You need to be in a voice channel to use this command!');
}
},
};
and i wanna play the file
'./sound/AAAAUUUGHHHH.mp3'
'./sound/AAAAUUUGHHHH.mp3'
but i have no idea how i could do that please help me out
4 Replies
d.js toolkit
d.js toolkit4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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!
mallusrgreat
mallusrgreat4mo ago
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
montur
montur4mo ago
i have it installed im just looking for some example code so far i have this
const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource } = require('@discordjs/voice');
const player = createAudioPlayer();

module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('Plays music'),
async execute(interaction) {
const guild = interaction.guild;
const member = interaction.member;

// Check if the member is in a voice channel
if (member.voice.channel) {
interaction.reply('Joining: ' + member.voice.channel.name);
// The member is in a voice channel
const connection = joinVoiceChannel({
channelId: member.voice.channel.id,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator,
});
const resource = createAudioResource('discord bot/sound/AAAAUUUGHHHH.mp3');

// Play "track.mp3" across two voice connections
connection.subscribe(player);
player.play(resource);


} else {
interaction.reply('You need to be in a voice channel to use this command!');
}
},
};
const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource } = require('@discordjs/voice');
const player = createAudioPlayer();

module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('Plays music'),
async execute(interaction) {
const guild = interaction.guild;
const member = interaction.member;

// Check if the member is in a voice channel
if (member.voice.channel) {
interaction.reply('Joining: ' + member.voice.channel.name);
// The member is in a voice channel
const connection = joinVoiceChannel({
channelId: member.voice.channel.id,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator,
});
const resource = createAudioResource('discord bot/sound/AAAAUUUGHHHH.mp3');

// Play "track.mp3" across two voice connections
connection.subscribe(player);
player.play(resource);


} else {
interaction.reply('You need to be in a voice channel to use this command!');
}
},
};
it joins the vc but it still doesnt play now it gives me this error
Error: FFmpeg/avconv not found!
at FFmpeg.getInfo (C:\Users\Alex\Desktop\discord bot\node_modules\prism-media\src\core\FFmpeg.js:143:11)
at FFmpeg.create (C:\Users\Alex\Desktop\discord bot\node_modules\prism-media\src\core\FFmpeg.js:156:38)
at new FFmpeg (C:\Users\Alex\Desktop\discord bot\node_modules\prism-media\src\core\FFmpeg.js:45:27)
at Object.transformer (C:\Users\Alex\Desktop\discord bot\node_modules\@discordjs\voice\dist\index.js:2248:29)
at C:\Users\Alex\Desktop\discord bot\node_modules\@discordjs\voice\dist\index.js:2457:58
at Array.map (<anonymous>)
at createAudioResource (C:\Users\Alex\Desktop\discord bot\node_modules\@discordjs\voice\dist\index.js:2457:39)
at Object.execute (C:\Users\Alex\Desktop\discord bot\commands\utility\play.js:24:30)
at Client.<anonymous> (C:\Users\Alex\Desktop\discord bot\main.js:50:17)
at Client.emit (node:events:514:28)
node:events:492
throw er; // Unhandled 'error' event
^


Node.js v18.17.1
Error: FFmpeg/avconv not found!
at FFmpeg.getInfo (C:\Users\Alex\Desktop\discord bot\node_modules\prism-media\src\core\FFmpeg.js:143:11)
at FFmpeg.create (C:\Users\Alex\Desktop\discord bot\node_modules\prism-media\src\core\FFmpeg.js:156:38)
at new FFmpeg (C:\Users\Alex\Desktop\discord bot\node_modules\prism-media\src\core\FFmpeg.js:45:27)
at Object.transformer (C:\Users\Alex\Desktop\discord bot\node_modules\@discordjs\voice\dist\index.js:2248:29)
at C:\Users\Alex\Desktop\discord bot\node_modules\@discordjs\voice\dist\index.js:2457:58
at Array.map (<anonymous>)
at createAudioResource (C:\Users\Alex\Desktop\discord bot\node_modules\@discordjs\voice\dist\index.js:2457:39)
at Object.execute (C:\Users\Alex\Desktop\discord bot\commands\utility\play.js:24:30)
at Client.<anonymous> (C:\Users\Alex\Desktop\discord bot\main.js:50:17)
at Client.emit (node:events:514:28)
node:events:492
throw er; // Unhandled 'error' event
^


Node.js v18.17.1
i ran
npm install ffmpeg
npm install ffmpeg
and it still says it
d.js docs
d.js docs4mo ago
- npm: npm install ffmpeg-static - Install: Download | chocolatey | homebrew | your distributions package manager - Tutorial: YouTube - ffmpeg-binaries is deprecated, uninstall it with npm rm ffmpeg-binaries :guide: Getting Started: Introduction - Installation > Extra Dependencies read more