From 8bbda6ae7adeaca489f570ad147109cbfe1bbb0c Mon Sep 17 00:00:00 2001 From: Von Random Date: Fri, 22 Nov 2024 14:02:18 +0200 Subject: [PATCH] hacky way to make template work in two places --- pgbotlib/cron.py | 1 + pgbotlib/response.py | 36 ++++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pgbotlib/cron.py b/pgbotlib/cron.py index 3d80275..398fbae 100644 --- a/pgbotlib/cron.py +++ b/pgbotlib/cron.py @@ -36,6 +36,7 @@ class Cron: await asyncio.sleep(wait_seconds) message = self.responder.get_response(tokens) message = self.responder.api_match(message, '') + message = self.responder.rndm_tmpl(message, job['chat']) await self.client.send_message(job['chat'], message) return send_message diff --git a/pgbotlib/response.py b/pgbotlib/response.py index 59abccb..c93d041 100644 --- a/pgbotlib/response.py +++ b/pgbotlib/response.py @@ -90,20 +90,23 @@ class Responder: api_spec = match.groups() return self.api.call(*api_spec, message) - async def template(self, response: str, - event: telethon.events.common.EventBuilder) -> str: - result = response - if '' in response: - sender = await event.get_sender() - username = self.namegen.get_name(sender) - result = result.replace('', username) - if '' in response: - peer_id = event.message.peer_id - users = await self.client.get_participants(entity=peer_id) - victim = random.choice(users) - username = self.namegen.get_name(victim) - result = result.replace('', username) - return result + async def user_tmpl(self, response: str, + event: telethon.events.common.EventBuilder) -> str: + marker = '' + if marker not in response: + return response + sender = await event.get_sender() + username = self.namegen.get_name(sender) + return response.replace(marker, username) + + async def rndm_tmpl(self, response: str, chat_id: str) -> str: + marker = '' + if marker not in response: + return response + users = await self.client.get_participants(entity=chat_id) + victim = random.choice(users) + username = self.namegen.get_name(victim) + return response.replace('', username) async def respond(self, event: telethon.events.common.EventBuilder) -> None: @@ -118,5 +121,6 @@ class Responder: if not response: return None response = self.api_match(response, message) - response = await self.template(response, event) - await self.client.send_message(event.message.peer_id, response) + response = await self.user_tmpl(response, event) + response = await self.rndm_tmpl(response, chat_id) + await self.client.send_message(chat_id, response)