hacky way to make template work in two places

This commit is contained in:
Von Random 2024-11-22 14:02:18 +02:00
parent 9b3dd1906f
commit 8bbda6ae7a
2 changed files with 21 additions and 16 deletions

View file

@ -36,6 +36,7 @@ class Cron:
await asyncio.sleep(wait_seconds) await asyncio.sleep(wait_seconds)
message = self.responder.get_response(tokens) message = self.responder.get_response(tokens)
message = self.responder.api_match(message, '') message = self.responder.api_match(message, '')
message = self.responder.rndm_tmpl(message, job['chat'])
await self.client.send_message(job['chat'], message) await self.client.send_message(job['chat'], message)
return send_message return send_message

View file

@ -90,20 +90,23 @@ class Responder:
api_spec = match.groups() api_spec = match.groups()
return self.api.call(*api_spec, message) return self.api.call(*api_spec, message)
async def template(self, response: str, async def user_tmpl(self, response: str,
event: telethon.events.common.EventBuilder) -> str: event: telethon.events.common.EventBuilder) -> str:
result = response marker = '<username>'
if '<username>' in response: if marker not in response:
return response
sender = await event.get_sender() sender = await event.get_sender()
username = self.namegen.get_name(sender) username = self.namegen.get_name(sender)
result = result.replace('<username>', username) return response.replace(marker, username)
if '<randomname>' in response:
peer_id = event.message.peer_id async def rndm_tmpl(self, response: str, chat_id: str) -> str:
users = await self.client.get_participants(entity=peer_id) marker = '<randomname>'
if marker not in response:
return response
users = await self.client.get_participants(entity=chat_id)
victim = random.choice(users) victim = random.choice(users)
username = self.namegen.get_name(victim) username = self.namegen.get_name(victim)
result = result.replace('<randomname>', username) return response.replace('<randomname>', username)
return result
async def respond(self, async def respond(self,
event: telethon.events.common.EventBuilder) -> None: event: telethon.events.common.EventBuilder) -> None:
@ -118,5 +121,6 @@ class Responder:
if not response: if not response:
return None return None
response = self.api_match(response, message) response = self.api_match(response, message)
response = await self.template(response, event) response = await self.user_tmpl(response, event)
await self.client.send_message(event.message.peer_id, response) response = await self.rndm_tmpl(response, chat_id)
await self.client.send_message(chat_id, response)