File size: 12,876 Bytes
78b07ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import importlib
import os
import sys
from pathlib import Path

from pyrogram import Client
from pyrogram.enums import MessagesFilter, ParseMode
from pyrogram.types import Message

from Hellbot.core import ENV, Config, Symbols

from . import HelpMenu, bot, db, handler, hellbot, on_message


@on_message("help", allow_stan=True)
async def help(client: Client, message: Message):
    hell = await hellbot.edit(message, "**Processing...**")
    if len(message.command) == 1:
        try:
            result = await client.get_inline_bot_results(bot.me.username, "help_menu")
            await client.send_inline_bot_result(
                message.chat.id,
                result.query_id,
                result.results[0].id,
                True,
            )
            return await hell.delete()
        except Exception as e:
            await hellbot.error(hell, str(e), 20)
            return

    plugin = await hellbot.input(message)
    if plugin.lower() in Config.CMD_MENU:
        try:
            await hellbot.edit(
                hell, Config.CMD_MENU[plugin.lower()], ParseMode.MARKDOWN
            )
            return
        except Exception as e:
            await hellbot.error(hell, str(e), 20)
            return

    available_plugins = f"{Symbols.bullet} **𝖠𝗏𝖺𝗂𝗅𝖺𝖻𝗅𝖾 π—‰π—…π—Žπ—€π—‚π—‡π—Œ:**\n\n"
    for i in sorted(list(Config.CMD_MENU.keys())):
        available_plugins += f"`{i}`, "
    available_plugins = available_plugins[:-2]
    available_plugins += (
        f"\n\nπ–£π—ˆ `{handler}help <plugin name>` π—π—ˆ 𝗀𝖾𝗍 𝖽𝖾𝗍𝖺𝗂𝗅𝖾𝖽 π—‚π—‡π–Ώπ—ˆ π—ˆπ–Ώ 𝗍𝗁𝖺𝗍 π—‰π—…π—Žπ—€π—‚π—‡."
    )
    await hellbot.edit(hell, available_plugins, ParseMode.MARKDOWN)


@on_message("repo", allow_stan=True)
async def repo(_, message: Message):
    REPO_TEXT = (
        "__πŸ€ π–±π–Ύπ—‰π—ˆ:__ [Github](https://github.com/The-HellBot/HellBot)\n\n"
        "__πŸ€ Updates:__ @Its_HellBot\n"
        "__πŸ€ Support:__ @HellBot_Chats\n\n"
        "**By ©️ @HellBot_Networks**"
    )
    await hellbot.edit(message, REPO_TEXT, no_link_preview=True)


@on_message("plinfo", allow_stan=True)
async def plugin_info(_, message: Message):
    plugin = await hellbot.input(message)
    if plugin.lower() in Config.CMD_MENU:
        try:
            await hellbot.edit(
                message, Config.CMD_MENU[plugin.lower()], ParseMode.MARKDOWN
            )
            return
        except Exception as e:
            await hellbot.error(message, str(e), 20)
            return
    await hellbot.error(message, f"**Invalid Plugin Name:** `{plugin}`", 20)


@on_message("cmdinfo", allow_stan=True)
async def command_info(_, message: Message):
    cmd = await hellbot.input(message)
    if cmd.lower() in Config.CMD_INFO:
        try:
            cmd_dict = Config.CMD_INFO[cmd.lower()]
            template = (
                f"**πŸ€ π–―π—…π—Žπ—€π—‚π—‡:** `{cmd_dict['plugin']}.py`\n\n"
                f"**{Symbols.anchor} π–’π—ˆπ—†π—†π–Ίπ—‡π–½:** `{cmd_dict['command']}`\n"
                f"**{Symbols.anchor} π–£π–Ύπ—Œπ–Όπ—‹π—‚π—‰π—π—‚π—ˆπ—‡:** __{cmd_dict['description']}__\n"
                f"**{Symbols.anchor} 𝖀𝗑𝖺𝗆𝗉𝗅𝖾:** `{cmd_dict['example']}`\n"
                f"**{Symbols.anchor} π–­π—ˆπ—π–Ύ:** __{cmd_dict['note']}__\n"
            )
            await hellbot.edit(message, template, ParseMode.MARKDOWN)
            return
        except Exception as e:
            await hellbot.error(message, str(e), 20)
            return
    await hellbot.error(message, f"**Invalid Command Name:** `{cmd}`", 20)


