这是命令的整个代码,问题是当我执行-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}`)
}
}
答案 0 :(得分:0)
channel.members
返回一个Collection
,而不是一个Array
,因此数组解构将无法进行。即使这样做,每个元素的第一个属性也不是memberID
和member
。
此外,即使您已经拥有频道对象,您也创建了一个新的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}`)
}
}
}