我正在尝试使用以下命令通过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',
} } });
},
};
答案 0 :(得分:0)
您正在做:command.execute(message, args, client);
,但随后execute(message, client) {
意味着在您的命令文件client
中实际上是数组args
。
您需要执行以下操作:execute(message, args, client) {
答案 1 :(得分:0)
您需要改用module.exports = (client, message) => {
。