-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
50 lines (40 loc) · 1.27 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
import discord
from discord.ext import commands
from dotenv import load_dotenv
import os
from config import *
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
loaded_cogs = []
@bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
await bot.tree.sync()
async def main():
global loaded_cogs
async with bot:
for filename in os.listdir(os.path.join(os.path.dirname(__file__), "cogs")):
if filename.endswith(".py"):
module = f"cogs.{filename[:-3]}"
print(f"Loading module {module}")
try:
await bot.load_extension(module)
loaded_cogs.append(module)
except Exception as e:
print(f"Failed to load module {module}: {e}")
await bot.start(os.environ["DISCORD_TOKEN"])
async def close_cogs():
for cog in loaded_cogs:
print(f"Unloading module {cog}")
await bot.remove_cog(cog)
await bot.close()
if __name__ == "__main__":
import asyncio
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Exiting...")
asyncio.run(close_cogs())
print("Closed.")