MatrixMediaBot/bot/events.py
2025-08-14 02:38:56 +04:00

43 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from nio import RoomMessageText, InviteMemberEvent
from .utils import is_trusted_room
class EventHandler:
def __init__(self, bot):
self.bot = bot
def register_callbacks(self):
"""Регистрация обработчиков событий"""
self.bot.client.add_event_callback(self.invite_callback, InviteMemberEvent)
self.bot.client.add_event_callback(self.message_callback, RoomMessageText)
self.bot.client.add_response_callback(self.bot.on_sync)
async def invite_callback(self, room, event):
"""Обработчик приглашений в комнаты"""
if event.state_key == self.bot.client.user_id:
if is_trusted_room(room.room_id, self.bot.config["trusted_homeservers"]):
print(f"[INFO] Joining trusted room: {room.room_id}")
try:
await self.bot.client.join(room.room_id)
# Отправляем приветственное сообщение
help_text = self.bot.command_handler.generate_help_text(detailed=False)
await self.bot.send_text(room.room_id, help_text)
except Exception as e:
print(f"[ERROR] Failed to join/send welcome: {str(e)}")
else:
print(f"[WARN] Ignored invite to untrusted room: {room.room_id}")
async def message_callback(self, room, event):
"""Обработчик входящих сообщений"""
# Игнорируем свои сообщения
if event.sender == self.bot.client.user_id:
return
# Не отвечаем если не загружен sync token
if not os.path.exists(self.bot.sync_token_file):
return
# Обрабатываем команду
await self.bot.command_handler.process_command(room.room_id, event)