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