pgbot/pgbotlib/misc.py

42 lines
1.4 KiB
Python
Raw Normal View History

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