网络挂钩问题和角色/权限想法

时间:2020-04-22 16:05:05

标签: discord discord.js

我用discord.js开发了一个使用msg.member.hasPermission("ADMINISTRATOR")msg.member.roles.cache.has(teacherRoleID)之类的机器人。一切正常,直到我尝试了webhooks。通过添加这两行:

client.on('ready', () => {
  client.user.setStatus("online")
  client.user.setActivity("!help", {
    type: "PLAYING",
  });
  superConsole(`Logged in as ${client.user.tag} in ${client.guilds.size} guilds!`);


    const hook = new Discord.WebhookClient("ID", "secret token"); // THESE
    hook.send("I am now alive!");                                 // LINES

  });

(顺便说一句 superConsole 是一个函数) 从那时起,该程序不再起作用,并始终返回相同的错误:(node:20736) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hasPermission' of null(node:20736) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of null

当我删除Webhook的这两行时,它再次起作用。为什么?我不明白。

权限和角色事物在消息侦听器中:

client.on('message', async msg => {
if (msg.member.hasPermission('ADMINISTRATOR') {
// some stuff here
}
if (msg.member.roles.cache.has(teacherRoleID) {
// some stuff here
}
});

1 个答案:

答案 0 :(得分:0)

问题在于,当您从hook发送消息时,它将触发客户端的message事件。由于Webhook不是公会成员,因此msg.member将从Webhook发送的消息中获得undefined

您将必须使用以下内容:

if (msg.member) {
  if (msg.member.hasPermission('ADMINISTRATOR') {
    // some stuff here
  }
  if (msg.member.roles.cache.has(teacherRoleID) {
  // some stuff here
  }
}
相关问题