Skip to content

PESU Auth Implementation for PESU-MC - #13

Merged
PrathamSGowda merged 3 commits into
mainfrom
dotpmm
Jun 14, 2026
Merged

PrathamSGowda merged 3 commits into
mainfrom
dotpmm

Conversation

@dotpmm

@dotpmm dotpmm commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

This pull request introduces several configuration and structural changes to support multiple MongoDB databases, improve Discord bot command handling, and add new authentication utilities. The main updates include environment variable changes, Docker and startup script adjustments, new authentication configuration and error handling utilities, and improvements to how the bot manages Minecraft-related commands and database connections.

Environment and Configuration Updates:

  • .env.example: Replaces MONGO_URI and MONGO_DB with separate variables for stats and auth databases (STATS_MONGO_URI, STATS_DB_NAME, AUTH_MONGO_URI, AUTH_DB_NAME), adds GUILD_ID and MC_READY flags, and explains the need for two MongoDB connections due to free plan limitations.
  • pyproject.toml: Updates project name and version, and adds httpx as a dependency.

Docker and Startup Script Adjustments:

  • Dockerfile: Changes exposed port from 7860 to 10000 and reduces default Gunicorn concurrency settings to 1 worker and 1 thread each.

As of now, i dont see any issues with this setup, and more over, render doesnt always guarantee u more than one thread

  • start.sh: Updates Gunicorn to bind to port 10000 (or the value of $PORT) instead of 7860.

Authentication and Error Handling Enhancements:

  • auth/config.py: Introduces a Config class to centrally manage Discord role and channel IDs, guild access, and role/channel lookup utilities, with robust error handling.
  • auth/general.py: Adds a utility to build a standardized Discord embed for unknown errors, improving error reporting.

Bot and Database Handling Improvements:

  • main.py:

    • Implements a custom MinecraftCommandTree to disable Minecraft commands when the MC_READY flag is false.
    • Adds support for separate async MongoDB connections for authentication data.
    • Dynamically loads the auth.verify extension and synchronizes commands to a specific guild if GUILD_ID is set, otherwise syncs globally. [1] [2] [3]
  • stats/mongo.py: Switches to using STATS_MONGO_URI and STATS_DB_NAME for stats data, and only initializes the MongoDB client if MC_READY is true, with improved error handling.

These changes collectively improve maintainability, allow for better resource separation, and provide a more robust and configurable Discord bot deployment.

Tested locally and deployed it on render.com here with uptimerobot hook!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a PESU authentication/verification flow to the Discord bot, splits MongoDB usage into separate “stats” and “auth” databases, and updates deployment config to better support Render-style environments.

Changes:

  • Introduces /verify, /deverify, /info, and /auth commands (PESUAuth-backed) plus shared auth config/error-embed utilities.
  • Adds separate MongoDB configuration for stats vs auth and gates Minecraft-related DB initialization/commands behind an MC_READY flag.
  • Updates Docker/startup configuration (port 10000 / $PORT) and adds httpx dependency.

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
uv.lock Locks new dependency set including httpx and its transitive deps.
pyproject.toml Renames project/version and adds httpx dependency.
.env.example Documents new env vars for dual MongoDB connections, guild scoping, and MC_READY.
Dockerfile Updates exposed port and default Gunicorn concurrency env vars.
start.sh Binds Gunicorn to $PORT (default 10000).
stats/mongo.py Switches to STATS_* env vars and conditionally initializes stats DB when MC_READY is true.
main.py Adds MC command gating via custom command tree, adds auth DB client wiring, loads auth extension, and syncs commands by guild when configured.
auth/config.py Adds centralized role/channel/guild configuration and lookup helpers.
auth/general.py Adds standardized “unknown error” embed builder.
auth/verify.py Implements PESUAuth-backed verification + admin tooling and role assignment/removal logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread main.py
Comment on lines 1 to +9
import os
from dotenv import load_dotenv

import asyncio
import discord
from discord.ext import commands, tasks
from discord import app_commands
import asyncio
from datetime import datetime, timezone

from pymongo import AsyncMongoClient

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gng, they serve different purpose 😭

Comment thread main.py
Comment on lines +50 to +55
# new async client for auth db, cuz 1 project db can only have 1 cluster under mongo free plan
mongo_uri = os.getenv("AUTH_MONGO_URI", os.getenv("MONGO_URI", ""))
auth_db_name = os.getenv("AUTH_DB_NAME", "xymic")
_mongo = AsyncMongoClient(mongo_uri, tz_aware=True) if mongo_uri else None
bot.link_collection = _mongo[auth_db_name]["link"]
bot.verify_enabled = True

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, wont happen! so....

Comment thread main.py
Login acknowledgement and start timers for `check_server`
"""
await tree.sync()
await bot.load_extension("auth.verify")
Comment thread auth/verify.py
Comment on lines +147 to +166
profile = data.get("profile", {})
verified_srn = profile.get("srn", srn.strip().upper())
raw_branch = profile.get("branch", "")
campus_api = profile.get("campus", "RR").strip().upper()
year = _year_from_srn(verified_srn)
branch_key = _normalise_branch(raw_branch)

if year:
grad_year = str(int(year) + 4)
display_year = f"Batch of {grad_year}"

display_campus = "RRC" if campus_api == "RR" else "ECC" if campus_api == "EC" else campus_api
# i like to call it RR'C' and EC'C' so ....had to do this drama ^^
roles_to_add: list[discord.Role] = []
skipped: list[str] = []
for role_type, key, label in [
("YEAR", grad_year, f"Year ({display_year})"),
("BRANCH", branch_key, f"Branch ({branch_key})"),
("CAMPUS", campus_api, f"Campus ({display_campus})"),
]:
Comment thread auth/verify.py
Comment on lines +52 to +57
try:
async with httpx.AsyncClient(timeout=60) as http:
resp = await http.post(_PESUAUTH_URL, json=payload)
return resp.json() if resp.status_code == 200 else None
except (httpx.HTTPError, httpx.TimeoutException):
return None
Comment thread auth/config.py
Comment on lines +8 to +10
if TYPE_CHECKING:
from bot import DiscordBot

Comment thread auth/general.py
Comment on lines +1 to +21
from datetime import datetime

import discord


def build_unknown_error_embed(error: Exception) -> discord.Embed:
return (
discord.Embed(
title="Unexpected Error",
description="Something went wrong while processing the command.",
color=discord.Color.red(),
timestamp=datetime.now(),
)
.add_field(name="Error Type", value=type(error).__name__, inline=True)
.add_field(
name="Details",
value=str(error)[:1000] or "No details available.",
inline=False,
)
.set_footer(text="Xymic")
)
Comment thread .env.example
Comment on lines +5 to +10
GUILD_ID
STATS_MONGO_URI
STATS_DB_NAME
AUTH_MONGO_URI
AUTH_DB_NAME
MC_READY
@dotpmm

dotpmm commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator Author

Request to change the name of the repo frompesu-mc-botto Xymic @PrathamSGowda @joshua-rajj @woterr

@PrathamSGowda
PrathamSGowda merged commit 464ddd0 into main Jun 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants