discord.py rewrite 如何将函数名改写成命令?

时间:2021-06-30 20:13:25

标签: python discord discord.py

Discord.py 重写命令的示例函数如下:

bot = commands.Bot(command_prefix="$", help_command=None)
@bot.command(pass_context=True)
async def say(ctx, *, message):
    await ctx.send(message)

不和谐调用函数的方式是$say something。 Discord.py 如何知道函数 say 的名称以使其成为命令?

1 个答案:

答案 0 :(得分:1)

函数具有 __name__ 属性,该属性返回与函数名称相对应的字符串

>>> def foo():
...     print("Inside foo")
... 
>>> foo.__name__
'foo'

在装饰器中:

>>> def my_decorator(func):
...     def wrapper(*args, **kwargs):
...         print(f"The function name is: {func.__name__}")
...         return func(*args, **kwargs)
...     return wrapper
... 
>>> 
>>> @my_decorator
... def foo():
...      print("Inside foo")
... 
>>> foo()
'The function name is: foo'
'Inside foo'