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
的名称以使其成为命令?
答案 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'