运行任意命令调用时的另一个函数discord.py

Run Another Function When Any Command Is Called discord.py

每当我的 discord 机器人命令被调用时,我都需要 运行 一个常用函数!就像公共函数应该先执行然后它应该 运行 命令函数
示例:

async def common():
    #some code

@client.command()
async def something(ctx):
    #command code

所以,当用户在 discord 中像 '!something' 一样调用 something() 函数时,common() 函数应该首先执行,然后它应该执行 something() 功能,在我的机器人中我有大量命令,所以我无法在每个命令功能之上键入 common()
提前致谢!

每次执行命令时都会触发一个事件:on_command

因此,如果您想 运行 调用任何命令时的常用函数,您可以创建一个 on_command 事件:

async def on_command(ctx):
    common() # execute common function

尝试在常用函数上使用 before_invoke 装饰器。您可以阅读更多内容 here.

@client.before_invoke
async def common(message):
    await message.channel.send("This is the common function")


@client.command()
async def test(ctx):
    await ctx.send("This is the test command")