-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (55 loc) · 1.89 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
"""Main file to start the bot."""
import os
from pathlib import Path
import discord
from discord.ext import commands
import config
def load_modules(bot: commands.Bot) -> list[str]:
"""Load all modules (cogs) and return a list of loaded ones.
Args:
----
bot (commands.Bot): The bot instance.
Returns:
-------
list[str]: A list of loaded modules.
"""
loaded: list[str] = []
ignore_items: list[str] = ["__pycache__", "__init__.py"]
for item in os.listdir("modules"):
full_path = Path("modules") / item
if item not in ignore_items and (
full_path.is_dir() or (full_path.is_file() and item.endswith(".py"))
):
try:
module_name = item[:-3] if item.endswith(".py") else item
bot.load_extension(f"modules.{module_name}")
loaded.append(module_name)
except Exception as error: # noqa: BLE001
print(f"Failed to load module {module_name}: {error}")
return loaded
def main() -> None:
"""First function to run when starting the bot."""
intents = discord.Intents.default()
intents.guilds = True
intents.message_content = True
intents.members = True
activity = discord.Activity(
type=discord.ActivityType.watching, name="de DDS server"
)
bot = commands.Bot(
command_prefix=commands.when_mentioned_or(config.PREFIX),
intents=intents,
activity=activity,
owner_id=config.OWNER_ID,
# debug_guilds=[config.GUILD_ID], # noqa: ERA001
sync_slash_commands=True,
)
print("--- Bot is starting up... ---")
# Load modules (cogs)
loaded_modules = load_modules(bot)
print(f"Successfully loaded modules (cogs): {', '.join(loaded_modules)}")
# Run the bot
bot.run(config.BOT_TOKEN)
# Run main() to start the bot
if __name__ == "__main__":
main()