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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
dc1df72c86
commit
455c55f5c2
@@ -0,0 +1,55 @@
|
||||
"""/drb — server-level DRB setup. Today: /drb link (server-26#213 item 1)."""
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
|
||||
from app.internal import guild_gate
|
||||
from app.internal.c2_client import c2
|
||||
|
||||
|
||||
class DRBCommands(commands.Cog):
|
||||
drb = app_commands.Group(
|
||||
name="drb",
|
||||
description="Set up DRB in this server.",
|
||||
guild_only=True,
|
||||
default_permissions=discord.Permissions(manage_guild=True),
|
||||
)
|
||||
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
|
||||
@drb.command(name="link", description="Link this server to your DRB organization.")
|
||||
@app_commands.describe(code="The code from DRB → Settings → Organization → Discord servers.")
|
||||
async def link(self, interaction: discord.Interaction, code: str):
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
# default_permissions is only the default — a server admin can grant
|
||||
# the command to anyone under Integrations. Linking decides which
|
||||
# organization's radio this whole server gets, so hold the line here.
|
||||
perms = getattr(interaction.user, "guild_permissions", None)
|
||||
if not perms or not perms.manage_guild:
|
||||
await interaction.followup.send("Only members with **Manage Server** can link this server.",
|
||||
ephemeral=True)
|
||||
return
|
||||
ok, body = await c2.redeem_guild_link(
|
||||
interaction.guild_id, code, str(interaction.user.id),
|
||||
interaction.guild.name if interaction.guild else None,
|
||||
)
|
||||
guild_gate.forget(interaction.guild_id)
|
||||
radio = self.bot.get_cog("RadioCommands")
|
||||
if radio is not None:
|
||||
radio._overview.pop(interaction.guild_id, None)
|
||||
if not ok:
|
||||
await interaction.followup.send(body.get("detail") or "DRB could not link this server.",
|
||||
ephemeral=True)
|
||||
return
|
||||
org = body.get("org_name") or "your DRB organization"
|
||||
if body.get("already_linked"):
|
||||
text = f"This server was already linked to **{org}**. Nothing changed."
|
||||
else:
|
||||
text = (f"This server is now linked to **{org}**.\n"
|
||||
"`/join`, `/leave` and `/status` now act for that organization only.")
|
||||
await interaction.followup.send(text, ephemeral=True)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(DRBCommands(bot))
|
||||
Reference in New Issue
Block a user