-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
65 lines (52 loc) · 1.8 KB
/
bot.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
import logging
from pathlib import Path
from discord.ext import commands
from aiohttp import ClientSession
from data import config
from cogs.utils.context import Context
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s: %(name)s -> %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger(__name__)
class JackassBot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.error_messages = {
"api": "Problems connecting to the API (status code `{}`)",
"no_results": "No results for `{}`"
}
@property
def config(self):
return config
async def on_ready(self):
log.info(f"{self.user} is in!")
async def get_context(self, message, *, cls=Context):
return await super().get_context(message, cls=cls)
@property
def module_list(self):
m = list(Path("cogs").glob("*.py"))
all_cogs = [f"cogs.{i.name}"[:-3] for i in m]
return all_cogs
async def load_modules(self):
_output = []
for cog in self.module_list:
try:
self.load_extension(cog)
_output.append(f"[{cog}] loaded")
except commands.ExtensionAlreadyLoaded:
_output.append(f"[{cog}] already loaded")
except commands.ExtensionNotLoaded:
_output.append(f"[{cog}] not loaded")
log.exception(f"Failed to load {cog}")
return "\n".join(_output)
async def start(self, token: str = None):
if not token:
token = self.config.token
self.session = ClientSession()
await self.load_modules()
await super().start(token)
async def close(self):
await super().close()
await self.session.close()