pgbot/pgbotlib/misc.py

52 lines
1.7 KiB
Python
Raw Normal View History

2024-10-23 14:26:09 +03:00
import enum
import os
2023-10-31 01:45:36 +02:00
import telethon
import pgbotlib.dbstuff
2024-10-23 14:26:09 +03:00
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')
2023-10-31 01:45:36 +02:00
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} совсем охуел, сообщения правит!')