Node.js Discord Bot TypeError:无法读取未定义的“语音”属性

时间:2020-10-17 23:04:03

标签: node.js discord discord.js bots

这是命令的整个代码,问题是当我执行-move每个人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}`)
  }
}

2 个答案:

答案 0 :(得分:0)

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);

答案 1 :(得分:0)

如果mem为空,则

undefined可能为message.mentions.members。使用之前,应检查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}`)
    }
  }
}