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

@@ -15,7 +15,7 @@ from mab import *
from _environment import check_environment
async def on_help_message(ctx: EventContext) -> None:
async def on_help_message(ctx: EventContext, vars: ContextVars) -> None:
"""Help message"""
RESPONSE = (
"Here's how you use the bot:"
@@ -32,8 +32,9 @@ async def on_help_message(ctx: EventContext) -> None:
)
await ctx.bot.send.text(ctx.room, RESPONSE, is_html=False)
async def on_cmd_create(ctx: EventContext) -> None:
async def on_cmd_create(ctx: EventContext, vars: ContextVars) -> None:
"""!create"""
raise NotImplementedError()
await ctx.bot.rooms.create()
alias = "".join([random.choice("abcdefghj") for i in range(10)])
name = alias.capitalize()
@@ -43,19 +44,19 @@ async def on_cmd_create(ctx: EventContext) -> None:
alias=alias,
name=name,
topic=topic,
invite_list=[ctx[CTX_SENDER]]
invite_list=[vars[CTX_EVENT]]
)
await ctx.bot.send.text(ctx.room, f"Created the room {res}")
except:
await ctx.bot.send.text(ctx.room, traceback.format_exc())
async def on_cmd_leave(ctx: EventContext) -> None:
async def on_cmd_leave(ctx: EventContext, vars: ContextVars) -> None:
"""!leave"""
await ctx.bot.send.text(ctx.room, "Leaving this room")
await asyncio.sleep(1)
await ctx.bot.rooms.leave(ctx.room)
async def on_cmd_forget(ctx: EventContext) -> None:
async def on_cmd_forget(ctx: EventContext, vars: ContextVars) -> None:
"""!forget"""
await ctx.bot.send.text(ctx.room, "Leaving the room and forgetting it")
await asyncio.sleep(1)
@@ -63,64 +64,64 @@ async def on_cmd_forget(ctx: EventContext) -> None:
await asyncio.sleep(1)
await ctx.bot.rooms.forget(ctx.room)
async def on_cmd_join(ctx: EventContext) -> None:
async def on_cmd_join(ctx: EventContext, vars: ContextVars) -> None:
"""!join"""
try:
room_id = ctx[CTX_CMD_ARGS][0]
room_id = vars[CTX_COMMAND].args[0]
await ctx.bot.send.text(ctx.room, f"Joining the room {room_id}")
await asyncio.sleep(1)
await ctx.bot.rooms.join(room_id)
except:
await ctx.bot.send.text(ctx.room, traceback.format_exc())
async def on_cmd_invite(ctx: EventContext) -> None:
async def on_cmd_invite(ctx: EventContext, vars: ContextVars) -> None:
"""!invite"""
try:
room_id = ctx[CTX_CMD_ARGS][0]
user_id = ctx[CTX_CMD_ARGS][1]
room_id = vars[CTX_COMMAND].args[0]
user_id = vars[CTX_COMMAND].args[1]
await ctx.bot.send.text(ctx.room, f"Inviting {user_id} to {room_id}")
await asyncio.sleep(1)
await ctx.bot.rooms.invite(room_id, user_id)
except:
await ctx.bot.send.text(ctx.room, traceback.format_exc())
async def on_cmd_kick(ctx: EventContext) -> None:
async def on_cmd_kick(ctx: EventContext, vars: ContextVars) -> None:
"""!kick"""
try:
room_id = ctx[CTX_CMD_ARGS][0]
user_id = ctx[CTX_CMD_ARGS][1]
room_id = vars[CTX_COMMAND].args[0]
user_id = vars[CTX_COMMAND].args[1]
await ctx.bot.send.text(ctx.room, f"Kicking {user_id} from {room_id}")
await asyncio.sleep(1)
await ctx.bot.rooms.kick(room_id, user_id, "Testing rooms")
except:
await ctx.bot.send.text(ctx.room, traceback.format_exc())
async def on_cmd_ban(ctx: EventContext) -> None:
async def on_cmd_ban(ctx: EventContext, vars: ContextVars) -> None:
"""!ban"""
try:
room_id = ctx[CTX_CMD_ARGS][0]
user_id = ctx[CTX_CMD_ARGS][1]
room_id = vars[CTX_COMMAND].args[0]
user_id = vars[CTX_COMMAND].args[1]
await ctx.bot.send.text(ctx.room, f"Banning {user_id} in {room_id}")
await asyncio.sleep(1)
await ctx.bot.rooms.ban(room_id, user_id, "Testing rooms")
except:
await ctx.bot.send.text(ctx.room, traceback.format_exc())
async def on_cmd_unban(ctx: EventContext) -> None:
async def on_cmd_unban(ctx: EventContext, vars: ContextVars) -> None:
"""!unban"""
try:
room_id = ctx[CTX_CMD_ARGS][0]
user_id = ctx[CTX_CMD_ARGS][1]
room_id = vars[CTX_COMMAND].args[0]
user_id = vars[CTX_COMMAND].args[1]
await ctx.bot.send.text(ctx.room, f"Unbanning {user_id} in {room_id}")
await asyncio.sleep(1)
await ctx.bot.rooms.unban(room_id, user_id)
except:
await ctx.bot.send.text(ctx.room, traceback.format_exc())
async def on_cmd_members(ctx: EventContext) -> None:
async def on_cmd_members(ctx: EventContext, vars: ContextVars) -> None:
"""!members"""
try:
room_id = ctx[CTX_CMD_ARGS][0]
room_id = vars[CTX_COMMAND].args[0]
await ctx.bot.send.text(ctx.room, f"Listing members of {room_id}")
await asyncio.sleep(1)
members = await ctx.bot.rooms.members(room_id)