-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowner.py
169 lines (143 loc) · 5.57 KB
/
owner.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
import sys
import traceback
from typing import *
import discord
from discord.ext import commands
from discord.ext.commands import *
from discord.client import _log
class OwnerCog(commands.Cog, name="Owner"):
"""Owner commands"""
def __init__(self, bot):
self.bot = bot
self.bot.recentcog = None
async def cog_check(self, ctx):
return await self.bot.is_owner(ctx.author)
@commands.command()
async def shutdown(self, ctx):
"""Shuts down the bot"""
try:
await ctx.reply("Shutting down...")
except discord.Forbidden:
await ctx.author.send("Shutting down...")
print(f"Shutting down...")
print(discord.utils.utcnow().strftime("%d/%m/%Y %I:%M:%S:%f"))
await self.bot.close()
@commands.group(name="cogs", aliases=["cog"])
async def cogs(self, ctx):
"""Cog management"""
return
@cogs.command(name='load')
async def load_cog(self, ctx, *, cog: str):
"""Loads cog. Remember to use dot path. e.g: cogs.owner"""
try:
await self.bot.load_extension(cog)
except Exception as e:
return await ctx.send(f'**ERROR:** {type(e).__name__} - {e}')
else:
await ctx.send(f'Successfully loaded `{cog}`.')
print('---')
print(f'{cog} was loaded.')
print('---')
@cogs.command(name='unload')
async def unload_cog(self, ctx, *, cog: str):
"""Unloads cog. Remember to use dot path. e.g: cogs.owner"""
try:
await self.cancel_tasks(cog)
await self.bot.unload_extension(cog)
except Exception as e:
return await ctx.send(f'**ERROR:** {type(e).__name__} - {e}')
else:
await ctx.send(f'Successfully unloaded `{cog}`.')
print('---')
print(f'{cog} was unloaded.')
print('---')
@cogs.command(name='reload')
async def reload_cog(self, ctx, *, cog: str):
"""Reloads cog. Remember to use dot path. e.g: cogs.owner"""
try:
await self.cancel_tasks(cog)
await self.bot.reload_extension(cog)
except Exception as e:
return await ctx.send(f'**ERROR:** {type(e).__name__} - {e}')
else:
await ctx.send(f'Successfully reloaded `{cog}`.')
self.bot.recentcog = cog
print('---')
print(f'{cog} was reloaded.')
print('---')
@commands.command(hidden=True, aliases=['crr'])
async def recent_cog_reload(self, ctx):
"""Reloads most recent reloaded cog"""
if not self.bot.recentcog: return await ctx.send("You haven't recently reloaded any cogs.")
return await ctx.invoke(self.reload_cog, cog=self.bot.recentcog)
@commands.command()
@commands.guild_only()
async def sync(self, ctx: Context, guilds: Greedy[discord.Object],
spec: Optional[Literal["~", "*", "^"]] = None) -> None:
"""
Works like:
!sync -> global sync
!sync ~ -> sync current guild
!sync * -> copies all global app commands to current guild and syncs
!sync ^ -> clears all commands from the current guild target and syncs (removes guild commands)
!sync id_1 id_2 -> syncs guilds with id 1 and 2
"""
if not guilds:
try:
if spec == "~":
synced = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "*":
ctx.bot.tree.copy_global_to(guild=ctx.guild)
synced = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "^":
ctx.bot.tree.clear_commands(guild=ctx.guild)
await ctx.bot.tree.sync(guild=ctx.guild)
synced = []
else:
synced = await ctx.bot.tree.sync()
await ctx.send(
f"Synced {len(synced)} commands {'globally' if spec is None else 'to the current guild.'}"
)
return
except Exception as e:
traceback.print_exc()
await ctx.send("Something went wrong!")
ret = 0
for guild in guilds:
try:
await ctx.bot.tree.sync(guild=guild)
except discord.HTTPException:
pass
else:
ret += 1
await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.")
async def cancel_tasks(self, name):
async def canceller(self, x):
try:
self.bot.tasks[x].cancel()
except Exception:
pass
if name == 'cogs.comics':
await canceller(self, 'releases')
if name == 'cogs.polls':
for x in ['starts', 'ends']:
for k, v in self.bot.tasks['poll_schedules'][x].items():
try:
v.cancel()
except Exception:
pass
if name == 'funcs.postgresql':
try:
await self.bot.db.close()
except Exception:
print("Couldn't close PostgreSQL connection")
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
ignored = (commands.CommandNotFound, commands.UserInputError)
if isinstance(error, ignored):
return
traceback.print_exception(
type(error), error, error.__traceback__, file=sys.stderr)
_log.error('Ignoring exception in command %r', error.command.name, exc_info=error)
async def setup(bot):
await bot.add_cog(OwnerCog(bot))