forked from FoxUserbot/FoxUserbot
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommand.py
More file actions
183 lines (142 loc) · 4.93 KB
/
command.py
File metadata and controls
183 lines (142 loc) · 4.93 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
178
179
180
181
182
183
import configparser
import json
import os
from pathlib import Path
from typing import List, Union
from pyrogram import filters
from pyrogram.types import ReplyParameters
from modules.core.settings.main_settings import add_command_help, file_list
#* language ===================
all_lang = ["en", "ru", "ua"]
default_lang = "en"
#* ============================
_PREFIX = None
_GLOBAL_LANG = None
# prefix
def my_prefix():
global _PREFIX
if _PREFIX is not None:
return _PREFIX
PATH_FILE = "userdata/config.ini"
Path("userdata").mkdir(exist_ok=True)
config = configparser.ConfigParser()
if os.path.exists(PATH_FILE):
config.read(PATH_FILE)
else:
config.add_section("prefix")
config.set("prefix", "prefix", "!")
with open(PATH_FILE, "w") as config_file:
config.write(config_file)
try:
_PREFIX = config.get("prefix", "prefix")
except:
_PREFIX = "!"
return _PREFIX
# alias
def load_aliases() -> dict:
try:
if os.path.exists("userdata/command_aliases.json"):
with open("userdata/command_aliases.json", 'r') as f:
return json.load(f)
except Exception:
pass
return {}
# sudo check
async def who_message(client, message):
me = await client.get_me()
if message.from_user.id == me.id:
return message
else:
try:
reply_id = message.reply_to_message.id
except:
reply_id = message.id
return await client.send_message(
message.chat.id,
message.text,
message_thread_id=message.message_thread_id,
reply_parameters=ReplyParameters(message_id=reply_id)
)
# sudo trigger
def fox_sudo():
sudo_file = Path("userdata/sudo_users.json")
sudo_users_list = []
try:
with open(sudo_file, 'r', encoding='utf-8') as f:
sudo_users_list = json.load(f)
i = (filters.user(sudo_users_list) or filters.chat(sudo_users_list))
return i
except:
return filters.me
def fox_command(
command: Union[str, List[str]],
module_name: str,
filename: str,
arguments: str = ""
) -> filters.Filter:
module_name = module_name.replace(" ", "_")
commands = [command] if isinstance(command, str) else command.copy()
aliases = load_aliases()
alias_list = []
for cmd in commands:
for alias, target_cmd in aliases.items():
if target_cmd.startswith(f"{my_prefix()}{cmd}") or target_cmd == cmd:
alias_list.append(alias)
all_commands = commands + alias_list
help_text = " | ".join(f"{my_prefix()}{cmd} {arguments}".strip() for cmd in commands)
if alias_list:
help_text += f" (Aliases: {my_prefix()}{f', {my_prefix()}'.join(alias_list)})"
add_command_help(module_name, help_text)
file_list[module_name] = filename
return filters.command(all_commands, prefixes=my_prefix())
# language
def get_global_lang() -> str:
global _GLOBAL_LANG
if _GLOBAL_LANG is not None:
return _GLOBAL_LANG
lang_config_path = Path("userdata/language.ini")
Path("userdata").mkdir(exist_ok=True)
if lang_config_path.exists():
config = configparser.ConfigParser()
config.read(lang_config_path)
try:
lang = config.get("language", "lang", fallback=default_lang)
lang = lang.lower()
if lang in all_lang:
_GLOBAL_LANG = lang
return _GLOBAL_LANG
except:
pass
_GLOBAL_LANG = default_lang
return _GLOBAL_LANG
def set_global_lang(lang: str) -> bool:
global _GLOBAL_LANG
if lang in all_lang:
_GLOBAL_LANG = lang
lang_config_path = Path("userdata/language.ini")
lang_config_path.parent.mkdir(exist_ok=True)
config = configparser.ConfigParser()
if lang_config_path.exists():
config.read(lang_config_path)
if not config.has_section("language"):
config.add_section("language")
config.set("language", "lang", lang)
with open(lang_config_path, "w") as f:
config.write(f)
return True
return False
def get_module_text(key: str, LANGUAGES: dict, **kwargs) -> str:
lang = get_global_lang()
text = LANGUAGES.get(lang, LANGUAGES["en"]).get(key, key)
text = text.replace("\n", """
""")
if kwargs:
text = text.format(**kwargs)
return text
def get_text(module: str, key: str, LANGUAGES: dict = None, **kwargs) -> str:
if LANGUAGES:
return get_module_text(key, LANGUAGES, **kwargs)
return key
def get_available_langs() -> list:
return all_lang
_ = get_global_lang()