CallBack for commands¶
CallBack for each commands can be of types
Functions
Generators
Asynchronous Functions
Asynchronous Generators
Function type¶
Functions can’t be a better since we can’t call asynchronous functions (including msg.channel.send) from non asynchronous functions
so we can reply only once.
Example:
@key(re.compile(r"^!repeat (\d*) (.*)$"))
def repeat(msg, times, string):
times = int(times)
return '\n'.join([string]*times)
Generator type¶
With Generators we can reply multiple times even though it is non asynchronous.
Example:
@key(re.compile(r"^!repeat (\d*) (.*)$"))
def repeat(msg, times, string):
times = int(times)
for i in range(times):
yield string
Asynchronous Function type¶
Asynchronous Functions is recommended since we can call both asynchronous functions and non asynchronous functions. Also most of the discord.py methods are asynchronous.
Example:
@key(re.compile(r"^!repeat (\d*) (.*)$"))
async def repeat(msg, times, string):
times = int(times)
for i in range(times):
await msg.channel.send(string)
Asynchronous Generator type¶
Asynchronous Generators has a benefit of reply multiple times. (Asynchronous Functions also can using msg.channel.send)
Example:
@key(re.compile(r"^!repeat (\d*) (.*)$"))
async def repeat(msg, times, string):
times = int(times)
for i in range(times):
yield string
Updating callback at runtime¶
It supports updating callbacks at runtime, since it is literally changing value of key(s) in python dictionary.
@key(re.compile(r"^repeat (.*)$"), re.compile(r"^twice (.*)$"))
def twice(msg, string):
return string * 2
@key("upgrade-to-thrice")
def upgrade(msg):
thrice = lambda msg, x: x * 3
key(re.compile(r"^repeat (.*)$"))(thrice)
return "repeat is upgrade to thrice the string"
"""
'repeat hello' -> "hellohello"
'twice world' -> "worldworld"
'upgrade-to-thrice' -> "repeat is upgrade to thrice the string"
'repeat hello' -> "hellohellohello"
'twice world' -> "worldworld"
"""