因此,我正在为我的客户开发Discord Bot,他希望我制作一个可以在其成员DM中发送事件的机器人。我已经编写了代码。 Bot执行该功能并将DM发送给所有成员。但是,由于出现“无法读取null的属性“第一个””错误,机器人停止了操作。
我尝试了很多事情,例如捕获控制台错误等。但是我仍然不明白为什么我的机器人在添加DM代码后会停止并写入该控制台错误。控制台在与“私人消息”命令没有实际连接的代码行中识别出某种类型的问题。它甚至不在同一个'bot.on'下...这是问题所在:
if (msg.guild && msg.content.startsWith('!private')) {
let text = msg.content.slice('!private'.length);
msg.guild.members.forEach(member => {
if (member.id != bot.user.id && !member.user.bot) member.send(text).catch(err => {
message.reply('I was unable to kick the member');
console.log(err);
});
})
}
});
**//This right here is a code that can execute the function i wanted to make.^**
bot.on('message', message=> {
const member = message.mentions.members.first(); // <-- **Console says this property'first' cannot be read (Cannot read property 'first' of null)**
let args = message.content.substring(PREFIX.length).split(" ");
const user = message.mentions.users.first();
switch(args[0]) {
case 'ping':
message.channel.sendMessage('pong!');
break;
//**Above is a part of the code that has a console error after I added the first code above. Before I added first code there was no such errors and everything worked perfectly.**
我希望Bot执行功能并在服务器私人消息(事件)中发送所有人,然后继续工作。
但是,它执行的功能是向所有人发送所需的消息,但是由于出现控制台错误,机器人停止了运行。