【发布时间】:2021-04-21 15:11:31
【问题描述】:
我尝试制作一个命令,可以用来向我显示机器人所在的服务器,但是当我开始制作命令时,它并不能以任何方式工作,我尝试它并且总是告诉我其中一件事是未定义或无法读取属性,它主要告诉我机器人或公会未定义或无法正确读取。
我想发出一个只有我可以在不和谐中使用的命令来查看我的机器人所在的所有服务器。
我使用命令处理程序,这意味着每个命令都在单独的 js 文件中。
我的 discord 客户端变量设置为“bot”(如代码所示)。
这是命令(servers.js):
module.exports = {
name: 'servers',
execute(bot, message, args) {
bot.guilds.cache.forEach(guild => {
message.channel.send(`Servers: ${guild.name}`);
})
}
}
这是命令处理程序/主文件(index.js):
const Discord = require('discord.js');
const bot = new Discord.Client();
const fs = require('fs');
const { prefix, token } = require('./config.json');
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command);
}
const cooldowns = new Discord.Collection();
bot.once('ready', () => {
console.log('Online!');
});
bot.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
if (!bot.commands.has(commandName)) return;
const command = bot.commands.get(commandName);
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
try {
command.execute(message, args);
}
catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!').then(msg => {
msg.delete({timeout:6000});
});
}
});
还有错误:
TypeError: Cannot read property 'cache' of undefined
at Object.execute (D:\Users\USER\Desktop\Bot\commands\serversinfo.js:4:20)
at Client.<anonymous> (D:\Users\USER\Desktop\Bot\index.js:64:21)
at Client.emit (node:events:376:20)
at MessageCreateAction.handle (D:\Users\USER\Desktop\Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (D:\Users\USER\Desktop\Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
【问题讨论】:
标签: discord discord.js