Added CTX_ROOM_EVENT

This commit is contained in:
2026-10-31 21:00:45 +03:00
parent d5367ebc6b
commit ada7af9dd6
6 changed files with 57 additions and 3 deletions

View File

@@ -34,7 +34,6 @@ async def on_help_message(ctx: EventContext, vars: ContextVars) -> None:
async def on_cmd_create(ctx: EventContext, vars: ContextVars) -> None: async def on_cmd_create(ctx: EventContext, vars: ContextVars) -> None:
"""!create""" """!create"""
raise NotImplementedError()
await ctx.bot.rooms.create() await ctx.bot.rooms.create()
alias = "".join([random.choice("abcdefghj") for i in range(10)]) alias = "".join([random.choice("abcdefghj") for i in range(10)])
name = alias.capitalize() name = alias.capitalize()
@@ -44,7 +43,7 @@ async def on_cmd_create(ctx: EventContext, vars: ContextVars) -> None:
alias=alias, alias=alias,
name=name, name=name,
topic=topic, topic=topic,
invite_list=[vars[CTX_EVENT]] invite_list=[vars[CTX_ROOM_EVENT].sender]
) )
await ctx.bot.send.text(ctx.room, f"Created the room {res}") await ctx.bot.send.text(ctx.room, f"Created the room {res}")
except: except:

View File

@@ -11,4 +11,5 @@ from .context.vars import ContextVars
# Context variables # Context variables
from .context.vars.command import * from .context.vars.command import *
from .context.vars.message import * from .context.vars.message import *
from .context.vars.room_event import *

View File

@@ -0,0 +1,3 @@
from .types import *
from .keys import *
from . import builders

View File

@@ -0,0 +1,17 @@
from ... import EventContext
from .. import ContextVars, ContextVarsPopulator
from . import *
@ContextVarsPopulator.autobuilder
def _build_room_event(ctx: EventContext, vars: ContextVars) -> None:
ev = ctx.event
vars[CTX_ROOM_EVENT] = RoomEvent(
source=ev.source,
content=ev.source["content"],
event_id=ev.event_id,
origin_server_ts=ev.source["origin_server_ts"],
room_id=ev.source["room_id"],
sender=ev.sender,
type=ev.source["type"]
)

View File

@@ -0,0 +1,5 @@
from .types import *
from .. import ContextVarKey
CTX_ROOM_EVENT = ContextVarKey[RoomEvent]("CTX_ROOM_EVENT")
"""Variable that describes the event that has happened in the room."""

View File

@@ -0,0 +1,29 @@
from pydantic import BaseModel
class RoomEvent(BaseModel):
"""This model defines basic fields of the room event."""
source: dict
"""`event.source` as returned by `matrix-nio`."""
content: dict
"""Raw event content as returned by the matrix homeserver. You should
validate it as it is not guranteed that the schema is valid."""
event_id: str
"""The globally unique identifier for the event. It is completely opaque and
is not intended to be human-readable."""
origin_server_ts: int
"""Timestamp (in milliseconds since the unix epoch) on originating
homeserver when this event was sent."""
room_id: str
"""Opaque identifier of a room starting with excamation mark. They are not
indended to be human-readable even if they contain readable domain."""
sender: str
"""Fully-qualified ID of the user who sent the event."""
type: str
"""The type of the event."""