|
| 1 | +import dc_interactions as dc |
| 2 | +from os import environ as env |
| 3 | +from xenon.cmd import * |
| 4 | +import inspect |
| 5 | +import textwrap |
| 6 | +import traceback |
| 7 | +import json |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +ADMIN_GUILD_ID = env.get("ADMIN_GUILD_ID", "496683369665658880") |
| 11 | + |
| 12 | + |
| 13 | +class AdminModule(dc.Module): |
| 14 | + @dc.Module.command(guild_id=ADMIN_GUILD_ID) |
| 15 | + async def admin(self, ctx): |
| 16 | + """ |
| 17 | + Manage the admin commands for a server |
| 18 | + """ |
| 19 | + |
| 20 | + @admin.sub_command() |
| 21 | + @checks.is_bot_owner |
| 22 | + async def enable(self, ctx, guild_id=None): |
| 23 | + """ |
| 24 | + Add the admin commands to a server |
| 25 | + """ |
| 26 | + guild_id = guild_id or ctx.guild_id |
| 27 | + await ctx.ack_with_source() |
| 28 | + |
| 29 | + for command in self.commands: |
| 30 | + if command.register: |
| 31 | + continue |
| 32 | + |
| 33 | + await self.bot.create_command(command, guild_id=guild_id) |
| 34 | + |
| 35 | + await ctx.respond_with_source(**create_message( |
| 36 | + f"Admin commands are now available on the server with the id `{guild_id}`", |
| 37 | + f=Format.SUCCESS |
| 38 | + )) |
| 39 | + |
| 40 | + @admin.sub_command() |
| 41 | + @checks.is_bot_owner |
| 42 | + async def disable(self, ctx, guild_id=None): |
| 43 | + """ |
| 44 | + Remove the admin commands from a server |
| 45 | + """ |
| 46 | + guild_id = guild_id or ctx.guild_id |
| 47 | + await ctx.ack_with_source() |
| 48 | + |
| 49 | + existing_commands = await ctx.bot.fetch_commands(guild_id=guild_id) |
| 50 | + for command in self.commands: |
| 51 | + if command.register: |
| 52 | + continue |
| 53 | + |
| 54 | + for ex_cmd in existing_commands: |
| 55 | + if ex_cmd["name"] == command.name: |
| 56 | + await self.bot.delete_command(ex_cmd["id"], guild_id=guild_id) |
| 57 | + |
| 58 | + await ctx.respond_with_source(**create_message( |
| 59 | + f"Admin commands are no longer available on the server with the id `{guild_id}`", |
| 60 | + f=Format.SUCCESS |
| 61 | + )) |
| 62 | + |
| 63 | + @dc.Module.command(register=False) |
| 64 | + @checks.is_bot_owner |
| 65 | + async def maintenance(self, ctx): |
| 66 | + """ |
| 67 | + Enable or disable the maintenance mode |
| 68 | + """ |
| 69 | + current = await self.bot.redis.exists("cmd:maintenance") |
| 70 | + if current: |
| 71 | + await self.bot.redis.delete("cmd:maintenance") |
| 72 | + await ctx.respond_with_source(**create_message( |
| 73 | + "**Disabled maintenance** mode.", |
| 74 | + f=Format.SUCCESS |
| 75 | + )) |
| 76 | + |
| 77 | + else: |
| 78 | + await self.bot.redis.set("cmd:maintenance", "1") |
| 79 | + await ctx.respond_with_source(**create_message( |
| 80 | + "**Enabled maintenance** mode.", |
| 81 | + f=Format.SUCCESS |
| 82 | + )) |
| 83 | + |
| 84 | + @dc.Module.command(register=False) |
| 85 | + async def morph(self, ctx, user_id=None): |
| 86 | + """ |
| 87 | + Morph into and execute commands as a different user |
| 88 | + """ |
| 89 | + if user_id is None: |
| 90 | + morph_source = getattr(ctx.payload, "morph_source", None) |
| 91 | + if morph_source is None: |
| 92 | + await ctx.respond_with_source(**create_message( |
| 93 | + "You are already executing commands as yourself.", |
| 94 | + f=Format.SUCCESS |
| 95 | + )) |
| 96 | + return |
| 97 | + |
| 98 | + await ctx.bot.redis.delete(f"cmd:morph:{morph_source}") |
| 99 | + await ctx.respond_with_source(**create_message( |
| 100 | + "You are now executing commands as yourself.", |
| 101 | + f=Format.SUCCESS |
| 102 | + )) |
| 103 | + return |
| 104 | + |
| 105 | + check_result = await checks.is_bot_owner.run(ctx) |
| 106 | + if check_result is not True: |
| 107 | + return |
| 108 | + |
| 109 | + await ctx.bot.redis.set(f"cmd:morph:{ctx.author.id}", user_id) |
| 110 | + await ctx.respond_with_source(**create_message( |
| 111 | + f"You are now executing commands as <@{user_id}>.", |
| 112 | + f=Format.SUCCESS |
| 113 | + )) |
| 114 | + |
| 115 | + @dc.Module.command(register=False) |
| 116 | + @checks.is_bot_owner |
| 117 | + async def eval(self, ctx, expression): |
| 118 | + """ |
| 119 | + Evaluate a python expression |
| 120 | + """ |
| 121 | + if expression.startswith("await "): |
| 122 | + expression = expression[6:] |
| 123 | + |
| 124 | + try: |
| 125 | + result = eval(expression) |
| 126 | + if inspect.isawaitable(result): |
| 127 | + result = await result |
| 128 | + |
| 129 | + except Exception as e: |
| 130 | + tb = "".join(traceback.format_exception(type(e), e, e.__traceback__)) |
| 131 | + await ctx.respond_with_source(**create_message( |
| 132 | + f"```py\n{tb[:1900]}```", |
| 133 | + title="Eval Error", |
| 134 | + f=Format.SUCCESS |
| 135 | + )) |
| 136 | + |
| 137 | + else: |
| 138 | + await ctx.respond_with_source(**create_message( |
| 139 | + f"```py\n{result}```", |
| 140 | + title="Eval Result", |
| 141 | + f=Format.SUCCESS |
| 142 | + )) |
| 143 | + |
| 144 | + @dc.Module.command(register=False) |
| 145 | + @checks.is_bot_owner |
| 146 | + async def exec(self, ctx, snippet): |
| 147 | + """ |
| 148 | + Execute a python code snippet |
| 149 | + """ |
| 150 | + if snippet.startswith('```') and snippet.endswith('```'): |
| 151 | + snippet = '\n'.join(snippet.split('\n')[1:-1]) |
| 152 | + |
| 153 | + snippet = snippet.strip("` \n") |
| 154 | + wrapped = f"async def func():\n{textwrap.indent(snippet, ' ')}" |
| 155 | + |
| 156 | + env = { |
| 157 | + "ctx": ctx, |
| 158 | + "self": self, |
| 159 | + "bot": ctx.bot, |
| 160 | + "http": ctx.bot.http, |
| 161 | + "redis": ctx.bot.redis |
| 162 | + } |
| 163 | + |
| 164 | + try: |
| 165 | + exec(wrapped, env) |
| 166 | + result = await env["func"]() |
| 167 | + |
| 168 | + except Exception as e: |
| 169 | + tb = "".join(traceback.format_exception(type(e), e, e.__traceback__)) |
| 170 | + await ctx.respond_with_source(**create_message( |
| 171 | + f"```py\n{tb[:1900]}```", |
| 172 | + title="Exec Error", |
| 173 | + f=Format.SUCCESS |
| 174 | + )) |
| 175 | + |
| 176 | + else: |
| 177 | + await ctx.respond_with_source(**create_message( |
| 178 | + f"```py\n{result}```", |
| 179 | + title="Exec Result", |
| 180 | + f=Format.SUCCESS |
| 181 | + )) |
| 182 | + |
| 183 | + @dc.Module.command(register=False) |
| 184 | + @checks.is_bot_owner |
| 185 | + async def redis(self, ctx, cmd): |
| 186 | + """ |
| 187 | + Execute a redis command |
| 188 | + """ |
| 189 | + result = await ctx.bot.redis.execute(*cmd.split(" ")) |
| 190 | + await ctx.respond_with_source(**create_message( |
| 191 | + f"```py\n{result}\n```", |
| 192 | + title="Redis Result", |
| 193 | + f=Format.SUCCESS |
| 194 | + )) |
| 195 | + |
| 196 | + @dc.Module.command(register=False) |
| 197 | + @checks.is_bot_owner |
| 198 | + async def error(self, ctx, error_id: str.lower): |
| 199 | + """ |
| 200 | + Show information about a command error |
| 201 | + """ |
| 202 | + error = await ctx.bot.redis.get(f"cmd:errors:{error_id}") |
| 203 | + if error is None: |
| 204 | + await ctx.respond_with_source(**create_message( |
| 205 | + f"**Unknown error** with the id `{error_id.upper()}`.", |
| 206 | + f=Format.ERROR |
| 207 | + )) |
| 208 | + return |
| 209 | + |
| 210 | + data = json.loads(error) |
| 211 | + embeds = [{ |
| 212 | + "title": "Command Error", |
| 213 | + "color": Format.ERROR.color, |
| 214 | + "fields": [ |
| 215 | + { |
| 216 | + "name": "Command", |
| 217 | + "value": data["command"], |
| 218 | + "inline": True |
| 219 | + }, |
| 220 | + { |
| 221 | + "name": "Author", |
| 222 | + "value": f"<@{data['author']}>", |
| 223 | + "inline": True |
| 224 | + }, |
| 225 | + { |
| 226 | + "name": "Timestamp", |
| 227 | + "value": datetime_to_string(datetime.fromtimestamp(data["timestamp"])) + " UTC", |
| 228 | + "inline": True |
| 229 | + } |
| 230 | + ] |
| 231 | + }] |
| 232 | + |
| 233 | + current = "" |
| 234 | + for line in data["traceback"].splitlines(): |
| 235 | + if (len(current) + len(line)) > 2000: |
| 236 | + embeds.append({ |
| 237 | + "color": Format.ERROR.color, |
| 238 | + "description": f"```py\n{current}```" |
| 239 | + }) |
| 240 | + current = "" |
| 241 | + |
| 242 | + else: |
| 243 | + current += f"\n{line}" |
| 244 | + |
| 245 | + if len(current) > 0: |
| 246 | + embeds.append({ |
| 247 | + "color": Format.ERROR.color, |
| 248 | + "description": f"```py\n{current}```" |
| 249 | + }) |
| 250 | + |
| 251 | + while len(embeds) > 0: |
| 252 | + await ctx.respond_with_source(embeds=embeds[:3]) |
| 253 | + embeds = embeds[3:] |
| 254 | + |
| 255 | + @dc.Module.command(register=False) |
| 256 | + @checks.is_bot_owner |
| 257 | + async def blacklist(self, ctx): |
| 258 | + """ |
| 259 | + Manage the blacklist |
| 260 | + """ |
0 commit comments