refactor templates

This commit is contained in:
Von Random 2024-11-21 15:44:58 +02:00
parent e6a0726b0b
commit 822982e21c

View file

@ -90,22 +90,21 @@ class Responder:
api_spec = match.groups()
return self.api.call(*api_spec, message)
async def username(self, response: str,
async def template(self, response: str,
event: telethon.events.common.EventBuilder) -> str:
template = None
match response:
case response if '<username>' in response:
template = '<username>'
sender = await event.get_sender()
case response if '<randomname>' in response:
template = '<randomname>'
peer_id = event.message.peer_id
users = await self.client.get_participants(entity=peer_id)
sender = random.choice(users)[0]
if template:
username = self.namegen.get_name(sender)
return response.replace(template, username)
return response
result = response
if '<username>' in response:
template = '<username>'
sender = await event.get_sender()
result = result.replace(template, username)
if '<randomname>' in response:
template = '<randomname>'
peer_id = event.message.peer_id
users = await self.client.get_participants(entity=peer_id)
sender = random.choice(users)[0]
result = result.replace(template, username)
return result
async def respond(self,
event: telethon.events.common.EventBuilder) -> None:
@ -120,5 +119,5 @@ class Responder:
if not response:
return None
response = self.api_match(response, message)
response = await self.username(response, event)
response = await self.template(response, event)
await self.client.send_message(event.message.peer_id, response)