commands cosmetics part 1

This commit is contained in:
Von Random 2024-11-09 22:02:04 +02:00
parent c4f65650b9
commit dc2c195e95

View file

@ -10,7 +10,8 @@ import pgbotlib.response
# TODO: quote via response? # TODO: quote via response?
# chat = await event.get_chat() # chat = await event.get_chat()
# result = await client.get_messages(chat.id, ids=[event.message.reply_to.reply_to_msg_id]) # result = await client.get_messages(chat.id,
# ids=[event.message.reply_to.reply_to_msg_id])
# print(result) # print(result)
class Commander: class Commander:
T_START = frozenset(['cmd_start']) T_START = frozenset(['cmd_start'])
@ -59,15 +60,18 @@ class Commander:
def __add_response(self, caller: int, command: str) -> bool: def __add_response(self, caller: int, command: str) -> bool:
if caller not in self.admins: if caller not in self.admins:
return self.NOPE return self.NOPE
DB_QUERY = 'INSERT INTO responses (tokens, response) values (%s,%s)'
try:
input_tokens, phrase = command.strip().split(' ', 1) input_tokens, phrase = command.strip().split(' ', 1)
input_tokenset = frozenset(input_tokens.split(',')) input_tokenset = frozenset(input_tokens.split(','))
for token in input_tokenset: for token in input_tokenset:
if token not in self.available_tokens: if token not in self.available_tokens:
return False return "token not found!"
query = 'INSERT INTO responses (tokens, response) values (%s,%s)'
values = (','.join(sorted(input_tokenset)), phrase.strip()) values = (','.join(sorted(input_tokenset)), phrase.strip())
self.db_conn.update(query, values) self.db_conn.update(DB_QUERY, values)
return self.YEP return self.YEP
except Exception as e:
return str(e)
def __add_user(self, caller: int, userspec: str) -> bool: def __add_user(self, caller: int, userspec: str) -> bool:
if caller not in self.admins: if caller not in self.admins:
@ -79,7 +83,6 @@ class Commander:
self.db_conn.update(query, values) self.db_conn.update(query, values)
return self.YEP return self.YEP
def __start_response(self) -> str: def __start_response(self) -> str:
if self.responder.is_enabled(): if self.responder.is_enabled():
return self.responder.get_response(self.T_START_E) return self.responder.get_response(self.T_START_E)
@ -109,35 +112,32 @@ class Commander:
return None return None
command = event.message.text command = event.message.text
sender = await event.get_sender() sender = await event.get_sender()
response = None match command:
if command.startswith('.add '): case '.chat':
try: 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()
case '.stop':
response = self.__stop_response()
self.responder.disable()
case '.help':
response = self.DOC
case command if command.startswith('.add '):
response = self.__add_response(sender.id, command[5:]) response = self.__add_response(sender.id, command[5:])
except Exception as e: case command if command.startswith('.adduser '):
response = str(e)
elif command.startswith('.adduser '):
try: try:
response = self.__add_user(sender.id, command[9:]) response = self.__add_user(sender.id, command[9:])
except Exception as e: except Exception as e:
response = str(e) response = str(e)
elif command == '.chat': case command if command.startswith('.regex '):
response = str(chat_id)
elif command == '.list':
response = ', '.join(self.available_tokens)
elif command.startswith('.regex '):
response = self.__list_regex(command[7:].strip()) response = self.__list_regex(command[7:].strip())
elif command == '.users': case _:
users = await self.client.get_participants(
entity=event.message.peer_id)
response = self.__list_users(users)
elif command == '.start':
response = self.__start_response()
self.responder.enable()
elif command == '.stop':
response = self.__stop_response()
self.responder.disable()
elif command == '.help':
response = self.DOC
if response:
await self.client.send_message(event.message.peer_id, response)
return None return None
await self.client.send_message(event.message.peer_id, response)