简体   繁体   English

Discord 机器人无法断开与 discord.js 中的语音通道的连接

[英]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有人请帮助我TT

You have missed the last } .你错过了最后一个} Because of that you didn't close the if-statement of your leave command.因此,您没有关闭 leave 命令的 if 语句。 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.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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