【问题标题】:How to make someone need a role to use a command?如何使某人需要角色才能使用命令?
【发布时间】:2018-02-28 11:21:24
【问题描述】:

我正在制作一个不和谐的机器人,我想知道如何让某人需要一个必需的角色才能使用命令。

@bot.command(pass_context = True)
@commands.cooldown(1, 30, commands.BucketType.user)
async def test(ctx):
if commands.has_role("| Premium |"):
    msg = ["test", "test2"]
    await bot.send_message(ctx.message.author,                                              
    random.choice(msg))

请告诉我执行此操作的正确方法,因为 if commands.has_role("| Premium |"): 不正确

【问题讨论】:

  • 呃...这个确切的问题不是因为今天早上太宽泛而被标记了吗?在问题关闭时删除并重新发布您的问题只会使您的帐户被标记为垃圾邮件发送者并被阻止提问。
  • @squaswin 我标记了我自己的帖子 lmao
  • @squaswin 我改变了它并添加了示例程序......所以它没有那么广泛

标签: python discord discord.py


【解决方案1】:

要根据用户角色的名称添加检查,请使用 commands.has_role()commands.has_any_role() 装饰器。

@bot.command()
@commands.has_any_role("Premium")
async def test():
    pass

【讨论】:

  • 在我创建了我希望他们能够执行的操作后,如果他们有权限,我将如何添加错误消息
  • 你的作品完美,但我需要知道如何为它制作错误消息@squaswin
  • 我不是 ext 用户,但您似乎可以为 on_command_error 创建一个事件并检查 CheckFailure 的实例
  • 我不知道,因为没有任何地方可以明确地告诉你如何设置检查失败的事件@squaswin
  • 如何为检查失败创建 on_error 事件
猜你喜欢
  • 2021-01-19
  • 2021-03-16
  • 1970-01-01
  • 2018-06-29
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
相关资源
最近更新 更多