-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathadmin.py
167 lines (144 loc) · 6.18 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
import aiohttp
import discord
import importlib
import os
from discord.ext import commands
from utils.default import CustomContext
from utils import permissions, default, http
from utils.data import DiscordBot
class Admin(commands.Cog):
def __init__(self, bot):
self.bot: DiscordBot = bot
@commands.command()
async def amiadmin(self, ctx: CustomContext):
""" Are you an admin? """
if ctx.author.id == self.bot.config.discord_owner_id:
return await ctx.send(f"Yes **{ctx.author.name}** you are an admin! ✅")
# Please do not remove this part.
# I would love to be credited as the original creator of the source code.
# -- AlexFlipnote
if ctx.author.id == 86477779717066752:
return await ctx.send(f"Well kinda **{ctx.author.name}**.. you still own the source code")
await ctx.send(f"no, heck off {ctx.author.name}")
@commands.command()
@commands.check(permissions.is_owner)
async def load(self, ctx: CustomContext, name: str):
""" Loads an extension. """
try:
await self.bot.load_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(default.traceback_maker(e))
await ctx.send(f"Loaded extension **{name}.py**")
@commands.command()
@commands.check(permissions.is_owner)
async def unload(self, ctx: CustomContext, name: str):
""" Unloads an extension. """
try:
await self.bot.unload_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(default.traceback_maker(e))
await ctx.send(f"Unloaded extension **{name}.py**")
@commands.command()
@commands.check(permissions.is_owner)
async def reload(self, ctx: CustomContext, name: str):
""" Reloads an extension. """
try:
await self.bot.reload_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(default.traceback_maker(e))
await ctx.send(f"Reloaded extension **{name}.py**")
@commands.command()
@commands.check(permissions.is_owner)
async def reloadall(self, ctx: CustomContext):
""" Reloads all extensions. """
error_collection = []
for file in os.listdir("cogs"):
if not file.endswith(".py"):
continue
name = file[:-3]
try:
await self.bot.reload_extension(f"cogs.{name}")
except Exception as e:
error_collection.append(
[file, default.traceback_maker(e, advance=False)]
)
if error_collection:
output = "\n".join([
f"**{g[0]}** ```diff\n- {g[1]}```"
for g in error_collection
])
return await ctx.send(
f"Attempted to reload all extensions, was able to reload, "
f"however the following failed...\n\n{output}"
)
await ctx.send("Successfully reloaded all extensions")
@commands.command()
@commands.check(permissions.is_owner)
async def reloadutils(self, ctx: CustomContext, name: str):
""" Reloads a utils module. """
name_maker = f"utils/{name}.py"
try:
module_name = importlib.import_module(f"utils.{name}")
importlib.reload(module_name)
except ModuleNotFoundError:
return await ctx.send(f"Couldn't find module named **{name_maker}**")
except Exception as e:
error = default.traceback_maker(e)
return await ctx.send(f"Module **{name_maker}** returned error and was not reloaded...\n{error}")
await ctx.send(f"Reloaded module **{name_maker}**")
@commands.command()
@commands.check(permissions.is_owner)
async def dm(self, ctx: CustomContext, user: discord.User, *, message: str):
""" DM the user of your choice """
try:
await user.send(message)
await ctx.send(f"✉️ Sent a DM to **{user}**")
except discord.Forbidden:
await ctx.send("This user might be having DMs blocked or it's a bot account...")
@commands.group()
@commands.check(permissions.is_owner)
async def change(self, ctx: CustomContext):
if ctx.invoked_subcommand is None:
await ctx.send_help(str(ctx.command))
@change.command(name="username")
@commands.check(permissions.is_owner)
async def change_username(self, ctx: CustomContext, *, name: str):
""" Change username. """
try:
await self.bot.user.edit(username=name)
await ctx.send(f"Successfully changed username to **{name}**")
except discord.HTTPException as err:
await ctx.send(err)
@change.command(name="nickname")
@commands.check(permissions.is_owner)
async def change_nickname(self, ctx: CustomContext, *, name: str = None):
""" Change nickname. """
try:
await ctx.guild.me.edit(nick=name)
if name:
return await ctx.send(f"Successfully changed nickname to **{name}**")
await ctx.send("Successfully removed nickname")
except Exception as err:
await ctx.send(err)
@change.command(name="avatar")
@commands.check(permissions.is_owner)
async def change_avatar(self, ctx: CustomContext, url: str = None):
""" Change avatar. """
if url is None and len(ctx.message.attachments) == 1:
url = ctx.message.attachments[0].url
else:
url = url.strip("<>") if url else None
try:
bio = await http.get(url, res_method="read")
await self.bot.user.edit(avatar=bio.response)
await ctx.send(f"Successfully changed the avatar. Currently using:\n{url}")
except aiohttp.InvalidURL:
await ctx.send("The URL is invalid...")
except discord.InvalidArgument:
await ctx.send("This URL does not contain a useable image")
except discord.HTTPException as err:
await ctx.send(err)
except TypeError:
await ctx.send("You need to either provide an image URL or upload one with the command")
async def setup(bot):
await bot.add_cog(Admin(bot))