首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >module.export和获取用户信息

module.export和获取用户信息
EN

Stack Overflow用户
提问于 2019-04-14 18:19:20
回答 2查看 1.3K关注 0票数 0

我尝试通过module.export在命令文件中显示client.user.avatarURL,使用:

代码语言:javascript
复制
...
icon_url: client.user.avatarURL;
...

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

TypeError: Cannot read property 'avatarURL' undefined

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

main.js

代码语言:javascript
复制
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);

和命令文件:

代码语言:javascript
复制
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',
            } } });
    },
};
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55674166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档