const { ActionRowBuilder, ButtonBuilder, ButtonStyle, Events, Client, GatewayIntentBits, Partials, Collection, ActivityType, SelectMenuBuilder } = require("discord.js");
const { createReadStream } = require('node:fs');
const { join } = require('node:path');
const { AudioPlayerStatus, StreamType, createAudioResource, createAudioPlayer } = require('@discordjs/voice');
const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
],
});
// list of sounds to play
//use always "./files" like this
const sounds = [
'./audio1.mp3',
'./audio2.mp3',
'./audio3.mp3'
];
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
console.log(client.channels);
});
client.on('messageCreate', async message => {
if (message.content === '!play') {
// check if bot is in a voice channel
const channel = client.channels.cache.find(c => c.name === "・vc・");
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false // this may also
});
const player = createAudioPlayer();
// pick a random sound to play
const sound = sounds[Math.floor(Math.random() * sounds.length)];
// Thanks```
#music play but doesn't sound
9 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.
const resource = createAudioResource("./audio1.mp3", {
inputType: StreamType.WebmOpus,
inlineVolume: true,
metadata: {
title: 'A good song!',
},
});
resource.volume.setVolume(100);
// Not recommended - listen to errors from the audio player instead for most usecases!
resource.playStream.on('error', error => {
console.error('Error:', error.message, 'with track', resource.metadata.title);
});
player.play(resource);
player.on(AudioPlayerStatus.Playing, () => {
console.log('The audio player has started playing!');
});
}
})
you forgot to subscribe the connection to the audio player
How? @craggy mauve