【问题标题】:Checking if someone has a role with Discord.js检查某人是否具有 Discord.js 的角色
【发布时间】:2020-09-21 21:17:24
【问题描述】:

我正在尝试使用 discord.js 制作一个不和谐机器人,但我遇到了这个错误。 我知道还有其他关于此的文章,但我无法让它发挥作用。 在运行命令之前,我需要检查用户是否具有 admin/mod 角色。

        case 'clear':
        if(!author.roles.cache.has('686001018277855308'))  return msg.reply('you dont have the permissions to do this.');
        else if(author.roles.cache.has('686001018277855308')){

            if(!args[1]) return msg.reply('please add the number of messages to clear in the command');
            else{
                msg.channel.bulkDelete(args[1]);
                msg.reply(args[1] + ' messages cleared!');
            }
        }

    break;

我就是无法让它工作。我收到以下错误:

C:\Program Files\nodejs\node.exe index.js froggo 上线啦! c:\Users\frenc\OneDrive\Documents\GitHub\froggobot\index.js:53 if(!author.roles.cache.has('686001018277855308')) return msg.reply('你没有权限这样做'); ^

TypeError: 无法读取未定义的属性“缓存”

如果您需要任何其他信息,请在 cmets 中询问。

还有一个问题,是否可以将此过程保存为单个命令并在其他文件中使用它而无需重新键入整个内容?

【问题讨论】:

  • 在您的情况下,author.roles 未定义。对最后一个问题的回答:是的,这个过程需要保存在其他的地方使用。
  • 如果if(author.roles && ......
  • 我看不到整个评论:(你能把它作为答案发布吗?

标签: javascript node.js discord discord.js


【解决方案1】:

我的建议是,您只需尝试检查用户是否可以通过权限检查删除其他人发送的消息。

这是我要做的:

case 'clear':
    if(!msg.member.permissions.has('MANAGE_MESSAGES')) return msg.reply('you dont have the permissions to do this.');
    if(!args[1]) return msg.reply('please add the number of messages to clear in the command');

    msg.channel.bulkDelete(args[1]);
    msg.reply(args[1] + ' messages cleared!');
break;

【讨论】:

  • 天!有效!我最终让它检查主 bulkdelete 命令中的权限,如果这不起作用,则表示他没有权限。再次感谢:)
猜你喜欢
  • 2021-07-07
  • 2021-04-06
  • 2018-01-01
  • 2021-02-24
  • 2021-10-25
  • 2021-11-15
  • 2020-09-14
  • 2022-01-19
  • 2023-03-03
相关资源
最近更新 更多