我的命令处理程序一直运行良好,但是现在在比较行会ID时,它使我的机器人随机崩溃了。错误为Cannot read property 'id' of null
。我的代码是这样(重要的一点):
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '&';
const version = '1.1';
const fs = require('fs');
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.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'acqs'){
if ( message.guild.id === '541003397462097921') {
client.commands.get('heavenacqs').execute(message, args);
} else message.reply(`this command isn't supported in this server yet.`);
}
我做错了什么和/或如何解决?谢谢:)
答案 0 :(得分:1)
您的机器人最有可能在DM中接收消息,在这种情况下discord.js将提供null
作为message.guild
。即使您的漫游器没有缓存公会,它也会提供部分公会或抛出错误,而不是提供null
。
在尝试使用该消息之前,添加一条检查该消息是否已在行会中发送的消息。
if(message.guild && message.guild.id === '541003397462097921')
如果您永远不想回复DM中发送的任何消息,则只需在顶部添加支票即可。
if(!message.guild) return;