-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathextension.py
More file actions
275 lines (236 loc) · 9.34 KB
/
extension.py
File metadata and controls
275 lines (236 loc) · 9.34 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
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
"""
Commands which allow control over loaded extensions
The cog in the file is named:
ExtensionControl
This file contains 4 commands:
.extension status
.extension load
.extension unload
.extension register
"""
from __future__ import annotations
import json
from typing import TYPE_CHECKING, Self
import discord
import ui
from core import auxiliary, cogs
from discord import app_commands
from discord.ext import commands
if TYPE_CHECKING:
import bot
async def setup(bot: bot.TechSupportBot) -> None:
"""Loading the Extension Control plugin into the bot
Args:
bot (bot.TechSupportBot): The bot object to register the cogs to
"""
await bot.add_cog(ExtensionControl(bot=bot))
class ExtensionControl(cogs.BaseCog):
"""
The class that holds the extension commands
Attributes:
extension_app_command_group (app_commands.Group): The group for the /extension commands
"""
extension_app_command_group: app_commands.Group = app_commands.Group(
name="extension", description="...", extras={"module": "extension"}
)
@extension_app_command_group.command(
name="list_disabled",
description="Lists all disabled extensions in the current server",
extras={"module": "extension"},
)
async def list_disabled(self: Self, interaction: discord.Interaction) -> None:
"""This will read the current guild config and list all the
extensions that are currently disabled
Args:
interaction (discord.Interaction): The interaction that triggered the slash command
"""
config = self.bot.guild_configs[str(interaction.guild.id)]
missing_extensions = [
item
for item in self.bot.extension_name_list
if item not in config.enabled_extensions
]
if len(missing_extensions) == 0:
embed = auxiliary.prepare_confirm_embed(
message="No currently loaded extensions are disabled"
)
else:
embed = auxiliary.prepare_confirm_embed(
message=f"Disabled extensions: {missing_extensions}"
)
await interaction.response.send_message(embed=embed)
@app_commands.checks.has_permissions(administrator=True)
@extension_app_command_group.command(
name="enable_all",
description="Enables all loaded but disabled extensions in the guild",
extras={"module": "extension"},
)
async def enable_everything(self: Self, interaction: discord.Interaction) -> None:
"""This will get all the disabled extensions and enable them for the current
guild.
Args:
interaction (discord.Interaction): The interaction that triggered the slash command
"""
config = self.bot.guild_configs[str(interaction.guild.id)]
missing_extensions = [
item
for item in self.bot.extension_name_list
if item not in config.enabled_extensions
]
if len(missing_extensions) == 0:
embed = auxiliary.prepare_confirm_embed(
message="No currently loaded extensions are disabled"
)
else:
for extension in missing_extensions:
config.enabled_extensions.append(extension)
config.enabled_extensions.sort()
# Modify the database
await self.bot.write_new_config(
str(interaction.guild.id), json.dumps(config)
)
# Modify the local cache
self.bot.guild_configs[str(interaction.guild.id)] = config
embed = auxiliary.prepare_confirm_embed(
f"I have enabled {len(missing_extensions)} for this guild."
)
await interaction.response.send_message(embed=embed)
@commands.check(auxiliary.bot_admin_check_context)
@commands.group(
name="extension",
brief="Executes an extension bot command",
description="Executes an extension bot command",
)
async def extension_group(self: Self, ctx: commands.Context) -> None:
"""The bare .extension command. This does nothing but generate the help message
Args:
ctx (commands.Context): The context in which the command was run in
"""
return
@auxiliary.with_typing
@extension_group.command(
name="status",
description="Gets the status of an extension by name",
usage="[extension-name]",
)
async def extension_status(
self: Self, ctx: commands.Context, *, extension_name: str
) -> None:
"""Gets the status of an extension.
This is a command and should be accessed via Discord.
Args:
ctx (commands.Context): the context object for the message
extension_name (str): the name of the extension
"""
extensions_status = (
"loaded"
if ctx.bot.extensions.get(
f"{self.bot.EXTENSIONS_DIR_NAME}.{extension_name}"
)
else "unloaded"
)
functions_status = (
"loaded"
if ctx.bot.extensions.get(f"{self.bot.FUNCTIONS_DIR_NAME}.{extension_name}")
else "unloaded"
)
embed = discord.Embed(
title=f"Extension status for `{extension_name}`",
description=f"Extension: {extensions_status}\nFunction: {functions_status}",
)
if functions_status == "loaded" or extensions_status == "loaded":
embed.color = discord.Color.green()
else:
embed.color = discord.Color.gold()
await ctx.send(embed=embed)
@auxiliary.with_typing
@extension_group.command(
name="load", description="Loads an extension by name", usage="[extension-name]"
)
async def load_extension(
self: Self, ctx: commands.Context, *, extension_name: str
) -> None:
"""Loads an extension by filename.
This is a command and should be accessed via Discord.
Args:
ctx (commands.Context): the context object for the message
extension_name (str): the name of the extension
"""
try:
await ctx.bot.load_extension(f"functions.{extension_name}")
except (ModuleNotFoundError, commands.errors.ExtensionNotFound):
await ctx.bot.load_extension(f"commands.{extension_name}")
await auxiliary.send_confirm_embed(
message="I've loaded that extension", channel=ctx.channel
)
@auxiliary.with_typing
@extension_group.command(
name="unload",
description="Unloads an extension by name",
usage="[extension-name]",
)
async def unload_extension(
self: Self, ctx: commands.Context, *, extension_name: str
) -> None:
"""Unloads an extension by filename.
This is a command and should be accessed via Discord.
Args:
ctx (commands.Context): the context object for the message
extension_name (str): the name of the extension
"""
try:
await ctx.bot.unload_extension(f"functions.{extension_name}")
except commands.errors.ExtensionNotLoaded:
await ctx.bot.unload_extension(f"commands.{extension_name}")
await auxiliary.send_confirm_embed(
message="I've unloaded that extension", channel=ctx.channel
)
@auxiliary.with_typing
@extension_group.command(
name="register",
description="Uploads an extension from Discord to be saved on the bot",
usage="[extension-name] |python-file-upload|",
)
async def register_extension(
self: Self, ctx: commands.Context, extension_name: str
) -> None:
"""Unloads an extension by filename.
This is a command and should be accessed via Discord.
Args:
ctx (commands.Context): the context object for the message
extension_name (str): the name of the extension
"""
if not ctx.message.attachments:
await auxiliary.send_deny_embed(
message="You did not provide a Python file upload", channel=ctx.channel
)
return
attachment = ctx.message.attachments[0]
if not attachment.filename.endswith(".py"):
await auxiliary.send_deny_embed(
message="I don't recognize your upload as a Python file",
channel=ctx.channel,
)
return
if extension_name.lower() in await self.bot.get_potential_extensions():
view = ui.Confirm()
await view.send(
message=f"Warning! This will replace the current `{extension_name}.py` "
+ "extension! Are you SURE?",
channel=ctx.channel,
author=ctx.author,
)
await view.wait()
if view.value is ui.ConfirmResponse.TIMEOUT:
return
if view.value is ui.ConfirmResponse.DENIED:
await auxiliary.send_deny_embed(
message=f"{extension_name}.py was not replaced", channel=ctx.channel
)
return
fp = await attachment.read()
await self.bot.register_file_extension(extension_name, fp)
await auxiliary.send_confirm_embed(
message="I've registered that extension. You can now try loading it",
channel=ctx.channel,
)