generated from interactions-py/template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
607 lines (553 loc) · 23.5 KB
/
main.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
"""
Main script to run
This script initializes extensions and starts the bot
Copyright (C) 2024 __retr0.init__
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import asyncio
import aiofiles
import os
import sys
import pathlib
import tempfile
import interactions
from interactions.ext.paginators import Paginator
from interactions.client.errors import (
MessageException,
NotFound,
RateLimited,
EmptyMessageException,
InteractionException,
InteractionMissingAccess,
ExtensionLoadException,
ExtensionNotFound,
Forbidden,
HTTPException
)
from dotenv import load_dotenv
'''
The DEV_GUILD must be set to a specific guild_id
'''
from config import DEBUG, DEV_GUILD
from src import logutil, compressutil, moduleutil
from typing import Union, Optional
try:
from icecream import ic
except ImportError: # Graceful fallback if IceCream isn't installed.
ic = lambda *a: None if not a else (a[0] if len(a) == 1 else a) # noqa
ic.disable()
load_dotenv()
# Configure logging for this main.py handler
logger = logutil.init_logger("main.py")
logger.debug(
"Debug mode is %s; This is not a warning, \
just an indicator. You may safely ignore",
DEBUG,
)
def compress_temp(filename: str) -> None:
# tmp = tempfile.NamedTemporaryFile(suffix=".tar.gz", prefix="Discord-Bot-Framework_")
# filename = tmp.name
# tmp.close()
compressutil.compress_directory(pathlib.Path(__file__).parent.resolve(), filename)
# return filename
if not os.environ.get("TOKEN"):
logger.critical("TOKEN variable not set. Cannot continue")
sys.exit(1)
client = interactions.Client(
token=os.environ.get("TOKEN"),
activity=interactions.Activity(
name="with interactions", type=interactions.ActivityType.PLAYING
),
debug_scope=DEV_GUILD,
sync_interactions=True,
sync_ext=True,
intents=interactions.Intents.ALL,
)
# Remove Token from environment variables
os.environ.pop("TOKEN")
# Remove dotenv file (It's copied in the startup script "run.sh")
os.remove(".env")
'''
Check the permission to run the key module command
The ROLE_ID needs to be set in .env file
'''
async def my_check(ctx: interactions.BaseContext):
res: bool = await interactions.is_owner()(ctx)
if os.environ.get("ROLE_ID"):
r: bool = ctx.author.has_role(os.environ.get("ROLE_ID"))
# print(os.environ.get("ROLE_ID"), type(os.environ.get("ROLE_ID")), r, ctx.author.roles)
else:
r: bool = False
return res or r
@interactions.listen()
async def on_startup():
"""Called when the bot starts"""
await client.synchronise_interactions(delete_commands=True)
logger.info(f"Logged in as {client.user}")
################ Kernel functions START ################
kernel_base: interactions.SlashCommand = interactions.SlashCommand(name="kernel", description="Bot Framework Kernel Commands")
kernel_module: interactions.SlashCommand = kernel_base.group(name="module", description="Bot Framework Kernel Module Commands")
kernel_review: interactions.SlashCommand = kernel_base.group(name="review", description="Bot Framework Kernel Review Commands")
dm_messages: dict[str, list[interactions.Message]] = dict()
async def _get_key_members(ctx: interactions.SlashContext) -> list[Union[interactions.Member, interactions.User]]:
"""
Get the list of key members for this bot
"""
role: Optional[interactions.Role] = await ctx.guild.fetch_role(os.environ.get("ROLE_ID")) if os.environ.get("ROLE_ID") else None
key_members: list[Union[interactions.Member, interactions.User]] = [] if role is None else role.members
if client.owner not in key_members:
key_members.append(client.owner)
return key_members
async def _dm_key_members(
ctx: interactions.SlashContext,
msg: Optional[str] = None,
*,
embeds: Optional[list[interactions.Embed]] = None,
components: Optional[list[interactions.ComponentType]] = None,
custom_id: Optional[str] = None
) -> None:
"""
Direct message all key members defined by `ROLE_ID` in .env file and bot owner.
custom_id is used to delete or edit the message later. If not specified, the DM message is not deletable until some components triggered.
"""
key_members: list[Union[interactions.Member, interactions.User]] = await _get_key_members(ctx)
dm_msg: list[interactions.Message] = []
for key_member in key_members:
try:
chan_dm = await key_member.fetch_dm()
_msg_to_send: interactions.Message = await chan_dm.send(content=msg, embeds=embeds, components=components)
except (EmptyMessageException, NotFound, Forbidden, HTTPException) as e:
logger.error(f"DM failed! Error as {e}")
else:
dm_msg.append(_msg_to_send)
if custom_id is not None:
dm_messages[custom_id] = dm_msg
async def _dm_key_members_delete(custom_id: str) -> None:
"""
Delete the direct message sent to key members identified by the custom_id
"""
if custom_id not in dm_messages:
logger.error(f"The direct message indexed by custom_id {custom_id} not exist.")
return
dm_msg: list[interactions.Message] = dm_messages[custom_id]
for msg in dm_msg:
try:
await msg.delete()
except (MessageException, NotFound, Forbidden) as e:
logger.error(f"The direct message has been deleted. Or the other error: {e}")
@kernel_review.subcommand("reboot", sub_cmd_description="Reboot the rebot")
@interactions.check(my_check)
@interactions.max_concurrency(interactions.Buckets.GUILD, 1)
@interactions.cooldown(interactions.Buckets.GUILD, 2, 60)
async def cmd_internal_reboot(ctx: interactions.SlashContext):
await ctx.defer()
executor: interactions.Member = ctx.author
await _dm_key_members(
ctx,
embeds=[interactions.Embed(
title="Bot rebooted",
description=f"{executor.display_name} [{executor.mention}] tries to reboot the bot",
color=interactions.Colour.from_rgb(255, 255, 0),
author=interactions.EmbedAuthor(name=executor.display_name, icon_url=executor.avatar_url),
footer=interactions.EmbedFooter(text=client.user.display_name, icon_url=client.user.avatar_url),
timestamp=interactions.Timestamp.now()
)]
)
await ctx.send(f"Rebooting the bot...")
with open("kernel_flag/reboot", 'a') as f:
f.write(f"Rebooted at {interactions.Timestamp.now().ctime()}\n")
# os.execv(sys.executable, ['python'] + sys.argv)
'''
Load the module from remote HTTPS Git Repository
The scope must be set to a specific guild_id
CC-BY-SA-3.0: https://stackoverflow.com/a/14050282
'''
@kernel_module.subcommand("load", sub_cmd_description="Load module from Git repo with HTTPS")
@interactions.slash_option(
name = "url",
description = "HTTPS URL to module",
required = True,
opt_type = interactions.OptionType.STRING
)
@interactions.check(my_check)
@interactions.cooldown(interactions.Buckets.GUILD, 2, 60)
async def kernel_module_load(ctx: interactions.SlashContext, url: str):
async def _delete_message(mesg: interactions.Message) -> None:
try:
await mesg.delete()
except (MessageException, NotFound, Forbidden) as e:
logger.warn(f"The message cannot be deleted. See error here: {e}")
await ctx.defer()
executor: interactions.Member = ctx.author
await _dm_key_members(
ctx,
embeds=[interactions.Embed(
title="Module Load",
description=f"{executor.display_name} [{executor.mention}] tries to load this module {url}",
color=interactions.Colour.from_rgb(255, 0, 0),
author=interactions.EmbedAuthor(name=executor.display_name, icon_url=executor.avatar_url),
footer=interactions.EmbedFooter(text=client.user.display_name, icon_url=client.user.avatar_url),
url=url,
timestamp=interactions.Timestamp.now()
)]
)
logger.debug("Kernel module load START")
ic()
# Defer the context as the following actions may cost more than 3 seconds
msg: interactions.Message = await ctx.send("Loading new module...")
ic()
# Parse and validate the Git repository url
git_url, parsed, validated = moduleutil.giturl_parse(url)
if not validated:
ic()
await ctx.send("The loaded module is not an HTTPS Git Repo!", ephemeral = True)
await _delete_message(msg)
else:
# Check whether the module extension folder exists
if os.path.isdir(os.path.join(os.getcwd(), "extensions", parsed)):
ic()
await ctx.send(f"The module {parsed} has been loaded!", ephemeral = True)
await _delete_message(msg)
else:
# Clone the git repo
module, clone_validated = moduleutil.gitrepo_clone(git_url)
if not clone_validated:
ic()
logger.warning(f"Module {module} clone failed")
await ctx.send(f"The module {module} clone failed!", ephemeral = True)
await _delete_message(msg)
else:
requirements_path: str = os.path.join(os.getcwd(), "extensions", module, "requirements.txt")
ic(requirements_path)
# Check whether requirements.txt exists in the module repo
if not os.path.exists(requirements_path):
ic()
# If not delete the repo
moduleutil.gitrepo_delete(module)
logger.warning(f"Module {module} requirements.txt does not exist.")
await ctx.send(f"The module {module} does not have `requirements.txt`", ephemeral = True)
await _delete_message(msg)
else:
# pip install -r requirements.txt
success: bool = moduleutil.piprequirements_operate(requirements_path)
if not success:
ic()
logger.warning(f"Module {module} requirements.txt install failed")
await ctx.send(f"Module {module} `requirements.txt` install fail.", ephemeral = True)
await _delete_message(msg)
else:
# Load the module into the kernel
try:
ic()
client.reload_extension(f"extensions.{module}.main")
logger.info(f"Loaded extension extensions.{module}.main")
try:
await msg.edit(content=f"Module `extensions.{module}.main` loaded")
except interactions.errors.Forbidden:
logger.warn("The bot missing permissions to edit the message")
await ctx.send(content=f"Module `extensions.{module}.main` loaded")
except Exception as e:
ic()
logger.exception(f"Failed to load extension {module}.", exc_info=e)
await client.synchronise_interactions(delete_commands=True)
# Delete the repo
moduleutil.gitrepo_delete(module)
await ctx.send(f"Module {module} load fail! The repo is removed.", ephemeral = True)
await _delete_message(msg)
ic()
logger.debug("Kernel module load END")
'''
Kernel module unload / update module option wrapper
'''
def kernel_module_option_module():
def wrapper(func):
return interactions.slash_option(
name = "module",
description = "The name of the loaded module. Check with the list command.",
required = True,
opt_type = interactions.OptionType.STRING,
autocomplete = True
)(func)
return wrapper
'''
Unload the module from kernel
'''
@kernel_module.subcommand("unload", sub_cmd_description="Unload module")
@kernel_module_option_module()
@interactions.check(my_check)
@interactions.cooldown(interactions.Buckets.GUILD, 2, 60)
async def kernel_module_unload(ctx: interactions.SlashContext, module: str):
await ctx.defer()
executor: interactions.Member = ctx.author
info, _ = moduleutil.gitrepo_info(module)
await _dm_key_members(
ctx,
embeds=[interactions.Embed(
title="Module Unload",
description=f"{executor.display_name} [{executor.mention}] tries to unload the module {module}\nIt's at `{info.current_commit.id}` from {info.remote_url}",
color=interactions.Colour.from_rgb(255, 0, 0),
author=interactions.EmbedAuthor(name=executor.display_name, icon_url=executor.avatar_url),
footer=interactions.EmbedFooter(text=client.user.display_name, icon_url=client.user.avatar_url),
timestamp=interactions.Timestamp.now(),
url=info.remote_url
)]
)
try:
client.unload_extension(f"extensions.{module}.main")
await client.synchronise_interactions(delete_commands=True)
except:
await ctx.send(f"Module {module} failed to unload. It will be deleted.", ephemeral=True)
else:
await ctx.send(f"Module {module} unloaded")
finally:
try:
moduleutil.gitrepo_delete(module)
except:
print("The module cannot be deleted")
'''
List all loaded modules in kernel
'''
@kernel_module.subcommand("list", sub_cmd_description="List loaded modules")
async def kernel_module_list(ctx: interactions.SlashContext):
modules_o: list[str] = [i for i in os.listdir("extensions/") if os.path.isdir(f"extensions/{i}") and i != "__pycache__"]
# Check whether the folder is a Git repo
modules: list[str] = [i for i in modules_o if moduleutil.is_gitrepo(i)]
# Join the module list if the list is not empty
if len(modules) > 0:
modules_str: str = '- ' + '\n- '.join(modules)
paginator = Paginator.create_from_string(client, "已加载的模块是\n" + modules_str, page_size=1900)
await paginator.send(ctx)
else:
# There is no module loaded
await ctx.send("没有加载的模块")
'''
Update the loaded module in kernel
'''
@kernel_module.subcommand("update", sub_cmd_description="Update the module")
@kernel_module_option_module()
@interactions.check(my_check)
@interactions.cooldown(interactions.Buckets.GUILD, 2, 60)
async def kernel_module_update(ctx: interactions.SlashContext, module: str):
await ctx.defer()
executor: interactions.Member = ctx.author
info, _ = moduleutil.gitrepo_info(module)
await _dm_key_members(
ctx,
embeds=[interactions.Embed(
title="Module update",
description=f"{executor.display_name} [{executor.mention}] tries to update the module {info.remote_url} from `{info.current_commit.id}` to `{info.remote_head_commit.id}`\nIt's from {info.remote_url}",
color=interactions.Colour.from_rgb(255, 255, 0),
author=interactions.EmbedAuthor(name=executor.display_name, icon_url=executor.avatar_url),
footer=interactions.EmbedFooter(text=client.user.display_name, icon_url=client.user.avatar_url),
timestamp=interactions.Timestamp.now(),
url=info.remote_url
)]
)
# Check whether the module exists in the folder
if not os.path.isdir(f"extensions/{module}"):
await ctx.send("The extension {module} does not exist!", ephemeral=True)
return
# Update the repo
err: int = moduleutil.gitrepo_pull(module)
# Return if the module is NOT a Git repo or updating failed
if err != 0:
reason: list[str] = [
"Not a git repo",
"Remote repo fetch failed",
"`master` branch does not exist"
]
await ctx.send("Module update failed! The reason is: {}".format(reason[err - 1]), ephemeral=True)
return
# Install requirements.txt
requirements_path: str = f"{os.getcwd()}/extensions/{module}/requirements.txt"
if not os.path.exists(requirements_path):
await ctx.send("`requirements.txt` does not exist! No reloading!", ephemeral=True)
return
moduleutil.piprequirements_operate(requirements_path)
# Reload module
client.reload_extension(f"extensions.{module}.main")
# Synchronise the slash command
await client.synchronise_interactions(delete_commands=True)
# Check CHANGELOG
changelog_path: str = f"{os.getcwd()}/extensions/{module}/CHANGELOG"
if os.path.isfile(changelog_path):
async with aiofiles.open(changelog_path) as f:
cl: str = await f.read()
else:
cL: str = "CHANGELOG not provided!"
paginator = Paginator.create_from_string(client, f"Module `{module}` updated!\n# CHANGELOG:\n\n{cl}\n", page_size=1900)
await paginator.send(ctx)
'''
Show the module information
'''
@kernel_module.subcommand("info", sub_cmd_description="Show the module info")
@kernel_module_option_module()
async def kernel_module_info(ctx: interactions.SlashContext, module: str):
await ctx.defer()
info, valid = moduleutil.gitrepo_info(module)
if not valid:
await ctx.send("The module does not exist!", ephemeral=True)
return
changelogs: list[str] = []
for line in info.CHANGELOG.splitlines(keepends=True):
if len(changelogs) == 0:
changelogs.append(line)
elif len(changelogs[len(changelogs) - 1]) + len(line) < 3500:
changelogs[len(changelogs) - 1] += line
else:
changelogs.append(line)
if len(changelogs) == 0:
changelogs.append("")
embed: interactions.Embed = interactions.Embed(
title = "Module Information",
description = f'''### {module}
### No Local Changes? {'❌' if info.modifications > 0 else '✅'}
### Current commit
- ID: `{info.current_commit.id}`
- Time: `{info.get_UTC_time()}`
### Remote HEAD commit
- ID: `{info.remote_head_commit.id}`
- Time: `{info.get_remote_UTC_time()}`
### CHANGELOG
```
{changelogs[0]}
```
''',
color = interactions.Color.from_rgb(255, 0, 0) if info.modifications > 0 else interactions.Color.from_rgb(0, 255, 0),
url = info.remote_url
)
embeds: list[interactions.Embed] = [embed]
changelogs.pop(0)
embeds.extend([interactions.Embed(
title = "Module Changelog",
description = f'''
```
{changelog}
```
''',
color = interactions.Color.from_rgb(255, 0, 0) if info.modifications > 0 else interactions.Color.from_rgb(0, 255, 0),
url = info.remote_url) for changelog in changelogs])
paginator = Paginator.create_from_embeds(client, *embeds)
await paginator.send(ctx)
'''
Autocomplete function for the kernel module unloading and update commands
'''
@kernel_module_unload.autocomplete("module")
@kernel_module_update.autocomplete("module")
@kernel_module_info.autocomplete("module")
async def kernel_module_option_module_autocomplete(ctx: interactions.AutocompleteContext):
module_option_input: str = ctx.input_text
modules: list[str] = [
i
for i in os.listdir("extensions")
if os.path.isdir(f"extensions/{i}") and i != "__pycache__" and moduleutil.is_gitrepo(i)
]
modules_auto: list[str] = [
i for i in modules if module_option_input in i
]
await ctx.send(
choices = [
{
"name": i,
"value": i,
} for i in modules_auto
]
)
# Global variable to determine whether the download is in progress
gDownloading: bool = False
'''
Download the running code in tarball (.tar.gz)
'''
@kernel_review.subcommand("download", sub_cmd_description="Download current running code in tarball")
@interactions.max_concurrency(interactions.Buckets.GUILD, 2)
@interactions.cooldown(interactions.Buckets.GUILD, 2, 60)
async def kernel_review_download(ctx: interactions.SlashContext):
global gDownloading
if gDownloading:
await ctx.send("There is already a download task running! Please run it later :)", ephemeral=True)
return
gDownloading = True
await ctx.defer()
with tempfile.NamedTemporaryFile(suffix=".tar.gz", prefix="Discord-Bot-Framework_") as f:
compress_temp(f.name)
await ctx.send("Current code that is running as attached", file=f.name)
gDownloading = False
'''
Show Kernel information
'''
@kernel_review.subcommand("info", sub_cmd_description="Show the Kernel information")
async def kernel_review_info(ctx: interactions.SlashContext):
info = moduleutil.kernel_gitrepo_info()
embed: interactions.Embed = interactions.Embed(
title = "Kernel Information",
description = f'''### Discord-Bot-Framework-Kernel
### No Local Changes? {'❌' if info.modifications > 0 else '✅'}
### Current commit
- ID: `{info.current_commit.id}`
- Time: `{info.get_UTC_time()}`
### Remote HEAD commit
- ID: `{info.remote_head_commit.id}`
- Time: `{info.get_remote_UTC_time()}`
''',
color = interactions.Color.from_rgb(255, 0, 0) if info.modifications > 0 else interactions.Color.from_rgb(0, 255, 0),
url = info.remote_url
)
await ctx.send(embed=embed)
'''
Update the kernel itself
'''
@kernel_review.subcommand("update", sub_cmd_description="Update the kernel")
@interactions.max_concurrency(interactions.Buckets.GUILD, 1)
async def kernel_review_update(ctx: interactions.SlashContext):
await ctx.defer()
# Pull the changes
err: int = moduleutil.kernel_gitrepo_pull()
# Return if the module is NOT a Git repo or updating failed
if err != 0:
reason: list[str] = [
"Not a git repo",
"Remote repo fetch failed",
"`master` branch does not exist"
]
await ctx.send("Module update failed! The reason is: {}".format(reason[err - 1]), ephemeral=True)
return
# Install requirements.txt
requirements_path: str = f"{os.getcwd()}/requirements.txt"
if not os.path.exists(requirements_path):
await ctx.send("`requirements.txt` does not exist! No reloading!", ephemeral=True)
return
moduleutil.piprequirements_operate(requirements_path)
await ctx.send("Kernel update complete! Please restart the bot!")
################ Kernel functions END ################
async def main_main():
# get all python files in "extensions" folder
extensions = [
f"extensions.{f[:-3]}"
for f in os.listdir("extensions")
if f.endswith(".py") and not f.startswith("_")
] + [
f"extensions.{i}.main"
for i in os.listdir("extensions")
if os.path.isdir(f"extensions/{i}") and i != "__pycache__" and moduleutil.is_gitrepo(i)
]
try:
client.load_extension("interactions.ext.jurigged")
except interactions.errors.ExtensionLoadException as e:
logger.exception(f"Failed to load extension {extension}.", exc_info=e)
for extension in extensions:
try:
client.load_extension(extension)
logger.info(f"Loaded extension {extension}")
except interactions.errors.ExtensionLoadException as e:
logger.exception(f"Failed to load extension {extension}.", exc_info=e)
await client.astart()
asyncio.run(main_main())