forked from bxrru/TAS-Comp-Bot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
222 lines (190 loc) · 8.43 KB
/
Copy pathmain.py
File metadata and controls
222 lines (190 loc) · 8.43 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# src/main.py
import asyncio
import logging
import os
import sys
import traceback
import discord
from discord.ext import commands
from dotenv import load_dotenv
from application.parsers.null_parser_strategy import NullParserStrategy
from application.parsers.registry import new_strategy
from application.parsers.rkg_parser_strategy import RkgParserStrategy
from application.parsers.rksys_parser_strategy import RksysParserStrategy
from application.parsers.zip_parser_strategy import ZipParserStrategy
from application.services.submission_services.service_factory import build_submission_service
from infrastructure.db import init_db
from infrastructure.repositories.sqlalchemy_task_repo import SqlAlchemyTaskRepository
from infrastructure.repositories.sqlalchemy_user_repo import SqlAlchemyUserRepository
from infrastructure.repositories.sqlalchemy_submission_repo import SqlAlchemySubmissionRepository
from infrastructure.repositories.sqlalchemy_team_repo import SqlAlchemyTeamRepository
from infrastructure.repositories.sqlalchemy_config_repo import SqlAlchemyConfigRepository
from infrastructure.repositories.sqlalchemy_speedtask_repo import SqlAlchemySpeedTaskRepository
from application.parsers.file_parser import FileParser
from application.services.task_manager import TaskManager
from application.services.user_service import UserService
from application.services.team_service import TeamService
from application.services.config_service import ConfigService
from application.services.speed_task_service import SpeedTaskService
# ────────────────────────── ENV / TOKEN ────────────────────────────
load_dotenv()
TOKEN = os.getenv("TOKEN")
if not TOKEN:
print("❌TOKEN not defined in .env", file=sys.stderr)
sys.exit(1)
# ────────────────────────── BOT ACTIVITY ───────────────────────────
activity = discord.Game(name="Dolphin Emulator")
# ────────────────────────── EXTENSIONS ────────────────────────────
commands_ext = [
"adapters.discord.commands.admin.config_commands",
"adapters.discord.commands.admin.error_test_commands",
"adapters.discord.commands.admin.sync_command",
"adapters.discord.commands.admin.say_command",
"adapters.discord.commands.comp.collab_command",
"adapters.discord.commands.comp.info_command",
"adapters.discord.commands.comp.leave_team_command",
"adapters.discord.commands.comp.name_commands",
"adapters.discord.commands.comp.request_task_command",
"adapters.discord.commands.comp.stop_timer_command",
"adapters.discord.commands.comp.teams_command",
"adapters.discord.commands.fun.8balls_command",
"adapters.discord.commands.fun.slots_command",
"adapters.discord.commands.credits_command",
"adapters.discord.commands.help_command",
"adapters.discord.commands.host.delete_submission_command",
"adapters.discord.commands.host.dm_command",
"adapters.discord.commands.host.edit_submission_command",
"adapters.discord.commands.host.end_task_command",
"adapters.discord.commands.host.extra_setting_command",
"adapters.discord.commands.host.get_results_command",
"adapters.discord.commands.host.get_submissions_command",
"adapters.discord.commands.host.host_dissolve_command",
"adapters.discord.commands.host.host_kick_command",
"adapters.discord.commands.host.set_deadline_command",
"adapters.discord.commands.host.set_file_command",
"adapters.discord.commands.host.speed_task_config_commands",
"adapters.discord.commands.host.start_task_command",
"adapters.discord.commands.host.submit_command",
]
events_ext = [
"adapters.discord.events.dm_logger",
"adapters.discord.events.dm_submission_listener",
"adapters.discord.events.discord_errors",
"adapters.discord.events.deadline_watcher",
"adapters.discord.events.speed_task_reminders",
"adapters.discord.events.speed_task_release"
]
# ────────────────────────── BOT CLASS ──────────────────────────
class Bot(commands.Bot):
def __init__(self):
super().__init__(
command_prefix="$",
intents=discord.Intents.all(),
description="MKWii TAS Competition Bot",
activity=activity,
)
async def setup_hook(self):
# load commands
for ext in commands_ext:
try:
await self.load_extension(ext)
except Exception:
print(f"Loading fail {ext}", file=sys.stderr)
traceback.print_exc()
# load listeners and events
for ext in events_ext:
try:
await self.load_extension(ext)
except Exception:
print(f"Loading fail {ext}", file=sys.stderr)
traceback.print_exc()
async def on_ready(self) -> None:
"""
Called exactly once when the WebSocket handshake is complete
and the bot cache is fully initialised.
"""
guild_names = ", ".join(g.name for g in self.guilds) or "no guilds"
logging.info(
"Bot is ready! Logged in as %s (%s) – connected to %s",
self.user, self.user.id, guild_names,
)
# Pour les environnements sans logging configuré :
print(f"Bot ready – {self.user} | Guild: {guild_names}")
async def _bootstrap() -> None:
# 0) DB init
await init_db()
# 1) Bot instance
bot = Bot()
# 2) Repositories
user_repo = SqlAlchemyUserRepository()
task_repo = SqlAlchemyTaskRepository()
team_repo = SqlAlchemyTeamRepository(user_repo=user_repo)
submission_repo = SqlAlchemySubmissionRepository(
user_repo=user_repo, task_repo=task_repo, team_repo=team_repo
)
config_repo = SqlAlchemyConfigRepository()
speed_repo = SqlAlchemySpeedTaskRepository(user_repo=user_repo, task_repo=task_repo)
# 3) Services that don’t depend on comp/file yet
config_service = ConfigService(config_repo)
user_service = UserService(user_repo, bot)
# 4) Services that depend on parameters
# Default objects (NullParser + generic submission service)
file_parser = FileParser(NullParserStrategy())
comp_key = None
guild_mappings = await config_service.list_guild_configs()
if guild_mappings:
comp_key = guild_mappings[0].comp # 'mkw', 'sm64', ...
# Get the parser strategy by getting the current file extension (if set)
ext_cfg = await config_service.get_submission_file_extension(comp_key)
if ext_cfg and ext_cfg.ext:
if strat := new_strategy(ext_cfg.ext):
file_parser.set_strategy(strat)
# implicit else: keep NullParser until /set-file is run
submission_service = build_submission_service(
comp=comp_key,
user_svc=user_service,
submission_repo=submission_repo,
task_repo=task_repo,
user_repo=user_repo,
team_repo=team_repo,
speed_repo=speed_repo,
file_parser=file_parser,
)
# 5) Remaining services
task_manager = TaskManager(
task_repo=task_repo,
config_service=config_service,
submission_repo=submission_repo,
team_repo=team_repo,
speed_repo=speed_repo,
)
team_service = TeamService(
user_svc=user_service,
team_repo=team_repo,
user_repo=user_repo,
task_repo=task_repo,
)
speed_task_service = SpeedTaskService(
speed_repo=speed_repo,
task_repo=task_repo,
user_svc=user_service,
cfg_svc=config_service,
)
# 6) Inject into bot for global access
bot.task_manager = task_manager
bot.config_service = config_service
bot.submission_service = submission_service
bot.speed_task_service = speed_task_service
bot.team_service = team_service
bot.user_service = user_service
bot.file_parser = file_parser
# 7) Finally run the bot
await bot.start(TOKEN)
# ──────────── Main ───────────
def main() -> None:
try:
asyncio.run(_bootstrap())
except KeyboardInterrupt:
print("Bot stopped.")
if __name__ == "__main__":
main()