Big refactoring
- Changed event callback signature to `(ctx: EventContext, vars: ContextVars)` - Changed event filter signature to `(ctx: EventContext, vars: ContextVars)` - Partially fixed examples - Refactored `src/mab/__init__.py` to be more structured - Added support for local event context variables so that different filters don't mess with each other - Refactored file structure for context variables - Added support for combination of ContextVars - `ContextVarsPopulator` does not look builders dynamically anymore; `ContextVarsPopulator.autobuilder` is used to mark function as a builder that must be automatically called - `ContextVarsPopulator` does not required to be created anymore - Added context variable `CTX_COMMAND` - `MessageTypeFilter` supports message types passed as strings from now on - `MessageHasFile` renamed to `MessageHasFileFilter` - Added docs for context variables
This commit is contained in:
@@ -18,15 +18,15 @@ from mab import *
|
||||
|
||||
from _environment import check_environment
|
||||
|
||||
async def on_gen_command(data: EventContext) -> None:
|
||||
async def on_gen_command(ctx: EventContext, vars: ContextVars) -> None:
|
||||
"""This callback is called when `!gen R G B` command is received."""
|
||||
# convert R, G and B to floats
|
||||
try:
|
||||
r, g, b = [float(f) for f in data[CTX_MESSAGE].body.split()[1:]]
|
||||
r, g, b = [float(f) for f in vars[CTX_COMMAND].args]
|
||||
except:
|
||||
await data.bot.send.text(data.room, "Invalid arguments")
|
||||
await ctx.bot.send.text(ctx.room, "Invalid arguments")
|
||||
return
|
||||
await data.bot.send.text(data.room, "Generating the noise...")
|
||||
await ctx.bot.send.text(ctx.room, "Generating the noise...")
|
||||
# create the basic noise
|
||||
img = Image.new("RGB", (16, 16))
|
||||
for x in range(img.width):
|
||||
@@ -44,9 +44,9 @@ async def on_gen_command(data: EventContext) -> None:
|
||||
buf.seek(0)
|
||||
buf = buf.read()
|
||||
# send
|
||||
await data.bot.send.image_bytes(data.room, buf, "noise.png")
|
||||
await ctx.bot.send.image_bytes(ctx.room, buf, "noise.png")
|
||||
|
||||
async def on_image(ctx: EventContext) -> None:
|
||||
async def on_image(ctx: EventContext, vars: ContextVars) -> None:
|
||||
"""This callback is called when an image is received."""
|
||||
# download
|
||||
s = time.time()
|
||||
@@ -73,10 +73,10 @@ async def on_image(ctx: EventContext) -> None:
|
||||
text="Download and decryption took %.4f seconds" % took_time
|
||||
)
|
||||
|
||||
async def on_wrong_message(data: EventContext) -> None:
|
||||
async def on_wrong_message(ctx: EventContext, vars: ContextVars) -> None:
|
||||
"""This callback is called when a wrong message is received."""
|
||||
await data.bot.send.text(
|
||||
data.room,
|
||||
await ctx.bot.send.text(
|
||||
ctx.room,
|
||||
"Text me something like <code>!gen 0.1 0.7 1.0</code> or send an image to blur"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user