【问题标题】:I have some errors on an discord.js bot im trying to make:我在尝试制作的 discord.js 机器人上有一些错误:
【发布时间】:2020-08-28 14:16:37
【问题描述】:

错误: TypeError:无法读取 null 的属性“id” 在 Object.execute (/Users/Desktop/MSBTM/commands/ban.js:18:32) 在客户端。 (/Users/Desktop/MSBTM/bot.js:56:36)


这是我的代码:

const client = new Discord.Client();

module.exports = {
    name: 'ban',
    description: "ban peoples ;D",
    execute(message, args) {
    if (!message.member.hasPermission("BAN_MEMBERS") || 
    !message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("You don't have 
    a permissions to do this, maybe later ;)");

    const user = message.mentions.users.first();
    const member = message.guild.member(user);

    const reason = args.slice(1).join(" ");

    if (!user) return message.channel.send("Please mention the user to make this action");

    if (user.id === message.author.id) return message.channel.send("You can't ban yourself, I tried :(");
    if (user.id === client.user.id) return message.channel.send("You can't ban me, I tried :(");

    if (!reason) reason = "No reason provided, please provide an reason to make this active";
      member.ban(reason).then(() => {
      message.channel.send(`Successfully banned **${user.tag}**`);
    }).catch(err => {
      message.reply("I was unable to ban the member :(");
    }) 
  } 
}

有人可以帮我吗?

【问题讨论】:

    标签: javascript visual-studio discord.js


    【解决方案1】:

    这是因为您创建了一个未登录的new 客户端对象。我建议您将客户端作为参数传递给函数或使用message.client 属性。在以下示例中,我将其用作函数参数:

    module.exports = {
        name: 'ban',
        description: "ban peoples ;D",
        execute(message, args, client) {
            if (!message.member.hasPermission("BAN_MEMBERS") ||
                !message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("You don't have a permissions to do this, maybe later ;) ");
    
            const user = message.mentions.users.first();
            const member = message.guild.member(user);
    
            const reason = args.slice(1).join(" ");
    
            if (!user) return message.channel.send("Please mention the user to make this action");
    
            if (user.id === message.author.id) return message.channel.send("You can't ban yourself, I tried :(");
            if (user.id === client.user.id) return message.channel.send("You can't ban me, I tried :(");
    
            if (!reason) reason = "No reason provided, please provide an reason to make this active";
            member.ban(reason).then(() => {
                message.channel.send(`Successfully banned **${user.tag}**`);
            }).catch(err => {
                message.reply("I was unable to ban the member :(");
            })
        }
    }
    

    你会像 execute(message, args, client) 这样调用这个函数。

    【讨论】:

    • 没问题!这就是这个网站的目的
    【解决方案2】:

    构造一个新的 Client 实例不会立即为您提供 Client.user。 当您登录 ClientUser 时,Client.user 会被填充:

    client.login('token');
    

    here了解更多信息

    【讨论】:

    • 这是我的机器人:drive.google.com/drive/folders/…
    • 人们应该小心这一点,因为如果这些命令没有精确分离,这可能会导致机器人监听命令的多个实例
    • 是的,接受的答案确实提供了更好的解决方案,但我将其保留在此处以供文档使用。依赖注入绝对是这里的必经之路。
    猜你喜欢
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 2019-09-10
    • 2021-06-27
    • 2020-05-27
    • 2012-10-07
    相关资源
    最近更新 更多