-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
97 lines (80 loc) · 2.97 KB
/
bot.py
File metadata and controls
97 lines (80 loc) · 2.97 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
"""
Main bot file for the Plex Discord Bot.
"""
import os
import logging
import asyncio
import json
import discord
from discord.ext import commands
from utils import load_config
from media_watcher_service import setup_media_watcher_service
from typing import Dict, Any
from config import bot_config, BotConfig
# --- Configuration Loading ---
CONFIG_FILE = "config.json"
try:
load_config(CONFIG_FILE)
except (FileNotFoundError, json.JSONDecodeError) as e:
logging.error(
f"Error loading configuration from '{CONFIG_FILE}': {e}. Exiting.")
exit(1)
# --- Logging Setup ---
configured_log_level_str: str = bot_config.log_level.upper()
LOGGING_LEVELS: Dict[str, int] = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL
}
log_level: int = LOGGING_LEVELS.get(configured_log_level_str, logging.INFO)
logging.basicConfig(
level=log_level,
format='%(asctime)s %(levelname)-8s %(name)-15s %(message)s',
handlers=[logging.StreamHandler()],
force=True
)
root_logger: logging.Logger = logging.getLogger()
effective_level_numeric: int = root_logger.getEffectiveLevel()
effective_level_name: str = logging.getLevelName(effective_level_numeric)
logging.critical(
f"LOGGING SERVICE: Root logger initialized. Effective log level set to: {effective_level_name} (Numeric: {effective_level_numeric})")
if log_level <= logging.INFO:
logging.getLogger('discord').setLevel(logging.INFO)
else:
logging.getLogger('discord').setLevel(logging.WARNING)
logging.getLogger('asyncio').setLevel(logging.WARNING)
logging.getLogger('requests').setLevel(logging.WARNING)
logging.getLogger('werkzeug').setLevel(logging.WARNING)
logging.getLogger('paramiko').setLevel(logging.WARNING)
# --- Bot Setup ---
DISCORD_TOKEN: str = os.getenv("DISCORD_TOKEN")
if not DISCORD_TOKEN:
logging.error(
"DISCORD_TOKEN environment variable not set. Please add it to your .env file. Exiting.")
exit(1)
intents: discord.Intents = discord.Intents.default()
intents.message_content = True
intents.members = True
class PlexBot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.config: BotConfig = bot_config
async def setup_hook(self) -> None:
# Load cogs
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await self.load_extension(f'cogs.{filename[:-3]}')
logging.info(f"Loaded cog: {filename}")
# Setup media watcher service
await setup_media_watcher_service(self)
logging.info("Media Watcher Service setup initiated.")
async def on_ready(self) -> None:
logging.info(f"Logged in as {self.user} (ID: {self.user.id})")
if __name__ == "__main__":
bot = PlexBot(command_prefix="!", intents=intents)
try:
bot.run(DISCORD_TOKEN)
except Exception as e:
logging.critical(f"Bot crashed with error: {e}", exc_info=True)