不吵架会关闭我的不和谐机器人

时间:2020-09-13 20:53:49

标签: javascript node.js discord discord.js bots

如果我没有定义要禁止的人,它将关闭我的机器人。

错误:TypeError: Cannot read property 'displayName' of undefined

if (msg.member.roles.cache.has()) {
 let user = msg.mentions.members.first();
 msg.channel.send(`?│ Banned @${user.displayName}!`);
 user.ban();
}

1 个答案:

答案 0 :(得分:-1)

是的,如果您不提任何提示,那么它将产生一个错误,并且机器人将崩溃。 您应该在防止这种情况发生的地方做一个退货声明。

root@myserver:~# whereis openssl
openssl: /usr/bin/openssl /usr/include/openssl /usr/share/man/man1/openssl.1ssl.gz
                      

所以我解决了许多问题:

  • 首先,如果要使用if (msg.member.roles.cache.has('BAN_MEMBERS')) { let member = msg.mentions.members.first(); // prevents from not mentionning if (!member) return msg.channel.send(`Give a user to ban !`); // If the code continues, then there is a mention try { user.ban(); msg.channel.send(`?│ Banned @${member.user.displayName}!`); } catch (err) { msg.channel.send('I cannot ban this member !'); } } else msg.channel.send('You do not have the permission to do this'); 的名称,则必须使用用户对象。因此,您不能使用member而是member.displayName
  • 第二,如果机器人不能禁止该成员,则需要防止这种情况。例如,如果成员是管理员或服务器所有者
  • 最后,您要的是,如果消息中没有提及,它将发送一条消息,告诉您您没有通过成员禁止该行为

我建议您查看一些教程,例如discord.js guide

相关问题