-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
275 lines (226 loc) · 9.88 KB
/
main.py
File metadata and controls
275 lines (226 loc) · 9.88 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import asyncio
import logging
import os
import signal
import sys
from logging.handlers import RotatingFileHandler
import discord
from discord.ext import commands
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from api.webhook_server import WebhookServer
from bot.admin_commands import AdminCommands
from bot.commands import SeerrCommands
from config import Config
from database.database import Database
from seerr.api import SeerrAPI
logging.basicConfig(
level=getattr(logging, Config.LOG_LEVEL, logging.INFO),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
RotatingFileHandler(
"data/logs/discordeerr.log",
maxBytes=10 * 1024 * 1024,
backupCount=5,
encoding="utf-8",
),
logging.StreamHandler(sys.stdout),
],
)
logger = logging.getLogger(__name__)
class SeerrBot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
super().__init__(command_prefix=None, intents=intents, help_command=None)
self.database = Database()
self.seerr_api = SeerrAPI()
self.webhook_server = None
async def setup_hook(self):
"""Setup hook called when bot is starting"""
logger.info("Setting up bot...")
await self.add_cog(SeerrCommands(self, self.database, self.seerr_api))
await self.add_cog(AdminCommands(self, self.database, self.seerr_api))
logger.info("Bot setup complete")
async def on_ready(self):
"""Called when bot is ready"""
logger.info(f"Bot is ready! Logged in as {self.user}")
logger.info(f"Bot ID: {self.user.id}")
logger.info(f"Connected to {len(self.guilds)} guild(s)")
logger.info("Syncing commands to Discord...")
commands = list(self.tree.get_commands())
logger.debug(f"Registered {len(commands)} commands")
max_retries = 3
for attempt in range(max_retries):
try:
logger.info(f"Syncing commands globally (attempt {attempt + 1}/{max_retries})")
await asyncio.wait_for(self.tree.sync(), timeout=60.0)
logger.info("Commands synced successfully!")
break
except TimeoutError:
logger.warning(f"Command sync timed out (attempt {attempt + 1}/{max_retries})")
if attempt < max_retries - 1:
logger.debug("Retrying in 5 seconds...")
await asyncio.sleep(5)
else:
logger.error("Command sync failed after all retries")
except Exception as e:
logger.error(f"Error syncing commands (attempt {attempt + 1}/{max_retries}): {e}")
if attempt < max_retries - 1:
logger.debug("Retrying in 5 seconds...")
await asyncio.sleep(5)
else:
logger.error("Command sync failed after all retries")
await self.test_connections()
await self.start_webhook_server()
await self.change_presence(
activity=discord.Activity(type=discord.ActivityType.watching, name="Seerr requests")
)
async def on_app_command_error(
self,
interaction: discord.Interaction,
error: discord.app_commands.AppCommandError,
):
"""Handle application command errors gracefully"""
if isinstance(error, discord.app_commands.errors.CommandNotFound):
logger.warning(
f"Command not found: {interaction.command.name if interaction.command else 'Unknown'}"
)
embed = discord.Embed(
title="Command Not Found",
description="This command has been removed or is not available. Please try refreshing Discord or contact an administrator.",
color=discord.Color.red(),
)
try:
await interaction.response.send_message(embed=embed, ephemeral=True)
except discord.errors.InteractionResponded:
try:
await interaction.followup.send(embed=embed, ephemeral=True)
except discord.errors.InteractionResponded:
pass
elif isinstance(error, discord.app_commands.errors.MissingPermissions):
embed = discord.Embed(
title="Permission Denied",
description="You don't have permission to use this command.",
color=discord.Color.red(),
)
try:
await interaction.response.send_message(embed=embed, ephemeral=True)
except discord.errors.InteractionResponded:
pass
elif isinstance(error, discord.app_commands.errors.CheckFailure):
pass
else:
logger.error(f"Application command error: {error}")
embed = discord.Embed(
title="Error",
description="An unexpected error occurred. Please try again later.",
color=discord.Color.red(),
)
try:
await interaction.response.send_message(embed=embed, ephemeral=True)
except discord.errors.InteractionResponded:
pass
async def on_error(self, event_method: str, *args, **kwargs):
"""Handle all bot errors including command tree errors"""
logger.error(f"Error in {event_method}: {args} {kwargs}")
if event_method == "on_interaction" and args:
interaction = args[0]
if hasattr(interaction, "response") and not interaction.response.is_done():
embed = discord.Embed(
title="Command Error",
description="This command is no longer available. Please try refreshing Discord or contact an administrator.",
color=discord.Color.red(),
)
try:
await interaction.response.send_message(embed=embed, ephemeral=True)
except discord.errors.InteractionResponded:
pass
async def test_connections(self):
"""Test all external connections"""
logger.info("Testing connections...")
if self.seerr_api.test_connection():
logger.info("Seerr API connection successful")
else:
logger.warning("Seerr API connection failed")
try:
self.database.get_admin_setting("test")
logger.info("Database connection successful")
except Exception as e:
logger.error(f"Database connection failed: {e}")
async def start_webhook_server(self):
"""Start the webhook server"""
try:
self.webhook_server = WebhookServer(self, self.database, self.seerr_api)
self.webhook_server.start_in_thread()
logger.info("Webhook server started")
except Exception as e:
logger.error(f"Failed to start webhook server: {e}")
async def on_command_error(self, ctx, error):
"""Handle command errors"""
if isinstance(error, commands.CommandNotFound):
return
elif isinstance(error, commands.MissingPermissions):
await ctx.send("You don't have permission to use this command.")
elif isinstance(error, commands.BotMissingPermissions):
await ctx.send("I don't have the required permissions to execute this command.")
else:
logger.error(f"Command error: {error}")
await ctx.send("An error occurred while executing the command.")
async def cleanup(self):
"""Cleanup resources before shutdown"""
logger.info("Cleaning up bot resources...")
try:
if hasattr(self, "webhook_server") and self.webhook_server:
logger.info("Stopping webhook server...")
self.webhook_server.stop()
if hasattr(self, "database") and self.database:
logger.info("Closing database connections...")
logger.info("Bot cleanup complete")
except Exception as e:
logger.error(f"Error during cleanup: {e}")
async def main():
"""Main function to run the bot"""
if not Config.DISCORD_TOKEN:
logger.error("DISCORD_TOKEN is required in environment variables or .env file")
sys.exit(1)
if not Config.SEERR_URL:
logger.warning("SEERR_URL not configured - some features may not work")
if not Config.SEERR_API_KEY:
logger.warning("SEERR_API_KEY not configured - some features may not work")
bot = SeerrBot()
shutdown_requested = False
def signal_handler(signum, frame):
nonlocal shutdown_requested
if not shutdown_requested:
shutdown_requested = True
logger.info(f"Received signal {signum}, initiating graceful shutdown...")
if hasattr(bot, "loop") and bot.loop and not bot.loop.is_closed():
bot.loop.create_task(shutdown_bot(bot))
else:
logger.info("Bot not fully started, exiting...")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
try:
await bot.start(Config.DISCORD_TOKEN)
except KeyboardInterrupt:
logger.info("Bot stopped by user (KeyboardInterrupt)")
except Exception as e:
logger.error(f"Bot error: {e}")
finally:
if not shutdown_requested:
await shutdown_bot(bot)
async def shutdown_bot(bot):
"""Gracefully shutdown the bot"""
logger.info("Shutting down bot gracefully...")
try:
await bot.cleanup()
if not bot.is_closed():
logger.info("Closing bot connection...")
await bot.close()
logger.info("Bot shutdown complete")
except Exception as e:
logger.error(f"Error during shutdown: {e}")
if __name__ == "__main__":
asyncio.run(main())