node.js - 类型错误 : Cannot read property 'avatarURL' of null | Trying to export the help command

标签 node.js discord.js

我一直在清理我的index.js 文件,并决定将帮助命令放在更实用的地方。唯一的问题:在页脚中我有机器人头像,但它出现了 TypeError: Cannot read property 'avatarURL' of null

//code in help.js
const Discord = require('discord.js');
const client = new Discord.Client();
var version = process.env.VERSION;

module.exports = {
    name: 'help',
    execute(message, args){
        const Discord = require('discord.js');

        const helpcommands = new Discord.RichEmbed()
        .setTitle('Akasuki Command List')
        .addField('Info Commands', 'userinfo, version, ping, invite, avatar')
        .addField('Moderation Commands', 'kick, ban, clear')
        .addField('Fun Commands', 'samwise, say')
        .setColor(0xFF8AFF)
        .setFooter(`Akasuki ${version}`, client.user.avatarURL)
        message.channel.send(helpcommands);
    }
}
//code in index.js
if (command === 'help') {
    client.commands.get('help').execute(message, args);
}

我还想知道是否有办法一次导出多个命令,或者是否必须逐一完成?

如果有人可以帮助我解决此错误,我将非常感激<3感谢您查看此内容。

最佳答案

I've been cleaning my index.js file, and decided I wanted to have the help command somewhere a little more practical. Only problem: in the footer I have the bots avatar, but it comes up with TypeError: Cannot read property 'avatarURL' of null

错误TypeError: Cannot read property 'avatarURL' of null 很清楚。这表示您正在尝试访问对象的 null 属性。

要调试此错误,您可以尝试记录有问题的对象:console.log(client.user);如果出现 null,这可能是由于未使用 Discord.js 客户端登录。

根据您的代码,您似乎尚未登录 Discord.js 的客户端实例。我建议您查看login方法,这是您想要用来登录 Discord 的方法。在访问客户的用户属性之前,您需要先登录 Discord。

此外,要查看或创建您的机器人应用程序,visit your Discord applications .

I'm also wondering if there is a way to export multiple commands at once, or if all have to be done one-by-one?

导出乘法命令的一个简单方法是使用数组。您可以定义并导出多个命令,如下所示:

module.exports = [
    {
        // command 1 ...
    },
    {
        // command 2 ...
    ]
];

然后你可以使用循环来访问它们。

关于node.js - 类型错误 : Cannot read property 'avatarURL' of null | Trying to export the help command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58888042/

相关文章:

javascript - PhantomJS 崩溃 - 退出代码 126

node.js - MongoDB 初始连接慢

node.js - 错误 : EPERM, Node.js 和 Etherpad Lite 不允许进行操作

javascript - 即使机器人是管理员,Discord JS 管理角色权限也会丢失

mysql - 防止不和谐机器人崩溃(mysql)

javascript - Discord.js 机器人在 channel 中发送 10 条消息后发送消息

javascript - 仅使用单词从数组中删除项目

javascript - HTML5 + discord.js 事件监听器

javascript - 使用nodejs将json对象附加到文件中

javascript - 如何在命令行中在nodejs中提问