【问题标题】:Node.js Discord Bot TypeError: Cannot read property of 'voice' of undefinedNode.js Discord Bot TypeError:无法读取未定义的“语音”属性
【发布时间】:2021-02-01 02:21:40
【问题描述】:

这是我的整个命令代码,问题是当我这样做时 - 移动所有人 123132123123(例如频道 ID)。该命令完美运行,但给我一个错误(node:317) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined。我一直在尝试.catch() 错误,但它仍然给我这个错误。每当我做return member.voice.setChannel(`${move}`);。错误停止了,但也只是出于某种原因让我感动......

if (command === 'move') {
  if (!message.member.permissions.has("MOVE_MEMBERS")) return message.channel.send(':x: **You do not have the permission to use this command!**');
  const args = message.content.slice(prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();
  const mem = message.mentions.members.first()
  let move = args[1]; // Remember arrays are 0-based!.
  let move2 = args[2];
  let idcheckchannel1 = client.channels.cache.get(move)
  let idcheckchannel2 = client.channels.cache.get(move2)
  if (!args[0]) return message.channel.send('Please mention user and voice channel ID/IDs')
  if (!args[1]) return message.channel.send('Please mention voice channel ID/IDs')

  if(args[0] === 'everyone' && !move2) {
    if (!idcheckchannel1) return message.channel.send('Please use a valid voice channel ID')
    let channel = message.guild.channels.cache.get(message.member.voice.channel.id);
    for (const [memberID, member] of channel.members)
      member.voice.setChannel(`${move}`);
  }

  if (!mem.voice.channel) return message.channel.send('User is not in voice channel')

  if (!move2) {
    if (!idcheckchannel1) return message.channel.send('Please use a valid voice channel ID')
    mem.voice.setChannel(`${move}`)
  } else {
    if (!idcheckchannel1) return message.channel.send('Please use a valid voice channel ID')
    if (!idcheckchannel2) return message.channel.send('Please use a valid voice channel ID')
    mem.voice.setChannel(`${move}`)
    mem.voice.setChannel(`${move2}`)
  }
}

【问题讨论】:

    标签: node.js discord discord.js bots


    【解决方案1】:

    如果message.mentions.members 为空,则mem 可能是undefined。在使用mem之前,您应该检查它是否未定义。

    试试这个:

    if (command === 'move') {
      if (!message.member.permissions.has("MOVE_MEMBERS")) return message.channel.send(':x: **You do not have the permission to use this command!**');
      const args = message.content.slice(prefix.length).trim().split(/ +/g);
      const command = args.shift().toLowerCase();
      const mem = message.mentions.members.first()
      let move = args[1]; // Remember arrays are 0-based!.
      let move2 = args[2];
      let idcheckchannel1 = client.channels.cache.get(move)
      let idcheckchannel2 = client.channels.cache.get(move2)
      if (!args[0]) return message.channel.send('Please mention user and voice channel ID/IDs')
      if (!args[1]) return message.channel.send('Please mention voice channel ID/IDs')
    
      if(args[0] === 'everyone' && !move2) {
        if (!idcheckchannel1) return message.channel.send('Please use a valid voice channel ID')
        let channel = message.guild.channels.cache.get(message.member.voice.channel.id);
        for (const [memberID, member] of channel.members)
          member.voice.setChannel(`${move}`);
      }
    
      if (mem != null) { // << ensure that mem is not undefined
        if (!mem.voice.channel) return message.channel.send('User is not in voice channel')
    
        if (!move2) {
          if (!idcheckchannel1) return message.channel.send('Please use a valid voice channel ID')
          mem.voice.setChannel(`${move}`)
        } else {
          if (!idcheckchannel1) return message.channel.send('Please use a valid voice channel ID')
          if (!idcheckchannel2) return message.channel.send('Please use a valid voice channel ID')
          mem.voice.setChannel(`${move}`)
          mem.voice.setChannel(`${move2}`)
        }
      }
    }
    

    【讨论】:

    • 我首先收到了SyntaxError: Unexpected token ')' 的错误,然后我通过在所有人行的末尾添加了一个额外的} 来修复它。然后现在它给了我这个(node:134) UnhandledPromiseRejectionWarning: ReferenceError: mem is not defined 的错误,这很奇怪,因为if (mem != null) 不应该阻止什么?
    • 也许你在错误的地方添加了}。我已经编辑了我的答案以完成代码。它现在应该可以工作了。
    • 谢谢!有效。我确实在错误的地方添加了}
    【解决方案2】:

    channel.members 返回Collection,而不是Array,因此数组解构将不起作用。即使是这样,每个元素的第一个属性也不是memberIDmember

    此外,您创建了一个新的 channel 变量,即使您已经拥有通道对象。

    // let channel = message.guild.channels.cache.get(message.member.voice.channel.id);
    // for (const [memberID, member] of channel.members)
    //  member.voice.setChannel(`${move}`);
    // };
    
    for (const member of message.member.voice.channel.members) 
     member.voice.setChannel(move);
    

    【讨论】:

    • 我已将代码更改为if(args[0] === 'everyone' &amp;&amp; !move2) { if (!idcheckchannel1) return message.channel.send('Please use a valid voice channel ID') for (const member of message.member.voice.channel.members) member.voice.setChannel(move); },但现在显示为(node:347) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'setChannel' of undefined。编辑对不起它很乱我不知道该怎么做
    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 2021-03-31
    • 2020-10-15
    • 2021-09-14
    • 2020-06-20
    • 2020-05-15
    • 2021-01-01
    • 2021-03-29
    相关资源
    最近更新 更多