-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (85 loc) · 2.67 KB
/
Copy pathmain.py
File metadata and controls
110 lines (85 loc) · 2.67 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
"""Discord Bot Template — Entry point and cog loader.
Replace YOUR_PROJECT with your project name throughout this template.
"""
import asyncio
import atexit
import os
import sys
import signal
import discord
from discord.ext import commands
from dotenv import load_dotenv
import config
from shared.db import init_pool, close_pool
# ── PID file lock — prevent duplicate bot instances ──
PID_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".bot.pid")
def _check_pid_lock():
"""Exit immediately if another instance is already running."""
if os.path.exists(PID_FILE):
try:
with open(PID_FILE) as f:
old_pid = int(f.read().strip())
os.kill(old_pid, 0)
print(f"ERROR: Bot already running (PID {old_pid}). Exiting.")
sys.exit(1)
except (ProcessLookupError, ValueError):
pass
with open(PID_FILE, "w") as f:
f.write(str(os.getpid()))
def _cleanup():
try:
os.remove(PID_FILE)
except OSError:
pass
atexit.register(_cleanup)
signal.signal(signal.SIGTERM, lambda *_: (_cleanup(), sys.exit(0)))
_check_pid_lock()
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
if not TOKEN:
raise RuntimeError("DISCORD_TOKEN not set in .env")
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(
command_prefix=config.COMMAND_PREFIX,
intents=intents,
help_command=None,
)
# ── Cog modules to load ──
# Add your cogs here as you build them.
# Organized by phase for incremental deployment.
COG_MODULES = [
# Phase 1 — Core
"cogs.logging_cog",
"cogs.welcome",
"cogs.tickets",
"cogs.announce",
# Phase 2 — Engagement
"cogs.leveling",
"cogs.giveaway",
# Add more cogs as needed:
# "cogs.your_cog_name",
]
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
guild = bot.get_guild(config.GUILD_ID)
if guild:
print(f"Connected to guild: {guild.name} ({guild.member_count} members)")
# Sync slash commands to guild
bot.tree.copy_global_to(guild=discord.Object(id=config.GUILD_ID))
await bot.tree.sync(guild=discord.Object(id=config.GUILD_ID))
print("Slash commands synced.")
async def main():
async with bot:
await init_pool()
for module in COG_MODULES:
try:
await bot.load_extension(module)
print(f"Loaded: {module}")
except Exception as e:
print(f"Failed to load {module}: {e}")
await bot.start(TOKEN)
if __name__ == "__main__":
asyncio.run(main())