I am attempting to make a music bot for my discord server. When I run the code, the bot joins my call however it instantly stops the code and it executes the "An error occurred trying to connect."
It says in the console:
The only part of code where 'adapterCreator' comes from is here:
if (command === 'play'){
if (!args.length) return message.channel.send('Please provide the second argument!');
let song = {};
joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
})
The code that defines message:
This is all under async execute(message, args, command, client, Discord){} in the play.js folder
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('You must be in a channel to run this command!')
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.channel.send('You do not have the permissions to do this!');
if(!permissions.has('SPEAK')) return message.channel.send('You do not have the permissions to do this!');
const server_queue = queue.get(message.guild.id);
In main.js Command Handler:
else if (command === 'play'){
client.commands.get('play').execute(message, args, command);
}
EDIT #2: Showing intents:
const { Client, Intents, DiscordAPIError, Collection } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '-'
const fs = require('fs');
client.commands = new Collection();
const command = require('./command')
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
I just tried the same way you did it in your code and it worked just fine and I can't reproduce the crash. So the problem might not be the code.
Have you tried to update the packages ? (npm update)
You have a DeprecationWarning for the on message event. This won't fix the problem but it would be good to change it to messageCreate.
Also to play music later you will need the connection when the bot joins a voice channel. Assign the result from joinVoiceChannel to a variable.