From d84707900d96b2c0142f266d9287fd4136b1470c Mon Sep 17 00:00:00 2001 From: Von Random Date: Sat, 9 Nov 2024 22:31:28 +0200 Subject: [PATCH] commands cosmetics part 2 --- pgbotlib/commands.py | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/pgbotlib/commands.py b/pgbotlib/commands.py index 4efc4e9..09deedd 100644 --- a/pgbotlib/commands.py +++ b/pgbotlib/commands.py @@ -18,6 +18,7 @@ class Commander: T_START_E = frozenset(['cmd_start_enabled']) T_STOP = frozenset(['cmd_stop']) T_STOP_D = frozenset(['cmd_stop_disabled']) + TOKEN_FAIL = "нет такой буквы в этом слове!" NOPE = "а ты что ещё за хуй с горы?" YEP = "да, господин!" DOC = """ @@ -66,7 +67,7 @@ class Commander: input_tokenset = frozenset(input_tokens.split(',')) for token in input_tokenset: if token not in self.available_tokens: - return "token not found!" + return self.TOKEN_FAIL values = (','.join(sorted(input_tokenset)), phrase.strip()) self.db_conn.update(DB_QUERY, values) return self.YEP @@ -76,12 +77,15 @@ class Commander: def __add_user(self, caller: int, userspec: str) -> bool: if caller not in self.admins: return self.NOPE - user_id, names = userspec.strip().split(' ', 1) - for name in names.strip().split(','): - query = 'INSERT INTO names (tg_id, name) values(%s,%s)' - values = (user_id, name) - self.db_conn.update(query, values) - return self.YEP + DB_QUERY = 'INSERT INTO names (tg_id, name) values (%s,%s)' + try: + user_id, names = userspec.strip().split(' ', 1) + for name in names.strip().split(','): + values = (user_id, name) + self.db_conn.update(DB_QUERY, values) + return self.YEP + except Exception as e: + return str(e) def __start_response(self) -> str: if self.responder.is_enabled(): @@ -107,20 +111,13 @@ class Commander: async def action(self, event: telethon.events.common.EventBuilder) -> None: - chat_id = telethon.utils.get_peer_id(event.message.peer_id) + peer_id = event.message.peer_id + chat_id = telethon.utils.get_peer_id(peer_id) if chat_id not in self.chats: return None command = event.message.text sender = await event.get_sender() match command: - case '.chat': - response = str(chat_id) - case '.list': - response = ', '.join(self.available_tokens) - case '.users': - users = await self.client.get_participants( - entity=event.message.peer_id) - response = self.__list_users(users) case '.start': response = self.__start_response() self.responder.enable() @@ -129,15 +126,20 @@ class Commander: self.responder.disable() case '.help': response = self.DOC + case '.chat': + #response = str(chat_id) + response = f'chat_id: {chat_id}\npeer_id: {peer_id}' + case '.users': + users = await self.client.get_participants(entity=peer_id) + response = self.__list_users(users) + case '.list': + response = ', '.join(self.available_tokens) case command if command.startswith('.add '): response = self.__add_response(sender.id, command[5:]) case command if command.startswith('.adduser '): - try: - response = self.__add_user(sender.id, command[9:]) - except Exception as e: - response = str(e) + response = self.__add_user(sender.id, command[9:]) case command if command.startswith('.regex '): response = self.__list_regex(command[7:].strip()) case _: return None - await self.client.send_message(event.message.peer_id, response) + await self.client.send_message(peer_id, response)