Skip to content
Advertisement

My bot can’t take errors when failling to run a “join” voice channel command

I’m currently coding a discord.js bot and I made this join command so the bot can join my voice channel

module.exports.run = async (client, message, args) => {

let membervc = message.member.voice.channel;
let botvc = message.guild.me.voice.channel;
if(!membervc) return message.send('You need to join a voice channel first.');
if(botvc){
    if(!(botvc.members.size - 1)) return message.reply(`I am already on ${botvc}`);
    if(membervc.id == botvc.id) return message.reply('We are already on the same voice channel.');
};
membervc.join();
}

The problem is that I have no idea how to make it so that if the last function gets an error or doesn’t work at all It can send an error message to the user like @User, I could not join the channel, "THE ERROR" :/ I don’t want my bot to crash and having to run it again just because of one small detail. Is there a way to fix it? It would help me a lot! Thanks in advance!

Advertisement

Answer

I would not let the user know about the error, because it might just confuse them, but you can do try catch to check if it goes through and if not message is sent.

try {
      membervc.join();
} catch(error){
      console.log(error);
      message.reply(`Something went wrong while joining voice channel`);
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement