forked from FoxUserbot/FoxUserbot
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmigrate.py
More file actions
177 lines (137 loc) · 6.91 KB
/
migrate.py
File metadata and controls
177 lines (137 loc) · 6.91 KB
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
import os
import re
from pathlib import Path
def convert_module_new_format(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
module_name_match = re.search(r"module_list\['([^']+)'\]", content)
if not module_name_match:
return
module_name = module_name_match.group(1)
content = re.sub(r"module_list\['[^']+'\].*?\n", "", content)
content = re.sub(r"file_list\['[^']+'\].*?\n", "", content)
content = re.sub(r"^from command import fox_command\s*\n?", "", content, flags=re.MULTILINE)
content = re.sub(r"^import os\s*\n?", "", content, flags=re.MULTILINE)
new_imports = "from command import fox_command\nimport os\n\n"
content = new_imports + content.lstrip()
if "filename = os.path.basename(__file__)" not in content:
insert_pos = 0
lines = content.splitlines(keepends=True)
for i, line in enumerate(lines):
if not line.strip() or line.startswith(("from ", "import ")):
continue
insert_pos = i
break
new_code = "filename = os.path.basename(__file__)\nModule_Name = '{}'\n\n".format(module_name)
content = "".join(lines[:insert_pos] + [new_code] + lines[insert_pos:])
content = re.sub(
r'filters\.command\(([\'"\[\]])([^\)]+)\1\s*,\s*prefixes\s*=\s*my_prefix\(\)\)',
lambda m: f'fox_command({m.group(1)}{m.group(2)}{m.group(1)}, Module_Name, filename)',
content
)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"File {file_path} converted!")
def convert_module_filters_me(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Проверяем наличие необходимых импортов
has_fox_sudo = "fox_sudo" in content
has_who_message = "who_message" in content
has_fox_command = "fox_command" in content
content = content.replace("message = await who_message(client, message, message.reply_to_message)", "message = await who_message(client, message)")
content = content.replace("from prefix import my_prefix", "from command import my_prefix")
content = content.replace("from modules.plugins_1system", "from modules.core")
# Умное добавление импортов
if "from command import" in content and not (has_fox_sudo and has_who_message):
content = re.sub(
r'(from command import)([^\n]*)',
lambda m: m.group(0) +
('' if has_fox_sudo else ', fox_sudo') +
('' if has_who_message else ', who_message'),
content,
count=1
)
# Обновляем флаги после добавления импортов
has_fox_sudo = has_fox_sudo or "fox_sudo" in content
has_who_message = has_who_message or "who_message" in content
# Заменяем только filters.me без ~ перед ним
if has_fox_sudo:
def safe_replace(match):
before = match.group(1)
after = match.group(2)
if not before.rstrip().endswith('~'):
if after and after[0] in (' ', '&', '|', ')', '\n'):
return f'@Client.on_message({before}fox_sudo(){after}'
return match.group(0)
content = re.sub(
r'@Client\.on_message\((.*?)filters\.me([^a-zA-Z0-9_]*)',
safe_replace,
content
)
# Умное добавление who_message в функции
if has_who_message:
def add_who_message(match):
decorator = match.group(1)
func_block = match.group(2)
# Проверяем, есть ли fox_command в декораторе
needs_who_message = "fox_command" in decorator
if needs_who_message and 'message = await who_message(client, message)' not in func_block:
func_block = re.sub(
r'(async def \w+\(client, message\):\n)',
r'\1 message = await who_message(client, message)\n',
func_block,
count=1
)
return decorator + func_block
content = re.sub(
r'(@Client\.on_message\(.*?\)\n)(async def \w+\(client, message\):[\s\S]*?(?=\n\n|\Z))',
add_who_message,
content
)
content = content.replace("message.command[", "message.text.split()[")
content = re.sub(
r'async def (\w+)\(client: Client, message: Message\)',
r'async def \1(client, message)',
content
)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
def check_duplicate(folder_path):
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
# Пропускаем папки и не .py файлы
if not os.path.isfile(file_path) or not filename.endswith('.py'):
continue
# Заменяем пробелы на _ и удаляем все скобки
new_name = re.sub(r'[()]', '', filename.replace(' ', '_'))
# Переименовываем только если имя изменилось
if new_name != filename:
new_path = os.path.join(folder_path, new_name)
try:
os.rename(file_path, new_path)
print(f'Renamed: {filename} -> {new_name}')
except Exception as f:
print(f)
def replace_chat_privileges(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Проверяем, есть ли импорт ChatPrivileges в pyrogram.types
if re.search(r'from pyrogram\.types import.*\bChatPrivileges\b|import.*\bChatPrivileges\b', content):
# Заменяем все вхождения ChatPrivileges на ChatAdministratorRights
content = re.sub(r'\bChatPrivileges\b', 'ChatAdministratorRights', content)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Replaced ChatPrivileges in {file_path}")
def process_modules_directory(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.py') and file != '__init__.py':
file_path = os.path.join(root, file)
replace_chat_privileges(file_path)
convert_module_new_format(file_path)
convert_module_filters_me(file_path)
check_duplicate(directory)
def convert_modules():
process_modules_directory("modules/loaded")
convert_modules()