import enum import os import telethon import pgbotlib.dbstuff class Paths(enum.Enum): CONFDIR = '/etc/pgbot' CONFIG = os.path.join(CONFDIR, 'pgbot.yml') TOKENS = os.path.join(CONFDIR, 'tokens.yml') SCHEDULE = os.path.join(CONFDIR, '/sched.yml') class NameGenerator: def __init__(self, config: dict, db: pgbotlib.dbstuff.DBConn) -> None: self.db = db def get_name(self, sender: telethon.tl.types.User) -> str: query = 'SELECT name FROM names WHERE tg_id = %s' result = self.db.query_random(query, (sender.id,)) if result: return result return self.get_tg_name(sender) @staticmethod def get_tg_name(sender: telethon.tl.types.User) -> str: result = [sender.first_name, sender.last_name] while None in result: result.remove(None) if result: return ' '.join(result) if sender.username: return sender.username return sender.id class MiscReactor: def __init__(self, config: dict, db: pgbotlib.dbstuff.DBConn) -> None: self.namegen = NameGenerator(config, db) self.db = db def spawn_edited_handler(self, client: telethon.TelegramClient, trigger: telethon.events.common.EventBuilder) -> None: @client.on(trigger) async def handle_edited_message(event): sender = await event.get_sender() sender_name = self.namegen.get_name(sender) chat_id = event.message.peer_id await client.send_message(chat_id, f'Я всё видел! {sender_name} совсем охуел, сообщения правит!')