WHITELIST_IDS = [123, 456]
class NotInWhiteList(commands.CheckFailure):
pass
def in_whitelist(whitelist):
async def inner_check(ctx):
if ctx.author.id not in whitelist:
raise NotInWhiteList("You're not on the whitelist!")
return True
return commands.check(inner_check)
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, NotInWhiteList):
await ctx.author.send(error)
@bot.command()
@in_whitelist(WHITELIST_IDS)
async def test(ctx):
await ctx.send("You do have permission")
更简单的方法是编写一个check来查看调用者是否可以调用命令,如果不能,则引发一个特殊的错误。然后可以在
on_command_error
中处理该错误,包括向用户发送警告。比如:要真正回答您的问题,您可以直接使用^{} 获取调用上下文。然后您可以自己检查
ctx.command
。(我现在使用的计算机没有安装discord.py
,因此这可能无法完美工作)您可以检查上下文是否使用
^{pr2}$ctx.valid
调用命令。如果True
,则表示它调用命令。否则就不会了相关问题 更多 >
编程相关推荐