我正在尝试为 discord bot (js) 制作离开/断开 VC 命令,我面临以下讨论的一些问题

时间:2021-03-30 06:15:26

标签: javascript node.js discord discord.js bots

代码无法完美运行,我发现了几个问题:

  1. 当我连接到 VC 并尝试一次尝试命令 bot leaves the VCtriggers two functions 时,即,General leave funtion 和 < em>作者不在同一频道。
  2. 当我发出命令 being outside the VC(bot 连接到 VC)时,bot 以函数响应我们不在同一个频道功能中。,而是我想要 it to respond 您没有连接到 VC
  3. 当我在 being outside VCbot not connected to VC 期间发出命令时,它什么都不响应,at console 我收到错误:

(node:37) UnhandledPromiseRejectionWarning: TypeError: cannot read property "id" of undefined

代码:

client.on('message', async (message) => {
  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const command = args.shift().toLowerCase();
  if (command === 'leave') {
    let authorVoice = message.member.voice;

   
    const embed1 = new discord.MessageEmbed()
      .setTitle('?`You are not in a Voice Channel!`')
      .setColor('RED')
      .setTimestamp();
    if (!authorVoice.channel) return message.channel.send(embed1);

    const embed2 = new discord.MessageEmbed()
      .setTitle('⛔`We are not in a same Voice Channel`')
      .setColor('YELLOW')
      .setTimestamp();
    if (authorVoice.channel.id !== client.voice.channel.id)
      return message.channel.send(embed2);
   const embed = new discord.MessageEmbed()
      .setTitle('`Successfully left the Voice Channel`✔')
      .setColor('RED')
      .setTimestamp();
    authorVoice.channel.leave()
    message.channel.send(embed);
  }
});

有人可以帮我解决吗?

2 个答案:

答案 0 :(得分:0)

client.voice 没有 channel 属性。这意味着 client.voice.channel 未定义。当您尝试读取其中的 id 时,您会收到错误消息,您“无法读取未定义的属性“id””

您可能想要 connections,它是 VoiceConnections 的集合。连接确实具有 channel 属性,但由于 client.voice.connections 是一个集合,因此您需要先找到您需要的那个。

由于您只想检查是否存在具有相同 ID 的频道,因此您可以使用 .some() 方法。在其回调函数中,您检查是否有任何连接具有与消息作者的频道 ID 相同的频道 ID。因此,您可以创建一个返回布尔值的变量,而不是 if(authorVoice.channel.id !== client.voice.channel.id)。如果机器人在同一频道中,则返回 true,否则返回 false

const botIsInSameChannel = client.voice.connections.some(
  (c) => c.channel.id === authorVoice.channel.id,
);

// bot is not in the same channel
if (!botIsInSameChannel)
  return message.channel.send(embed2);

答案 1 :(得分:0)

如果有人关注,这里是更新的代码:

client.on('message', async message => {
    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();
    if (command === 'leave') {
             let authorVoice = message.member.voice; 
        const botIsInSameChannel = client.voice.connections.some(
  (c) => c.channel.id === authorVoice.channel.id,
); 
     const embed1 = new discord.MessageEmbed()
      .setTitle(':no_entry_sign: You are not in a Voice Channel!')
      .setColor('RED')
      .setTimestamp();
        if(!authorVoice.channel) return message.channel.send(embed1);
        
    const embed2 = new discord.MessageEmbed()
      .setTitle(':no_entry: We are not in a same Voice Channel')
      .setColor('YELLOW')
      .setTimestamp();
        if (!botIsInSameChannel) return message.channel.send(embed2)      
        
        const embed = new discord.MessageEmbed()
      .setTitle('Successfully left the Voice Channel✔')
      .setColor('RED')
      .setTimestamp();
        authorVoice.channel.leave();
        message.channel.send(embed);
      }
    });