简体   繁体   English

我在尝试制作的 discord.js bot 上有一些错误:

[英]I have some errors on an discord.js bot im trying to make:

error: TypeError: Cannot read property 'id' of null at Object.execute (/Users/Desktop/MSBTM/commands/ban.js:18:32) at Client.错误:TypeError:无法在客户端读取 Object.execute (/Users/Desktop/MSBTM/commands/ban.js:18:32) 的 null 属性“id”。 (/Users/Desktop/MSBTM/bot.js:56:36) (/用户/桌面/MSBTM/bot.js:56:36)


And here is my the code:这是我的代码:

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 :(");
    }) 
  } 
}

can someone help me?有人能帮我吗?

This is because you create a new client object, which isn't logged in. I suggest you pass the client as a parameter to the function or use the message.client property.这是因为您创建了一个未登录的new客户端对象。我建议您将客户端作为参数传递给函数或使用message.client属性。 In the following example I used it as a function parameter:在以下示例中,我将其用作函数参数:

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 :(");
        })
    }
}

You'd call this function like execute(message, args, client) then.然后你会像execute(message, args, client)一样调用这个函数。

Constructing a new Client instance does not provide you a Client.user right away.构造一个新的 Client 实例不会立即为您提供 Client.user。 Client.user gets populated when you login the ClientUser:当您登录 ClientUser 时,Client.user 会被填充:

client.login('token');

Read more about it from here这里阅读更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用 discord.js 制作一个 discord 机器人,我正在制作一个偶数处理程序,但我收到此错误 - i am trying to make a discord bot using discord.js and im making an even handler but im getting this error 我正在尝试打开我的 discord 机器人。 但它向我展示了这条信息。 我安装了 discord.js - I am trying to open my discord bot . But it shows me this message . I have discord.js installed 如何让 discord 机器人在 discord.js 中发送消息后有表情符号反应? - How do I make discord bot have an emoji reaction after it sends its message in discord.js? 如何使我的 discord 机器人具有自定义状态 (discord.js) - How do I make my discord bot have a custom status (discord.js) 试图为我的 discord.js 机器人创建一个警告系统。 我做错了什么? - Trying to create a warning system for my discord.js bot. What have I done wrong? Discord.js:我怎样才能让我的机器人打字几秒钟? - Discord.js: How can i make my bot typing for some seconds? 我正在尝试为我的机器人在 discord.js 中创建一个自动角色 function 但它不起作用 - I'm trying to make an autorole function in discord.js for my bot but it doesn't work Discord.js 音乐机器人出错 - Discord.js music bot getting errors 我将如何使用 Discord.js 制作一个“主持活动”的机器人? - How would I make a bot that "host events" with Discord.js? 我想让我的 discord.js 机器人不可见 - I'm looking to make my discord.js bot invisible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM