2023-10-31 01:45:36 +02:00
|
|
|
""" Respond to commands """
|
|
|
|
|
|
|
|
import telethon
|
2023-11-05 13:31:42 +02:00
|
|
|
import telethon.utils
|
2023-10-31 01:45:36 +02:00
|
|
|
|
|
|
|
import pgbotlib.dbstuff
|
2023-11-01 02:26:09 +02:00
|
|
|
import pgbotlib.misc
|
2023-10-31 01:45:36 +02:00
|
|
|
import pgbotlib.response
|
|
|
|
|
|
|
|
|
2023-11-01 02:26:09 +02:00
|
|
|
# TODO: quote via response?
|
|
|
|
# chat = await event.get_chat()
|
2024-11-09 22:02:04 +02:00
|
|
|
# result = await client.get_messages(chat.id,
|
|
|
|
# ids=[event.message.reply_to.reply_to_msg_id])
|
2023-11-01 02:26:09 +02:00
|
|
|
# print(result)
|
2023-10-31 01:45:36 +02:00
|
|
|
class Commander:
|
2023-11-01 02:26:09 +02:00
|
|
|
T_START = frozenset(['cmd_start'])
|
|
|
|
T_START_E = frozenset(['cmd_start_enabled'])
|
|
|
|
T_STOP = frozenset(['cmd_stop'])
|
2023-11-05 13:31:42 +02:00
|
|
|
T_STOP_D = frozenset(['cmd_stop_disabled'])
|
2023-11-06 23:21:00 +02:00
|
|
|
NOPE = "а ты что ещё за хуй с горы?"
|
|
|
|
YEP = "да, господин!"
|
2023-11-05 13:31:42 +02:00
|
|
|
DOC = """
|
|
|
|
Команды:
|
|
|
|
__.start__
|
|
|
|
запустить бота
|
|
|
|
__.stop__
|
|
|
|
остановить бота
|
|
|
|
__.list__
|
|
|
|
перечислить доступные токены
|
2023-11-06 23:21:00 +02:00
|
|
|
__.regex token__
|
|
|
|
перечислить регулярные выражения, относящиеся к токену
|
2023-11-05 13:31:42 +02:00
|
|
|
__.chat__
|
|
|
|
получить id текущего чата
|
|
|
|
__.users__
|
|
|
|
перечислить id пользователей
|
|
|
|
__.add token1[,token2,...] your phrase here__
|
|
|
|
добавить фразу your phrase here для реакции на токены
|
|
|
|
__.adduser id имя__
|
|
|
|
добавить пользователю имя
|
|
|
|
__.help__
|
|
|
|
вывести этот текст
|
|
|
|
"""
|
2023-10-31 01:45:36 +02:00
|
|
|
|
|
|
|
def __init__(self, config: dict,
|
|
|
|
client: telethon.TelegramClient,
|
|
|
|
admins: list,
|
|
|
|
db_conn: pgbotlib.dbstuff.DBConn,
|
2023-11-01 02:26:09 +02:00
|
|
|
namegen: pgbotlib.misc.NameGenerator,
|
2023-10-31 01:45:36 +02:00
|
|
|
responder: pgbotlib.response.Responder) -> None:
|
2023-11-05 13:31:42 +02:00
|
|
|
self.chats = config['chats']
|
2023-10-31 01:45:36 +02:00
|
|
|
self.client = client
|
|
|
|
self.admins = admins
|
|
|
|
self.db_conn = db_conn
|
2023-11-01 02:26:09 +02:00
|
|
|
self.namegen = namegen
|
2023-10-31 01:45:36 +02:00
|
|
|
self.responder = responder
|
|
|
|
self.available_tokens = [
|
|
|
|
str(token) for token, _ in self.responder.tokens]
|
|
|
|
|
2023-11-05 13:31:42 +02:00
|
|
|
def __add_response(self, caller: int, command: str) -> bool:
|
2023-10-31 01:45:36 +02:00
|
|
|
if caller not in self.admins:
|
2023-11-06 23:21:00 +02:00
|
|
|
return self.NOPE
|
2024-11-09 22:02:04 +02:00
|
|
|
DB_QUERY = 'INSERT INTO responses (tokens, response) values (%s,%s)'
|
|
|
|
try:
|
|
|
|
input_tokens, phrase = command.strip().split(' ', 1)
|
|
|
|
input_tokenset = frozenset(input_tokens.split(','))
|
|
|
|
for token in input_tokenset:
|
|
|
|
if token not in self.available_tokens:
|
|
|
|
return "token not found!"
|
|
|
|
values = (','.join(sorted(input_tokenset)), phrase.strip())
|
|
|
|
self.db_conn.update(DB_QUERY, values)
|
|
|
|
return self.YEP
|
|
|
|
except Exception as e:
|
|
|
|
return str(e)
|
2023-10-31 01:45:36 +02:00
|
|
|
|
2023-11-01 02:26:09 +02:00
|
|
|
def __add_user(self, caller: int, userspec: str) -> bool:
|
|
|
|
if caller not in self.admins:
|
2023-11-06 23:21:00 +02:00
|
|
|
return self.NOPE
|
2023-11-01 02:26:09 +02:00
|
|
|
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)
|
2023-11-06 23:21:00 +02:00
|
|
|
return self.YEP
|
2023-11-05 13:31:42 +02:00
|
|
|
|
2023-11-01 02:26:09 +02:00
|
|
|
def __start_response(self) -> str:
|
2024-11-09 22:02:04 +02:00
|
|
|
if self.responder.is_enabled():
|
|
|
|
return self.responder.get_response(self.T_START_E)
|
|
|
|
return self.responder.get_response(self.T_START)
|
2023-11-01 02:26:09 +02:00
|
|
|
|
|
|
|
def __stop_response(self) -> str:
|
2024-11-09 22:02:04 +02:00
|
|
|
if self.responder.is_enabled():
|
|
|
|
return self.responder.get_response(self.T_STOP)
|
|
|
|
return self.responder.get_response(self.T_STOP_D)
|
2023-11-01 02:26:09 +02:00
|
|
|
|
|
|
|
def __list_users(self, users: list) -> str:
|
|
|
|
userlist = [f'{user.id}: {self.namegen.get_tg_name(user)}'
|
|
|
|
for user in users]
|
|
|
|
return '\n'.join(userlist)
|
|
|
|
|
2023-11-06 23:21:00 +02:00
|
|
|
def __list_regex(self, token: str) -> str:
|
|
|
|
for t, r in self.responder.tokens:
|
|
|
|
if token == t:
|
|
|
|
regexlist = [i.pattern for i in r]
|
|
|
|
return '\n'.join(regexlist)
|
|
|
|
return 'not found!'
|
|
|
|
|
2023-10-31 01:45:36 +02:00
|
|
|
async def action(self,
|
|
|
|
event: telethon.events.common.EventBuilder) -> None:
|
2023-11-05 13:31:42 +02:00
|
|
|
chat_id = telethon.utils.get_peer_id(event.message.peer_id)
|
|
|
|
if chat_id not in self.chats:
|
|
|
|
return None
|
2023-10-31 01:45:36 +02:00
|
|
|
command = event.message.text
|
|
|
|
sender = await event.get_sender()
|
2024-11-09 22:02:04 +02:00
|
|
|
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()
|
|
|
|
case '.stop':
|
|
|
|
response = self.__stop_response()
|
|
|
|
self.responder.disable()
|
|
|
|
case '.help':
|
|
|
|
response = self.DOC
|
|
|
|
case command if command.startswith('.add '):
|
2023-11-05 13:31:42 +02:00
|
|
|
response = self.__add_response(sender.id, command[5:])
|
2024-11-09 22:02:04 +02:00
|
|
|
case command if command.startswith('.adduser '):
|
|
|
|
try:
|
|
|
|
response = self.__add_user(sender.id, command[9:])
|
|
|
|
except Exception as e:
|
|
|
|
response = str(e)
|
|
|
|
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)
|