【问题标题】:Problem with webhooks and the role/permissions ideawebhook 和角色/权限概念的问题
【发布时间】:2021-10-16 20:04:41
【问题描述】:

我用 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
}
});

【问题讨论】:

  • 能否请您显示您使用msg.member.hasPermission("ADMINISTRATOR")msg.member.roles.cache.has(teacherRoleID) 的代码?
  • 您能否编辑您的帖子以包含该代码?这比尝试使用 cmets 中的代码更新您的问题更容易。
  • 我刚刚更新了它

标签: discord discord.js


【解决方案1】:

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

你必须使用这样的东西:

if (msg.member) {
  if (msg.member.permissions.has('ADMINISTRATOR') {
    // some stuff here
  }
  if (msg.member.roles.cache.has(teacherRoleID) {
  // some stuff here
  }
}

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 2019-05-10
    • 2012-03-17
    • 2011-07-26
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多