-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathapplication.py
More file actions
110 lines (86 loc) · 3.08 KB
/
application.py
File metadata and controls
110 lines (86 loc) · 3.08 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import time
from pathlib import Path
import discord
from discord import Intents
from discord.app_commands import CommandTree
from dotenv import load_dotenv
from pymongo import AsyncMongoClient
from bot import DiscordBot
from utils.config import Config
load_dotenv()
bot_prefix = os.getenv("BOT_PREFIX")
client = DiscordBot(
command_prefix=bot_prefix,
help_command=None,
intents=Intents().all(),
tree_cls=CommandTree,
)
connection = ""
try:
client.mongo_client = AsyncMongoClient(os.environ["MONGO_URI"], tz_aware=True)
db_name = os.environ["DB_NAME"]
client.db = client.mongo_client[db_name]
client.link_collection = client.db["link"]
client.student_collection = client.db["student"]
client.anonban_collection = client.db["anonban"]
client.mute_collection = client.db["mute"]
connection = "Connected to MongoDB"
except Exception as e:
connection = f"Failed to connect to MongoDB: {e}"
@client.event
async def on_ready() -> None:
client.startTime = time.time()
if client.user:
client.logger.info(f"Logged in as {client.user.name} ({client.user.id})")
client.logger.info(connection)
# Initialize the config instance after bot is ready
client.config = Config(client)
client.logger.info("Config initialized")
# Clear all commands
# await clear_all_commands(client=client)
# Load cogs
for path in Path("cogs").rglob("*.py"):
if path.name.startswith("__"):
continue
cog = ".".join(path.with_suffix("").parts)
try:
await client.load_extension(cog)
client.logger.info(f"Loaded {cog}")
except Exception as e:
client.logger.error(f"Failed to load {cog}: {e}")
# Sync commands
# await sync_all_commands(client=client)
# Set status
await client.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name="students suffer",
)
)
client.logger.info("Set status")
client.logger.info("Bot is ready")
await client.config.bot_logs_channel.send("Bot is online")
async def clear_all_commands(client: DiscordBot) -> None:
"""Clear all guild commands"""
guild = client.config.guild
try:
# Clear all guild commands
client.tree.clear_commands(guild=guild)
await client.tree.sync(guild=guild)
client.logger.info("Cleared all guild commands")
except Exception as e:
client.logger.error(f"Failed to clear guild commands: {e}")
async def sync_all_commands(client: DiscordBot) -> None:
"""Sync all commands to the guild"""
guild = client.config.guild
try:
await client.tree.sync(guild=guild)
client.logger.info("Synced all commands to the guild")
except Exception as e:
client.logger.error(f"Failed to sync commands: {e}")
bot_token = os.getenv("BOT_TOKEN")
if bot_token:
client.run(bot_token)
else:
client.logger.error("BOT_TOKEN environment variable not set")