-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (66 loc) · 2.45 KB
/
main.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
import logging
import os
import sys
import time
import traceback
from datetime import datetime, timezone
import aiohttp
import discord
from discord.ext import commands
from cogs._config import OWNERS, TOKEN, prefix
from core import formatter, help
os.environ["JISHAKU_NO_UNDERSCORE"] = "True"
class Bot(commands.Bot):
def __init__(self) -> None:
self.start_time = datetime.now(timezone.utc)
intents = discord.Intents.all()
super().__init__(
command_prefix=commands.when_mentioned_or(prefix),
intents=intents,
help_command=help.HelpMenu(),
case_insensitive=True,
)
self.session: aiohttp.ClientSession
formatter.install("discord", "INFO")
formatter.install("bot", "INFO")
self.logger = logging.getLogger("bot")
async def setup_hook(self):
await self.load_extension("jishaku")
await self.load_cogs()
async def load_cogs(self):
s = time.perf_counter()
for file in os.listdir("cogs/"):
if file.endswith(".py") and not file.startswith("_"):
extension = f"cogs.{file[:-3]}"
try:
await self.load_extension(extension)
self.logger.info(f"Loaded - {extension}")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
self.logger.exception(f"Failed to load extension {extension}. - {exception}")
traceback.print_exc()
elapsed = time.perf_counter() - s
self.logger.info(f"Loaded all extensions - took {elapsed:.2f}s")
async def is_owner(self, user: discord.abc.User):
if user.id in OWNERS:
return True
# Else fall back to the original
return await super().is_owner(user)
async def on_ready(self) -> None:
self.session = aiohttp.ClientSession(loop=self.loop)
await self.tree.sync()
await self.change_presence(activity=discord.Game(name="Free Media Heck Yeah"))
self.logger.info("Bot is ready!")
if __name__ == "__main__":
bot = Bot()
try:
bot.run(TOKEN, log_handler=None)
except Exception as e:
bot.logger.error(f"{type(e).__name__}: {e}")
traceback.print_exc()
except KeyboardInterrupt:
bot.logger.info("Shutting down...")
bot.loop.run_until_complete(bot.session.close())
bot.logger.info("Goodbye!")
finally:
sys.exit()