Discord.js禁止命令问题

时间:2020-03-03 20:15:16

标签: javascript discord.js

我正在尝试解决我今天遇到的错误,错误是这样的:

C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ commands \ moderation \ ban.js:15 让{用户} = message.mentions.members.first()|| message.guild.members.get(args [0]); ^

TypeError:无法解构以下属性的“用户” '(message.mentions.members.first(...)|| message.guild.members.get(...)),因为它是未定义的。 在Object.run(C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ commands \ moderation \ ban.js:15:11) 在客户处。 (C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ index.js:36:34) 在Client.emit(events.js:323:22) 在MessageCreateHandler.handle(C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ node_modules \ discord.js \ src \ client \ websocket \ packets \ handlers \ MessageCreate.js:9:34) 在WebSocketPacketManager.handle(C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ node_modules \ discord.js \ src \ client \ websocket \ packets \ WebSocketPacketManager.js:108:65) 在WebSocketConnection.onPacket(C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketConnection.js:336:35) 在WebSocketConnection.onMessage(C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketConnection.js:299:17) 在WebSocket.onMessage(C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ node_modules \ ws \ lib \ event-target.js:120:16) 在WebSocket.emit(events.js:311:20) 在Receiver.receiverOnMessage(C:\ Users \ nalfo \ OneDrive \ Desktop \ Discord Bot \ NetSync \ node_modules \ ws \ lib \ websocket.js:789:20)

之前,它工作得很好,不知道出了什么问题。这是我的模块导出。

module.exports = {
    name: "ban",
    category: "moderation",
    description: "Bans the mentioned user.",
    usage: "<imputs>",
    run: (client, message, args) => {
    let { user } = message.mentions.members.first() || message.guild.members.get(args[0]);

    if(!message.member.hasPermission("BAN_MEMBERS")){
        message.channel.send(permission);
    } else {
        if(!user)
            return message.channel.send(novalidmember);
        if(!user.bannable) 
            return message.channel.send("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");

        let reason = args.slice(1).join(' ');
        if(!reason) reason = "No reason provided";

        user.ban({
            reason: `${reason}`
        })

        const embed = new Discord.RichEmbed()
         The embed is hidden.

        let logchannel = message.guild.channels.find(x => x.name === `logs`);


        if (!logchannel){
            Sends a message to the channel.
            Logs the ban in a file.
        } else {
            Sends a message to the channel.
            Logs ban in the log channel.
          }


    }
}
}

我隐藏的一些代码并不重要。但是我仍然不确定整个命令有什么问题。

1 个答案:

答案 0 :(得分:1)

这里的错误是因为您在下面的行中无法"destructure" message.mentions.members.first() || message.guild.members.get(args[0])的结果,因为结果是undefined

let { user } = message.mentions.members.first() || message.guild.members.get(args[0]);

message.mentions.members.first()message.guild.members.get(args[0])的结果是否可能是undefined?我看不到您尝试从返回的对象中获取用户密钥之前先检查undefined

这是我要做的,而不是在分配后检查user中的伪造值:replace

if (!user)
    return message.channel.send(novalidmember);

在文件中进一步存储一些内容(在分配user之前),如下所示:

if (message.mentions.members.first() == undefined || message.guild.members.get(args[0]) == undefined)
{
    return message.channel.send(novalidmember);
}