module.export并获取用户信息

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (49)

我正试图client.user.avatarURL通过module.export使用以下命令显示在命令文件中:

...
icon_url: client.user.avatarURL;
...

但是当我通过机器人调用命令时,我在控制台中出现此错误:

TypeError: Cannot read property 'avatarURL' undefined

我已经尝试在const上获取客户端的值并将其传递给命令处理程序,但也没有解决它。如果你删除该行一切正常,所以我猜这只是传递客户信息的错误方法。

main.js

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
client.commands = new Discord.Collection();


const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Online!');
});

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const commandName = args.shift().toLowerCase();

    if (!client.commands.has(commandName)) return;
    const command = client.commands.get(commandName);

    if (command.args && !args.length) {
        let reply = `You didn't provide enough arguments, ${message.author}!`;

        if (command.usage) {
            reply += `\nThe proper usage would be: \n\`${prefix}${command.name} ${command.usage}\``;
        }

        return message.channel.send(reply);
    }


    try {
        command.execute(message, args, client);
    }
    catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

client.login(token);

和命令文件:

module.exports = {
    name: 'help',
    description: 'Help file',
    execute(message, client) {
        message.channel.send({ embed: {
            color: 0xf7da66,
            author: {
                name: 'Bot',
                icon_url: client.user.avatarURL,
            },
            title: 'commands guide',
            description: 'This is an discord bot.',
            fields: [{
                name: 'Command',
                value: 'Type: example.',
            },
            ],
            timestamp: new Date(),
            footer: {
                icon_url: client.user.avatarURL,
                text: '© Bot',
            } } });
    },
};
提问于
用户回答回答于

你需要改用module.exports = (client, message) => {

用户回答回答于

你正在执行:command.execute(message, args, client);但是execute(message, client) {意味着你的命令文件client现在实际上是数组args

你需要这样做: execute(message, args, client) {

扫码关注云+社区

领取腾讯云代金券