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
input_tokens, phrase = command.strip().split(' ', 1) DB_QUERY = 'INSERT INTO responses (tokens, response) values (%s,%s)'
input_tokenset = frozenset(input_tokens.split(',')) try:
for token in input_tokenset: input_tokens, phrase = command.strip().split(' ', 1)
if token not in self.available_tokens: input_tokenset = frozenset(input_tokens.split(','))
return False for token in input_tokenset:
query = 'INSERT INTO responses (tokens, response) values (%s,%s)' if token not in self.available_tokens:
values = (','.join(sorted(input_tokenset)), phrase.strip()) return "token not found!"
self.db_conn.update(query, values) values = (','.join(sorted(input_tokenset)), phrase.strip())
return self.YEP self.db_conn.update(DB_QUERY, values)
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,16 +83,15 @@ 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)
return self.responder.get_response(self.T_START) return self.responder.get_response(self.T_START)
def __stop_response(self) -> str: def __stop_response(self) -> str:
if self.responder.is_enabled(): if self.responder.is_enabled():
return self.responder.get_response(self.T_STOP) return self.responder.get_response(self.T_STOP)
return self.responder.get_response(self.T_STOP_D) return self.responder.get_response(self.T_STOP_D)
def __list_users(self, users: list) -> str: def __list_users(self, users: list) -> str:
userlist = [f'{user.id}: {self.namegen.get_tg_name(user)}' userlist = [f'{user.id}: {self.namegen.get_tg_name(user)}'
@ -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) try:
elif command.startswith('.adduser '): response = self.__add_user(sender.id, command[9:])
try: except Exception as e:
response = self.__add_user(sender.id, command[9:]) response = str(e)
except Exception as e: case command if command.startswith('.regex '):
response = str(e) response = self.__list_regex(command[7:].strip())
elif command == '.chat': case _:
response = str(chat_id) return None
elif command == '.list': await self.client.send_message(event.message.peer_id, response)
response = ', '.join(self.available_tokens)
elif command.startswith('.regex '):
response = self.__list_regex(command[7:].strip())
elif command == '.users':
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