Python不和.py检测消息是否调用任何命令

2024-03-28 07:57:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道消息是否可以调用任何命令,而不执行它。在

我的意思是,我有一条消息,我想知道这个消息是否触发了任何命令。有什么我没注意到的吗?类似于ctx.command的内容,它告诉我在不运行消息的情况下可以执行什么命令。在

这是为了在bot没有发送权限的情况下对用户进行perm检查和DM。谢谢!在


Tags: 用户命令消息权限内容bot情况dm
1条回答
网友
1楼 · 发布于 2024-03-28 07:57:52

更简单的方法是编写一个check来查看调用者是否可以调用命令,如果不能,则引发一个特殊的错误。然后可以在on_command_error中处理该错误,包括向用户发送警告。比如:

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")

要真正回答您的问题,您可以直接使用^{}获取调用上下文。然后您可以自己检查ctx.command。(我现在使用的计算机没有安装discord.py,因此这可能无法完美工作)

您可以检查上下文是否使用ctx.valid调用命令。如果True,则表示它调用命令。否则就不会了

^{pr2}$

相关问题 更多 >