forked from CodeWithSwastik/DuccLife
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoreload.py
More file actions
58 lines (48 loc) · 1.87 KB
/
Copy pathautoreload.py
File metadata and controls
58 lines (48 loc) · 1.87 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
import asyncio
import os
import traceback
from discord.ext.commands import Bot, Cog
from watchgod import awatch
from watchgod.watcher import Change
class UnloadException(Exception):
pass
class AutoReloader(Cog):
def __init__(self, bot: Bot):
self.bot: Bot = bot
self.coro = self.reloader()
self.task = asyncio.ensure_future(self.coro)
def cog_unload(self) -> None:
self.coro.throw(UnloadException())
async def reloader(self):
try:
async for changes in awatch("cogs" + os.path.sep):
for change_type, path in changes:
if not path.endswith(".py"):
print(f"Skipping {path} as it doesn't end with .py")
continue
if path.count(".") != 1:
print(
f"Skipping {path} as it has more than one '.' in it's name"
)
continue
ext = path.replace(os.path.sep, ".")[:-3]
if change_type == Change.modified:
if ext in self.bot.extensions:
self.bot.unload_extension(ext)
self.load(ext)
elif change_type == Change.added:
self.load(ext)
elif change_type == Change.deleted and ext in self.bot.extensions:
self.bot.unload_extension(ext)
except UnloadException:
return
def load(self, ext: str):
try:
self.bot.load_extension(ext)
except Exception:
print(f"Failed to load extension {ext}")
traceback.print_exc()
else:
print(f"Loaded extension {ext}")
def setup(bot: Bot):
bot.add_cog(AutoReloader(bot))