【问题标题】:Admins only command仅管理员命令
【发布时间】:2018-11-13 01:03:39
【问题描述】:

只有选定的用户 ID 才应有权使用此命令。如下所示,只有添加到此列表中的用户 ID 才能获得使用该命令的权限。

def is_any_user(ids):
    async def predicate(ctx):
        return ctx.author.id in ids
    return commands.check(predicate)

LIST_OF_ADMINS = [3557657657647676, 36567565756766767, 343657687786435432]

@bot.command(pass_context=True)
@is_any_user(LIST_OF_ADMINS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

【问题讨论】:

  • 试试if ctx.(some_id) in LIST_OF_ADMINS?或者为这些人创建一个新角色
  • @cricket_007 我的机器人安装在许多服务器上,因此它们可以创建角色。所以我需要通过一些使用的 ids 列表来限制一些命令。

标签: python-3.x discord.py


【解决方案1】:

您可以编写自己的检查来装饰命令

def is_any_user(ids):
    def predicate(ctx):
        return ctx.message.author.id in ids
    return commands.check(predicate)

LIST_OF_ADMINS = ['3557657657647676', '36567565756766767', '343657687786435432']

@bot.command(pass_context=True)
@is_any_user(LIST_OF_ADMINS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

您可以在commands 扩展in the documentation 中阅读有关检查的更多信息

【讨论】:

  • @Bosen 哎呀,我忘了添加commands.check。现在应该修好了。
  • @Bosen 看起来在异步分支中不希望谓词成为协程。
  • 现在它不会回复任何在使用命令时出错的人。 return all(predicate(context) for predicate in predicates) File "E:\sample.py", line 17, in predicate return ctx.author.id in ids AttributeError: 'Context' object has no attribute 'author'
  • @Bosen 是的,messages 有作者,而不是上下文。您的 lsit 管理员也应该是字符串列表,而不是整数。
猜你喜欢
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多