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,
|
2024-11-09 22:01:38 +02:00
|
|
|
trigger: telethon.events.common.EventBuilder
|
|
|
|
) -> None:
|
2023-10-31 01:45:36 +02:00
|
|
|
@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
|
2024-11-09 22:01:38 +02:00
|
|
|
await client.send_message(
|
|
|
|
chat_id,
|
|
|
|
f'Я всё видел! {sender_name} совсем охуел, сообщения правит!'
|
|
|
|
)
|