Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GLOSSARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ The words this codebase uses for its own concepts, and the decisions that shape

**Cache invalidation uses Redis pub/sub.** Instances tell each other about changed guild settings over the existing Redis event bus (`core/clustering/`). Postgres `LISTEN/NOTIFY` triggers were considered, so outside writers such as the dashboard backend would invalidate caches automatically, and rejected until the monorepo move.

**Message visibility is configured per message, not in code.** Whether a response is ephemeral (and whether it deletes itself) comes from its localization key, which the dashboard will make editable per message. Commands don't hard-code `ephemeral`, and the bot doesn't defer slash commands automatically (a deferred response's visibility can't be changed afterwards). Revisit deferring when the dashboard work starts.
**Message visibility is configured per message, not in code.** Whether a response is ephemeral (and whether it deletes itself) comes from its localization key, which the dashboard will make editable per message. Commands don't hard-code `ephemeral`, and the bot doesn't defer slash commands automatically (a deferred response's visibility can't be changed afterwards). Commands that can take longer than Discord's 3-second window (rank card, snapshots, `voice play`, `info ip`, `info template`) call `ctx.defer()` themselves, so their replies are public. Revisit deferring when the dashboard work starts.
2 changes: 2 additions & 0 deletions cogs/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async def role(self, ctx: Context, role: discord.Role | None = None):

@info.command(user=False, l10n_key="ipinfo")
async def ip(self, ctx: Context, ip_addr: str):
await ctx.defer() # slow: calls ipinfo.io
try:
ip_json = await self.client.request(f"https://ipinfo.io/{ip_addr}/json")
except RuntimeError:
Expand Down Expand Up @@ -124,6 +125,7 @@ async def channel(self, ctx: Context, channel: discord.abc.GuildChannel):

@info.command(l10n_key="tmplteinfo")
async def template(self, ctx: Context, template: str):
await ctx.defer() # slow: fetches the template from Discord
regex = DISCORD_TEMPLATE.search(template)
if regex:
template_code = regex.group(1)
Expand Down
1 change: 1 addition & 0 deletions cogs/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ async def set_role_multiplier(self, guild_id: int, role_id: int, multiplier: flo
async def level_command(self, ctx: Context, member: discord.Member | None = None) -> None:
if not ctx.guild:
return
await ctx.defer() # slow: renders the rank card image

target = member or (ctx.author if isinstance(ctx.author, discord.Member) else None)
if not target:
Expand Down
2 changes: 2 additions & 0 deletions cogs/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,15 @@ async def load_snapshot(self, ctx: Context, payload: dict):
@group(permissions=["administrator"], user=False)
async def snapshot(self, ctx: Context):
assert ctx.guild is not None
await ctx.defer() # slow: reads every role and channel
code = await self.create_snapshot(ctx)

await ctx.send("snapshot.create", code=code)

@snapshot.command(l10n_key="ss_load", permissions=["administrator"])
async def load(self, ctx: Context, code: str):
assert ctx.guild is not None
await ctx.defer() # slow: recreates roles and channels
payload = await self.get_snapshot(code)
if not payload:
await ctx.send("snapshot.not_found")
Expand Down
1 change: 1 addition & 0 deletions cogs/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ async def on_voice_state_update(

@command(user=False)
async def play(self, ctx: Context, *, query: str):
await ctx.defer() # slow: connects to voice and searches Lavalink
player = await self._get_player(ctx, connect=True)
if not player:
return
Expand Down
17 changes: 17 additions & 0 deletions tests/test_level_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ async def test_level_command_sends_card(level_cog):
ctx.author = MagicMock(spec=discord.Member)
ctx.author.id = 111
ctx.send = AsyncMock()
ctx.defer = AsyncMock()

level_cog.client.db.fetchrow.side_effect = [
{"xp": 120, "level": 1}, # user_levels row
Expand Down Expand Up @@ -422,3 +423,19 @@ async def fetch(query, *args):
await level_cog.get_role_overrides(guild_id)
assert level_cog.client.db.fetch.await_count == 4
level_cog.client.event_bus.publish.assert_awaited()


@pytest.mark.asyncio
async def test_level_command_defers_before_rendering(level_cog):
"""Rendering the card can exceed Discord's 3-second window, so the interaction is acknowledged first."""
ctx = MagicMock()
ctx.guild = None
ctx.defer = AsyncMock()
await level_cog.level_command.callback(level_cog, ctx)
ctx.defer.assert_not_awaited() # DMs return before any work

ctx.guild = MagicMock()
ctx.author = MagicMock(spec=discord.User) # not a member: no card, but the interaction was already acknowledged
ctx.send = AsyncMock()
await level_cog.level_command.callback(level_cog, ctx)
ctx.defer.assert_awaited_once()
Loading