Home > Software engineering >  How to make the bot knows if its in a voice channel or not?
How to make the bot knows if its in a voice channel or not?

Time:10-28

I'm kinda new to coding and i need help, When the bot is in a voice channel and i do the leave command, the bot will leave and reply with leave.result. But when i'm still in the voice channel and do the command, the bot will reply with the same answer, i want the bot to answer with leave.errorNotChannel here is the code

execute(message) {     
    const { channel } = message.member.voice;

    const serverQueue = message.client.queue.get(message.guild.id);
    if (!channel) return message.reply(i18n.__("leave.errorNotChannel")).catch(console.error);
    if (serverQueue && channel !== message.guild.me.voice.channel)
      return message
        .reply(i18n.__mf("leave.errorNotInSameChannel", { user: message.client.user }))
        .catch(console.error);
    
    channel.leave();
    message
    .channel
    .send(i18n.__("leave.result"))
    .catch(console.error);

     }

And i dont know how to make the bot knows if its in a voice channel or not.

CodePudding user response:

you can try something like this using an if statement:

    if (message.guild.me.voice.channel) {
            message.channel.send('I am in a voice channel)
} else {
message.channel.send('I\'m not in a voice channel')
}

What I've done here find the voice channel the bot is connected in (in the guild) the voice channel is defined as message.guild.me.voice.channel now if the channel exists you can send an output something like I did or if it isnt connected it'll just send an error

You can customise this further!

  • Related