-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyg_trigger.py
302 lines (243 loc) · 13.1 KB
/
yg_trigger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
__version__ = (1, 0, 0, 1)
# This file is a part of Hikka Userbot
# Code is NOT licensed under CC-BY-NC-ND 4.0 unless otherwise specified.
# 🌐 https://github.com/hikariatama/Hikka
# You CAN edit this file without direct permission from the author.
# You can redistribute this file with any modifications.
# meta developer: @yg_modules
# scope: hikka_only
# scope: hikka_min 1.6.3
# █▄█ █░█ █▀▄▀█ █▀▄▀█ █▄█ █▀▄▀█ █▀█ █▀▄ █▀
# ░█░ █▄█ █░▀░█ █░▀░█ ░█░ █░▀░█ █▄█ █▄▀ ▄█
import re
from .. import loader, utils
from telethon import events
import os
@loader.tds
class yg_trigger(loader.Module):
"""Триггер-модуль. Документация: ямме.рф/триггер"""
strings = {"name": "yg_trigger"}
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue(
"watcher_on",
True,
"состояние вотчера",
validator=loader.validators.Boolean()
)
)
async def client_ready(self, client, db):
self.db = db
self.triggers = self.db.get("triggers", "list", [])
self.client = client
handlers = [
(self.trigger, [events.NewMessage, events.MessageEdited])
]
for handler_func, event_list in handlers:
for event in event_list:
self.client.add_event_handler(handler_func, event)
async def save_triggers(self):
self.db.set("triggers", "list", self.triggers)
async def validate(self, entity_value, target_entity):
entity_k = entity_value.replace("@", "")
entity_id = getattr(target_entity, "id", None)
entity_usernames = await self.get_usernames(target_entity)
if entity_k.isdigit():
return int(entity_value) == entity_id
return entity_k.lower() in entity_usernames
async def add_trigger(self, conditions: dict, response: str):
self.triggers.append({"conditions": conditions, "response": response})
await self.save_triggers()
async def triggercmd(self, message):
"""вкл/выкл вотчер"""
self.config["watcher_on"] = not self.config["watcher_on"]
await message.edit(f"<emoji document_id=5361741454685256344>🎮</emoji> <b>Триггер-мод {'включен' if self.config['watcher_on'] else 'выключен'}</b>")
async def remove_trigger(self, index: int):
try:
del self.triggers[index]
await self.save_triggers()
return True
except IndexError:
return False
async def clear_triggers(self):
self.triggers = []
await self.save_triggers()
async def get_usernames(self, entity):
usernames = []
if entity.username:
username = entity.username
if username:
usernames.append(username.lower())
if entity.usernames:
additional_usernames = [
u.username.lower() for u in (getattr(entity, "usernames", []) or [])
]
usernames.extend(additional_usernames)
return usernames
async def list_triggerscmd(self, message):
"""показать все существующие триггеры"""
if not self.triggers:
await utils.answer(message, "<emoji document_id=5461117441612462242>🙂</emoji> <b>Нет активных триггеров</b>")
return
reply_with_html = "<emoji document_id=5334544901428229844>ℹ️</emoji> <b>Список триггеров:</b>\n\n"
reply_plain_text = "Список триггеров:\n\n"
for i, trigger in enumerate(self.triggers):
conditions = "\n".join([f"<b>-</b> {k}: <code>{v}</code>" for k, v in trigger["conditions"].items()])
if not conditions:
conditions = "<b>Нет условий (all=true)</b>"
reply_with_html += f"<b>{i}.</b> Условия:\n{conditions}\n\n<emoji document_id=5443038326535759644>💬</emoji> <b>Ответ:</b> <code>{trigger['response']}</code>\n\n"
reply_plain_text += f"{i}.\nУсловия:\n{conditions}\nОтвет: {trigger['response']}\n\n"
if len(reply_with_html) > 4096:
file_path = "triggers_list.txt"
with open(file_path, "w", encoding="utf-8") as file:
file.write(reply_plain_text)
await message.delete()
await self.client.send_file(message.chat, caption="<emoji document_id=5433653135799228968>📁</emoji> <i>Вывод команды слишком длинный, поэтому он отправлен в файле.</i>", file=file_path)
os.remove(file_path)
else:
await utils.answer(message, reply_with_html)
async def split(self, input_str):
is_in_quotes = False
split_index = -1
for i, char in enumerate(input_str):
if char == '"':
is_in_quotes = not is_in_quotes
elif char == '|' and not is_in_quotes:
split_index = i
break
if split_index == -1:
raise ValueError("Некорректный формат: отсутствует разделитель '|'")
return input_str[:split_index].strip(), input_str[split_index + 1:].strip()
async def add_triggercmd(self, message):
"""<условия> | <ответ> - добавить новый триггер"""
args = utils.get_args_raw(message)
if not args or "|" not in args:
await utils.answer(
message,
"<emoji document_id=5240241223632954241>🚫</emoji> <b>Формат: <условия> | <ответ></b>\n<emoji document_id=5325547803936572038>✨</emoji> Пример: <code>text=\"Привет\" | Здравствуй!</code>"
)
return
try:
conditions_raw, response = await self.split(args)
valid_keys = {
"text", "user", "chat", "starts_with", "ends_with", "contains",
"regex", "is_command", "word_count", "char_count", "is_reply", "is_forwarded",
"media_type", "message_length", "time_range", "date", "weekday", "all"
}
conditions = {}
pattern = r'(\w+)=((?:\"(?:[^\"]|\\\")*\"|[^,\s]+))'
matches = re.finditer(pattern, conditions_raw)
for match in matches:
key, value = match.group(1), match.group(2)
if key not in valid_keys:
raise ValueError(f"Некорректное условие: {key}")
if key in {"text", "regex", "contains", "starts_with", "ends_with"}:
if not (value.startswith('"') and value.endswith('"')):
raise ValueError(f"Значение для условия {key} должно быть в кавычках")
value = value[1:-1].replace('\\"', '"')
if key == "all" and value.lower() == "true":
conditions = {"all": True}
break
conditions[key] = value
if not conditions:
raise ValueError(
"😨 Ты не указал никаких условий.. Если ты такой смелый, укажи all=true в условиях!"
)
if conditions.get("all"):
conditions = {}
await self.add_trigger(conditions, response)
conditions_str = "\n".join([f"<b>-</b> {k}: <code>{v}</code>" for k, v in conditions.items()])
if not conditions_str:
conditions_str = "<b>Нет условий (all=true)</b>"
await utils.answer(
message,
f"<emoji document_id=5456140674028019486>⚡️</emoji> <b>Триггер добавлен!</b>\n\nУсловия:\n{conditions_str}\n\n<emoji document_id=5443038326535759644>💬</emoji> <b>Ответ:</b> <code>{response}</code>"
)
except ValueError as e:
await utils.answer(message, f"<emoji document_id=5240241223632954241>🚫</emoji> <b>Ошибка:</b> <code>{e}</code>")
except Exception as e:
await utils.answer(message, f"<emoji document_id=5240241223632954241>🚫</emoji> <b>Непредвиденная ошибка:</b> <code>{e}</code>")
async def remove_triggercmd(self, message):
"""<номер> - удалить триггер"""
args = utils.get_args_raw(message)
if not args.isdigit():
await utils.answer(
message,
"<emoji document_id=5240241223632954241>🚫</emoji> <b>Укажи номер триггера для удаления</b>"
)
return
index = int(args)
if await self.remove_trigger(index):
await utils.answer(message, "<emoji document_id=5445267414562389170>🗑</emoji> <b>Триггер удалён!</b>")
else:
await utils.answer(message, "<emoji document_id=5240241223632954241>🚫</emoji> <b> Неверный номер триггера</b>")
async def clear_triggerscmd(self, message):
"""удалить все триггеры"""
await self.clear_triggers()
await utils.answer(message, "<emoji document_id=5445267414562389170>🗑</emoji> <b>Все триггеры удалены!</b>")
async def trigger(self, message):
if not self.config["watcher_on"]:
return
responses = []
for trigger in self.triggers:
conditions = trigger["conditions"]
match = True
for key, value in conditions.items():
value = value.strip('"')
if key == "text" and value.lower() != message.raw_text.lower():
match = False
break
elif key == "user" and not await self.validate(value, message.sender):
match = False
break
elif key == "chat" and not await self.validate(value, message.chat):
match = False
break
elif key == "starts_with" and not message.raw_text.lower().startswith(value.lower()):
match = False
break
elif key == "ends_with" and not message.raw_text.lower().endswith(value.lower()):
match = False
break
elif key == "contains" and value.lower() not in message.raw_text.lower():
match = False
break
elif key == "regex" and not re.search(value, message.raw_text):
match = False
break
elif key == "is_command" and value.lower() == "true" and not message.raw_text.startswith("/"):
match = False
break
elif key == "word_count" and len(message.raw_text.split()) != int(value):
match = False
break
elif key == "char_count" and len(message.raw_text) != int(value):
match = False
break
elif key == "is_reply" and value.lower() == "true" and not message.is_reply:
match = False
break
elif key == "is_forwarded" and value.lower() == "true" and not message.fwd_from:
match = False
break
elif key == "media_type":
media_types = {"photo": message.photo, "video": message.video, "sticker": message.sticker, "voice": message.voice, "audio": message.audio}
if not media_types.get(value.lower()):
match = False
break
elif key == "message_length" and not (int(value.split("-", 1)[0]) <= len(message.raw_text) <= int(value.split("-", 1)[1])):
match = False
break
elif key == "time_range" and not (int(value.split("-", 1)[0]) <= message.date.hour <= int(value.split("-", 1)[1])):
match = False
break
elif key == "date" and value != message.date.strftime("%Y-%m-%d"):
match = False
break
elif key == "weekday" and value.lower() != message.date.strftime("%A").lower():
match = False
break
if match:
responses.append(trigger["response"])
for response in responses:
await message.reply(response)