This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
/
admin.py
252 lines (200 loc) · 8.02 KB
/
admin.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
from discord.ext import commands as cmd
from discord.http import Route
import discord
import traceback
import inspect
from contextlib import redirect_stdout
import textwrap
import io
from prettytable import PrettyTable
import asyncio
from datetime import timedelta
from utils import checks, formatter, context
class Admin(cmd.Cog, command_attrs=dict(hidden=True)):
def __init__(self, bot):
self.bot = bot
self._last_result = None
@cmd.command()
@checks.has_role_on_support_guild("Staff")
@checks.has_role_on_support_guild("Staff")
async def su(self, ctx, member: discord.User, *, msg):
"""
Execute a command in place of another user
__Arguments__
**member**: The user (must be a member of this guild)
**msg**: The message, doesn't need to be a command
"""
if member.id == ctx.bot.owner_id:
raise cmd.CommandError("How about ... **no**?")
webhook = await ctx.channel.create_webhook(name="sudo")
await webhook.send(content=msg, username=member.name, avatar_url=member.avatar_url)
await webhook.delete()
await asyncio.sleep(1) # Webhooks are slow
message = ctx.message
message.author = member
message.content = msg
await self.bot.process_commands(message)
@cmd.command()
@cmd.is_owner()
async def sudo(self, ctx, *, command):
"""
Execute a command and bypass cooldown
__Arguments__
**command**: The command
"""
message = ctx.message
message.content = command
new_ctx = await self.bot.get_context(message, cls=context.Context)
new_ctx.command.reset_cooldown(new_ctx)
if isinstance(new_ctx.command, cmd.Group):
for command in new_ctx.command.all_commands.values():
command.reset_cooldown(new_ctx)
await self.bot.invoke(new_ctx)
@cmd.command(aliases=["rl"])
@cmd.is_owner()
async def reload(self, ctx, cog):
"""
Reload a cog
__Arguments__
**cog**: The name of the cog
"""
if cog.lower() == "all":
failed = 0
success = 0
for cog in self.bot.config.extensions:
try:
self.bot.reload_extension(cog)
success += 1
except:
failed += 1
traceback.print_exc()
await ctx.send(**ctx.em(f"Reloaded all cogs.\n**Success**: {success} **Failed**: {failed}", type="info"))
return
base_path = "cogs."
try:
self.bot.reload_extension(base_path + cog.lower())
await ctx.send(**ctx.em(f"Successfully reloaded the cog named **{cog}**.", type="success"))
except:
traceback.print_exc()
raise cmd.CommandError(f"Error while reloading the cog named **{cog}**.")
@cmd.command()
@checks.has_role_on_support_guild("Admin")
async def restart(self, ctx):
await ctx.send(**ctx.em("Restarting ...", type="info"))
await self.bot.close()
@cmd.command(name="exec")
@cmd.is_owner()
async def _exec(self, ctx, *, body: str):
"""
Executes something, uses exec not eval -> returns None
__Arguments__
**body**: The code to get executed
"""
def cleanup_code(content):
"""Automatically removes code blocks from the code."""
# remove ```py\n```
if content.startswith('```') and content.endswith('```'):
return '\n'.join(content.split('\n')[1:-1])
# remove `foo`
return content.strip('` \n')
env = {
'bot': self.bot,
'ctx': ctx,
'channel': ctx.channel,
'author': ctx.author,
'guild': ctx.guild,
'msg': ctx.message,
'_': self._last_result
}
env.update(globals())
body = cleanup_code(body)
stdout = io.StringIO()
to_compile = f'async def func():\n{textwrap.indent(body, " ")}'
try:
exec(to_compile, env)
except Exception as e:
return await ctx.send(f'```py\n{e.__class__.__name__}: {e}\n```')
func = env['func']
try:
with redirect_stdout(stdout):
ret = await func()
except:
value = stdout.getvalue()
await ctx.send(f'```py\n{value}{traceback.format_exc()}\n```')
else:
value = stdout.getvalue()
try:
await ctx.message.add_reaction('\u2705')
except:
pass
if ret is None:
if value:
await ctx.send(f'```py\n{value}\n```')
else:
self._last_result = ret
await ctx.send(f'```py\n{value}{ret}\n```')
@cmd.command()
@cmd.is_owner()
async def eval(self, ctx, *, expression: str):
"""
Evaluate a single expression and return the result
__Arguments__
**expressions**: The expression
"""
to_eval = expression.replace("await ", "")
try:
result = eval(to_eval)
if inspect.isawaitable(result):
result = await result
except Exception as e:
result = type(e).__name__ + ": " + str(e)
result = str(result).replace(ctx.config.token, "you tried")
embed = ctx.em("")["embed"]
embed.title = "Eval Result"
embed.add_field(name="Input 📥", value=f"```Python\n{expression}```", inline=False)
embed.add_field(name="Output 📤", value=f"```Python\n{result}```", inline=False)
await ctx.send(embed=embed)
@cmd.command()
@cmd.is_owner()
async def query(self, ctx, timeout: float = 0.5, *, expression: str):
"""
Evaluate a single expression on all shards and return the results
__Arguments__
**expressions**: The expression
"""
results = await self.bot.query(expression, timeout=timeout)
table = PrettyTable()
table.field_names = ["Shard-Id", "Result"]
for shards, result in sorted(results, key=lambda r: sum(r[0])):
table.add_row([", ".join([str(s) for s in shards]), result])
pages = formatter.paginate(str(table))
for page in pages:
await ctx.send(f"```diff\n{page}```")
@cmd.command()
@checks.has_role_on_support_guild("Staff")
@checks.has_role_on_support_guild("Staff")
async def gateway(self, ctx):
"""
x!gateway x!gateway looked at the x!gateway x!gateway in his hands and felt x!gateway.
He walked over to the window and reflected on his x!gateway surroundings. He had always loved x!gateway
x!gateway with its xanthocarpous, xanthic x!gateway. It was a place that encouraged his tendency to feel
x!gateway. Then he saw something in the distance, or rather someone. It was the figure of x!gateway x!gateway.
x!gateway was a x!gateway x!gateway with x!gateway x!gateway and x!gateway x!gateway.
x!gateway gulped. He glanced at his own reflection. He was a x!gateway, x!gateway, x!gateway drinker with
x!gateway x!gateway and x!gateway x!gateway. His friends saw him as a xanthocarpous, xanthic x!gateway. Once,
he had even revived a dying, x!gateway.
"""
data = await ctx.bot.http.request(Route('GET', '/gateway/bot'))
identifies = data["session_start_limit"]
embed = ctx.em("", type="info", title="Bot Gateway")["embed"]
embed.add_field(name="Url", value=data["url"])
embed.add_field(name="Shards", value=data["shards"])
embed.add_field(name="Total Identifies", value=identifies["total"])
embed.add_field(name="Remaining Identifies", value=identifies["remaining"])
embed.add_field(
name="Reset After",
value=str(timedelta(milliseconds=identifies["reset_after"])).split(".")[0]
)
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(Admin(bot))