@on_message("send", allow_stan=True)
async def send_plugin(client: Client, message: Message):
    if len(message.command) < 2:
        return await hellbot.delete(message, "Give me a plugin name to send.")

    plugin = message.command[1].lower().replace(".py", "").strip()
    if plugin not in Config.CMD_MENU:
        return await hellbot.delete(message, f"**Invalid Plugin Name:** `{plugin}`")

    try:
        await client.send_document(
            message.chat.id,
            f"./Hellbot/plugins/user/{plugin}.py",
            caption=f"**πŸ€ π–―π—…π—Žπ—€π—‚π—‡:** `{plugin}.py`",
        )
        await hellbot.delete(message, f"**Sent:** `{plugin}.py`")
    except Exception as e:
        await hellbot.error(message, str(e), 20)


@on_message("install", allow_stan=True)
async def install_plugins(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.document:
        return await hellbot.delete(message, "Reply to a plugin to install it.")

    hell = await hellbot.edit(message, "**Installing...**")
    plugin_path = await message.reply_to_message.download("./Hellbot/plugins/user/")

    if not plugin_path.endswith(".py"):
        os.remove(plugin_path)
        return await hellbot.error(hell, "**Invalid Plugin:** Not a python file.", 20)

    plugin = plugin_path.split("/")[-1].replace(".py", "").strip()
    if plugin in Config.CMD_MENU:
        os.remove(plugin_path)
        return await hellbot.error(
            hell, f"**Plugin Already Installed:** `{plugin}.py`", 20
        )

    if "(" in plugin:
        os.remove(plugin_path)
        return await hellbot.error(
            hell, f"**Plugin Already Installed:** `{plugin}.py`", 20
        )

    try:
        shortname = Path(plugin_path).stem.replace(".py", "")
        path = Path(f"Hellbot/plugins/user/{shortname}.py")
        name = "Hellbot.plugins.user." + shortname
        spec = importlib.util.spec_from_file_location(name, path)
        load = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(load)
        sys.modules["Hellbot.plugins.user." + shortname] = load
        await hellbot.edit(hell, f"**Installed:** `{plugin}.py`")
    except Exception as e:
        await hellbot.error(hell, str(e), 20)
        os.remove(plugin_path)


@on_message("uninstall", allow_stan=True)
async def uninstall_plugins(_, message: Message):
    if len(message.command) < 2:
        return await hellbot.delete(message, "Give me a plugin name to uninstall.")

    plugin = message.command[1].lower().replace(".py", "").strip()
    if plugin not in Config.CMD_MENU:
        return await hellbot.delete(message, f"**Invalid Plugin Name:** `{plugin}`")

    try:
        os.remove(f"./Hellbot/plugins/user/{plugin}.py")
        for i in Config.HELP_DICT[plugin]["commands"]:
            cmd = i["command"]
            for i in hellbot.users:
                i.remove_handler(cmd)
            del Config.CMD_INFO[cmd]
        del Config.HELP_DICT[plugin]
        del Config.CMD_MENU[plugin]
        await hellbot.delete(message, f"**Uninstalled:** `{plugin}.py`")
    except Exception as e:
        await hellbot.error(message, str(e), 20)


@on_message("installall", allow_stan=True)
async def installall(client: Client, message: Message):
    if len(message.command) < 2:
        return await hellbot.delete(
            message, "Give me channel username to install plugins."
        )

    try:
        chat = await client.get_chat(message.command[1])
    except Exception as e:
        return await hellbot.delete(message, f"**Invalid Channel Username:** `{e}`")

    hell = await hellbot.edit(message, f"**Installing plugins from {chat.title}...**")
    finalStr = f"{Symbols.check_mark} **π–―π—…π—Žπ—€π—‚π—‡π—Œ π–¨π—‡π—Œπ—π–Ίπ—…π—…π–Ύπ–½: {chat.title}**\n\n"
    count = 0

    async for msg in client.search_messages(chat.id, filter=MessagesFilter.DOCUMENT):
        if msg.document.file_name.endswith(".py"):
            dwl_path = await msg.download("./Hellbot/plugins/user/")
            plugin = dwl_path.split("/")[-1].replace(".py", "").strip()
            if plugin in Config.CMD_MENU:
                os.remove(dwl_path)
                finalStr += (
                    f"   {Symbols.anchor} `{plugin}.py` - **𝖠𝗅𝗋𝖾𝖺𝖽𝗒 π–¨π—‡π—Œπ—π–Ίπ—…π—…π–Ύπ–½!**\n"
                )
                continue
            if "(" in plugin:
                os.remove(dwl_path)
                finalStr += (
                    f"   {Symbols.anchor} `{plugin}.py` - **𝖠𝗅𝗋𝖾𝖺𝖽𝗒 π–¨π—‡π—Œπ—π–Ίπ—…π—…π–Ύπ–½!**\n"
                )
                continue
            try:
                shortname = Path(dwl_path).stem.replace(".py", "")
                path = Path(f"Hellbot/plugins/user/{shortname}.py")
                name = "Hellbot.plugins.user." + shortname
                spec = importlib.util.spec_from_file_location(name, path)
                load = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(load)
                sys.modules["Hellbot.plugins.user." + shortname] = load
                count += 1
                finalStr += f"   {Symbols.anchor} `{plugin}.py` - **π–¨π—‡π—Œπ—π–Ίπ—…π—…π–Ύπ–½!**\n"
            except Exception as e:
                os.remove(dwl_path)
                finalStr += (
                    f"   {Symbols.anchor} `{plugin}.py` - **π–€π—‹π—‹π—ˆπ—‹ π–¨π—‡π—Œπ—π–Ίπ—…π—…π—‚π—‡π—€!**\n"
                )
                continue

    finalStr += f"\n**πŸ€ π–³π—ˆπ—π–Ίπ—… π–―π—…π—Žπ—€π—‚π—‡π—Œ π–¨π—‡π—Œπ—π–Ίπ—…π—…π–Ύπ–½:** `{count}`"
    await hell.edit(finalStr, ParseMode.MARKDOWN, disable_web_page_preview=True)


@on_message("unload", allow_stan=True)
async def unload_plugins(_, message: Message):
    if len(message.command) < 2:
        return await hellbot.delete(message, "Give me a plugin name to unload.")

    plugin = message.command[1].lower().replace(".py", "").strip()
    if plugin not in Config.CMD_MENU:
        return await hellbot.delete(message, f"**Invalid Plugin Name:** `{plugin}`")

    unloaded = await db.get_env(ENV.unload_plugins) or ""
    unloaded = unloaded.split(" ")
    if plugin in unloaded:
        return await hellbot.delete(message, f"**Already Unloaded:** `{plugin}.py`")

    unloaded.append(plugin)
    await db.set_env(ENV.unload_plugins, " ".join(unloaded))
    await hellbot.delete(
        message, f"**Unloaded:** `{plugin}.py` \n\n__Restart the bot to see changes.__"
    )


@on_message("load", allow_stan=True)
async def load_plugins(_, message: Message):
    if len(message.command) < 2:
        return await hellbot.delete(message, "Give me a plugin name to load.")

    plugin = message.command[1].lower().replace(".py", "").strip()
    unloaded = await db.get_env(ENV.unload_plugins) or ""
    unloaded = unloaded.split(" ")
    if plugin not in unloaded:
        return await hellbot.delete(message, f"**Already Loaded:** `{plugin}.py`")

    unloaded.remove(plugin)
    await db.set_env(ENV.unload_plugins, " ".join(unloaded))
    await hellbot.delete(
        message, f"**Loaded:** `{plugin}.py` \n\n__Restart the bot to see changes.__"
    )


HelpMenu("help").add(
    "help",
    "<plugin name>",
    "Get the detailed help menu for that mentioned plugin or get the whole help menu instead.",
    "help alive",
).add("repo", None, "Get the repo link of the bot.", "repo").add(
    "plinfo",
    "<plugin name>",
    "Get the detailed info of the mentioned plugin.",
    "plinfo alive",
).add(
    "cmdinfo",
    "<command name>",
    "Get the detailed info of the mentioned command.",
    "cmdinfo alive",
).add(
    "send", "<plugin name>", "Send the mentioned plugin.", "send alive"
).add(
    "install",
    "<reply to plugin>",
    "Install the replied plugin.",
    None,
    "Do not install plugins from untrusted sources, they can be a malware. We're not responsible for any damage caused by them.",
).add(
    "uninstall",
    "<plugin name>",
    "Uninstall the mentioned plugin.",
    "uninstall alive",
    "This will remove all the commands of that plugin from the bot till a restart is initiated.",
).add(
    "installall",
    "<channel username>",
    "Install all the plugins from the mentioned channel.",
    "installall @plugins_for_hellbot",
    "Do not install plugins from untrusted sources, they can be a malware. We're not responsible for any damage caused by them.",
).add(
    "unload",
    "<plugin name>",
    "Unload the mentioned plugin.",
    "unload alive",
    "This will remove all the commands of that plugin from the bot permanently.",
).add(
    "load",
    "<plugin name>",
    "Load the mentioned plugin.",
    "load alive",
    "This will load all the commands of that plugin to the bot that was previously unloaded permanently.",
)