简体   繁体   中英

The Discord Bot Can't Disconnect To The Voice Channel in discord.js

Code:

const guild = message.guild;
let voiceId = message.content.split(" ").slice(1).join("");
let voiceChannelId = client.channels.cache.get(voiceId);
if(message.content.startsWith("$join")){
    let msgLength = message.content.length;
    if(msgLength > 5){
        voiceChannelId.join().then(connection=>{message.channel.send("Successfully Connected!")
        }).catch(err=>{console.error(err)});
    } else if(msgLength <= 5)return message.channel.send("Invalid channel ID. Pls try again!")};
let voiceChannel = message.member.voiceChannel;
if(message.content === "$leave"){
    // console.log(voiceChannel);
    if(voiceChannel){
        voiceChannel.leave();
    } else return message.channel.send("I'm not connected to any voice channel.");

The bot still stay in the voice channel when I use leave command. Somebody pls help me TT

You have missed the last } . Because of that you didn't close the if-statement of your leave command. It was just a little syntax error.

const guild = message.guild;
let voiceId = message.content.split(" ").slice(1).join("");
let voiceChannelId = client.channels.cache.get(voiceId);
if (message.content.startsWith("$join")) {
  let msgLength = message.content.length;
  if (msgLength > 5) {
    voiceChannelId.join().then(connection => {
      message.channel.send("Successfully Connected!")
    }).catch(err => { console.error(err) });
  } else if (msgLength <= 5) return message.channel.send("Invalid channel ID. Pls try again!");
}
let voiceChannel = message.member.voiceChannel;
if (message.content === "$leave") {
  // console.log(voiceChannel);
  if (voiceChannel) {
    voiceChannel.leave();
  } else return message.channel.send("I'm not connected to any voice channel.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM