patching voting system
This commit is contained in:
@@ -5,6 +5,7 @@ private 'xcom-lounge' channels without banning them from the server.
|
||||
"""
|
||||
import os
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from collections import defaultdict, deque
|
||||
from typing import Deque, Dict
|
||||
@@ -24,6 +25,7 @@ from utils.config import (
|
||||
get_keywords,
|
||||
add_keyword_points,
|
||||
clear_votes,
|
||||
check_writable,
|
||||
)
|
||||
from cogs.moderation import (
|
||||
get_or_create_xcom_role,
|
||||
@@ -32,6 +34,8 @@ from cogs.moderation import (
|
||||
VOTE_RESET_DAYS,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("excommunicado.bot")
|
||||
|
||||
# Simple in-memory rate limiting for flood detection (user_id -> deque of timestamps)
|
||||
USER_MESSAGE_TIMES: Dict[int, Deque[datetime]] = defaultdict(lambda: deque(maxlen=20))
|
||||
FLOOD_THRESHOLD = 5 # messages
|
||||
@@ -51,24 +55,36 @@ class ExcommunicadoBot(commands.Bot):
|
||||
async def setup_hook(self) -> None:
|
||||
"""Load cogs and sync slash commands."""
|
||||
await self.load_extension("cogs.moderation")
|
||||
logger.info("Loaded extension: cogs.moderation")
|
||||
|
||||
guild_id = os.getenv("GUILD_ID")
|
||||
if guild_id:
|
||||
guild = discord.Object(id=int(guild_id))
|
||||
self.tree.copy_global_to(guild=guild)
|
||||
await self.tree.sync(guild=guild)
|
||||
print(f"[INFO] Synced commands to guild {guild_id}")
|
||||
synced = await self.tree.sync(guild=guild)
|
||||
logger.info("Synced %d commands to guild %s: %s",
|
||||
len(synced), guild_id, ", ".join(c.name for c in synced))
|
||||
else:
|
||||
await self.tree.sync()
|
||||
print("[INFO] Synced global commands (may take up to 1h)")
|
||||
synced = await self.tree.sync()
|
||||
logger.warning(
|
||||
"GUILD_ID not set: synced %d GLOBAL commands (may take up to 1h to appear): %s",
|
||||
len(synced), ", ".join(c.name for c in synced),
|
||||
)
|
||||
|
||||
log_id = os.getenv("LOG_CHANNEL_ID")
|
||||
if log_id:
|
||||
self.log_channel_id = int(log_id)
|
||||
logger.info("Log channel configured: %s", log_id)
|
||||
|
||||
async def on_ready(self) -> None:
|
||||
ok, msg = check_writable()
|
||||
if ok:
|
||||
logger.info(msg)
|
||||
else:
|
||||
logger.error(msg)
|
||||
logger.info("Logged in as %s (ID: %s)", self.user, self.user.id)
|
||||
logger.info("Excommunicado bot is ready (XCOM stealth isolation mode).")
|
||||
print(f"✅ Logged in as {self.user} (ID: {self.user.id})")
|
||||
print("Excommunicado bot is ready (XCOM stealth isolation mode).")
|
||||
|
||||
async def on_message(self, message: discord.Message) -> None:
|
||||
"""Stealth isolation + flood detection.
|
||||
@@ -202,17 +218,41 @@ class ExcommunicadoBot(commands.Bot):
|
||||
if log_channel:
|
||||
try:
|
||||
await log_channel.send(embed=embed)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as err:
|
||||
logger.warning("Failed to post log embed to channel %s: %s",
|
||||
self.log_channel_id, err)
|
||||
|
||||
|
||||
def _configure_logging() -> None:
|
||||
"""Configure root logging from the LOG_LEVEL env var (default INFO)."""
|
||||
level_name = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||
level = getattr(logging, level_name, logging.INFO)
|
||||
logging.basicConfig(
|
||||
level=level,
|
||||
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
# discord.py is noisy at DEBUG; keep it at INFO unless explicitly debugging.
|
||||
logging.getLogger("discord").setLevel(
|
||||
logging.DEBUG if level <= logging.DEBUG else logging.INFO
|
||||
)
|
||||
logger.info("Logging configured at level %s", level_name)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
load_dotenv()
|
||||
_configure_logging()
|
||||
token = os.getenv("DISCORD_TOKEN")
|
||||
if not token:
|
||||
print("ERROR: DISCORD_TOKEN not set in .env")
|
||||
logger.error("DISCORD_TOKEN not set in .env")
|
||||
return
|
||||
|
||||
ok, msg = check_writable()
|
||||
if not ok:
|
||||
logger.error(msg)
|
||||
else:
|
||||
logger.info(msg)
|
||||
|
||||
bot = ExcommunicadoBot()
|
||||
async with bot:
|
||||
await bot.start(token)
|
||||
|
||||
Reference in New Issue
Block a user