Files
server-26/drb-server-discord-bot/app/bot.py
T
Logan CusanoandClaude Opus 5.5 455c55f5c2 feat(discord): self-serve server->org linking with /drb link (#213 item 1)
- c2-core: POST /discord/link-codes (org owner/admin) mints a single-use,
  10-minute code; minting revokes the org's older codes. The bot redeems it
  via POST /discord/guilds/{id}/link/redeem (service key) inside one
  Firestore transaction, so two servers racing for one code cannot both win
  and a server linked to org A refuses org B's code. GET/DELETE
  /discord/linked-guilds lists/unlinks the caller's org's servers; unlink
  stops that server's streams. Rate-limited (mint per user, redeem per
  server and per Discord user); every mint/link/refusal/unlink audited.
- resolve_guild_org: an explicitly unlinked server no longer falls back to
  DISCORD_UNLINKED_GUILD_ORG_ID. Linking away from the fallback org stops
  the streams it had running for that org.
- Discord bot: /drb link code: (Manage Server, enforced at runtime too);
  GatedTree answers every other command in an unlinked server with how to
  link (autocomplete returns nothing). With the default fallback org set,
  nothing is gated — unchanged behaviour.
- Frontend: "Linked Discord servers" card on Settings -> Organization —
  generate code with countdown + copy, polls for the new link, unlink.
- 16 tests: single-use, two-server race, one-server/two-org race, expiry,
  revoked/malformed codes, wrong-org takeover, idempotent relink, rate
  limits, fallback stop, scoped list, unlink without fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 20:01:25 -04:00

43 lines
1.4 KiB
Python

import asyncio
import discord
from discord.ext import commands
from app.config import settings
from app.internal.guild_gate import GatedTree
from app.internal.logger import logger
class DRBBot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
# GatedTree: unlinked servers get only /drb link and /help (server-26#213).
super().__init__(command_prefix="!", intents=intents, tree_cls=GatedTree)
async def setup_hook(self):
await self.load_extension("app.commands.radio")
await self.load_extension("app.commands.drb")
await self.load_extension("app.commands.trips")
if settings.dev_guild_id:
guild = discord.Object(id=settings.dev_guild_id)
self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)
logger.info(f"Slash commands synced to dev guild {settings.dev_guild_id}.")
else:
await self.tree.sync()
logger.info("Slash commands synced globally.")
async def on_ready(self):
logger.info(f"Bot ready: {self.user} ({self.user.id})")
await self.change_presence(
activity=discord.Activity(
type=discord.ActivityType.listening,
name="the radio"
)
)
bot = DRBBot()
if __name__ == "__main__":
bot.run(settings.discord_token)