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:
2026-10-31 20:37:57 +03:00
parent eacb0c0d25
commit d5367ebc6b
18 changed files with 329 additions and 272 deletions

View File

@@ -16,7 +16,7 @@ from mab import *
from _environment import check_environment
async def on_help_command(ctx: EventContext) -> None:
async def on_help_command(ctx: EventContext, vars: ContextVars) -> None:
"""!help"""
HELP_MESSAGE = (
"<strong>Here is the list of the commands:</strong><br>"
@@ -31,14 +31,14 @@ async def on_help_command(ctx: EventContext) -> None:
)
await ctx.bot.send.text(ctx.room, HELP_MESSAGE)
async def on_time_command(ctx: EventContext) -> None:
async def on_time_command(ctx: EventContext, vars: ContextVars) -> None:
"""!time"""
await ctx.bot.send.text(
ctx.room,
f"Current UNIX timestamp is <strong>{int(time.time())}</strong>"
)
async def on_raise_command(ctx: EventContext) -> None:
async def on_raise_command(ctx: EventContext, vars: ContextVars) -> None:
"""!raise"""
await ctx.bot.send.text(
ctx.room,
@@ -46,7 +46,7 @@ async def on_raise_command(ctx: EventContext) -> None:
)
raise RuntimeError()
async def on_assert_command(ctx: EventContext) -> None:
async def on_assert_command(ctx: EventContext, vars: ContextVars) -> None:
"""!assert"""
await ctx.bot.send.text(
ctx.room,
@@ -54,10 +54,10 @@ async def on_assert_command(ctx: EventContext) -> None:
)
assert False
async def on_mul_command(ctx: EventContext) -> None:
async def on_mul_command(ctx: EventContext, vars: ContextVars) -> None:
"""!mul"""
try:
numbers = [float(v) for v in ctx[CTX_CMD_ARGS]]
numbers = [float(v) for v in vars[CTX_COMMAND].args]
v = numbers[0]
for n in numbers[1:]:
v *= n
@@ -67,16 +67,16 @@ async def on_mul_command(ctx: EventContext) -> None:
except Exception as e:
await ctx.bot.send.text(ctx.room, f"Could not process the command: {e}")
async def on_args_command(ctx: EventContext) -> None:
async def on_args_command(ctx: EventContext, vars: ContextVars) -> None:
"""!args"""
try:
response = (
f"Prefix: <code>{ctx[CTX_CMD_PREFIX]}</code><br>"
f"Verb: <code>{ctx[CTX_CMD_VERB]}</code><br>"
f"Arguments: <code>{len(ctx[CTX_CMD_ARGS])}</code><br>"
f"Prefix: <code>{vars[CTX_COMMAND].prefix}</code><br>"
f"Verb: <code>{vars[CTX_COMMAND].verb}</code><br>"
f"Arguments: <code>{len(vars[CTX_COMMAND].args)}</code><br>"
f"Arguments are:<br><ol>"
)
for arg in ctx[CTX_CMD_ARGS]:
for arg in vars[CTX_COMMAND].args:
response += f"<li><code>{html.escape(arg)}</code></li>"
response += "</ol>"
await ctx.bot.send.text(
@@ -89,7 +89,7 @@ async def on_args_command(ctx: EventContext) -> None:
f"Could not process the command: {traceback.format_exc()}"
)
async def invalid_usage(ctx: EventContext) -> None:
async def invalid_usage(ctx: EventContext, vars: ContextVars) -> None:
"""This callback is called when the bot used incorrectly."""
await ctx.bot.send.text(ctx.room, "Use <code>!help</code>")