fix(discord): resolve slash commands from interaction payload instead of gateway cache #98

Closed
Toutsu wants to merge 0 commits from fix/discord-slash-commands-guild-context into main
Owner

Summary

Discord slash-команды /listsessions, /newsession, /reschedule возвращали ошибку "This command can only be used in a guild", даже когда вызывались внутри канала сервера.

Root cause: Context.Guild в NetCord берется из gateway client cache (cache.Guilds.GetValueOrDefault(guildId)), а не из JSON payload интеракции. После рестарта бота guild может еще не быть в кэше, и Context.Guild = null.

Fix: Используем Context.Interaction.GuildId из payload + REST API GetGuildAsync/GetGuildUserAsync для получения guild и member.

Changes

  • DiscordListSessionsCommand.csContext.Interaction.GuildId вместо Context.Guild.Id
  • DiscordNewSessionCommand.cs — REST запросы для guild/member, GetResolvedPermissions на RestGuild + GuildUser
  • DiscordRescheduleCommand.cs — аналогично
  • Версия синхронизирована до 3.0.5

Test plan

  • dotnet build src/GmRelay.DiscordBot — успешно
  • Полный test suite — 271/271 зеленых

Workflow

  • CI passes
  • Code review approved
  • Deployed
  • Release published
## Summary Discord slash-команды `/listsessions`, `/newsession`, `/reschedule` возвращали ошибку "This command can only be used in a guild", даже когда вызывались внутри канала сервера. **Root cause:** `Context.Guild` в NetCord берется из gateway client cache (`cache.Guilds.GetValueOrDefault(guildId)`), а не из JSON payload интеракции. После рестарта бота guild может еще не быть в кэше, и `Context.Guild` = null. **Fix:** Используем `Context.Interaction.GuildId` из payload + REST API `GetGuildAsync`/`GetGuildUserAsync` для получения guild и member. ## Changes - `DiscordListSessionsCommand.cs` — `Context.Interaction.GuildId` вместо `Context.Guild.Id` - `DiscordNewSessionCommand.cs` — REST запросы для guild/member, `GetResolvedPermissions` на `RestGuild` + `GuildUser` - `DiscordRescheduleCommand.cs` — аналогично - Версия синхронизирована до `3.0.5` ## Test plan - [x] `dotnet build src/GmRelay.DiscordBot` — успешно - [x] Полный test suite — 271/271 зеленых ## Workflow - [ ] CI passes - [ ] Code review approved - [ ] Deployed - [ ] Release published
Toutsu added 1 commit 2026-05-25 18:02:46 +03:00
Context.Guild in NetCord resolves the Guild object from the gateway client cache
(cache.Guilds.GetValueOrDefault(guildId)), not from the interaction JSON payload.
After a bot restart, the guild may not yet be cached when the first slash command
arrives, causing Context.Guild to be null even though the command is invoked
inside a guild channel. This produced "This command can only be used in a guild."

Changes:
- DiscordListSessionsCommand: use Context.Interaction.GuildId instead of Context.Guild.Id
- DiscordNewSessionCommand: use Context.Interaction.GuildId + REST GetGuildAsync/GetGuildUserAsync
- DiscordRescheduleCommand: same as above
- DiscordSessionInteractionModule: same fix for button interactions (CreateInput)
- Add null guard in GetResolvedPermissions for safety
- Bump version to 3.0.5

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Toutsu force-pushed fix/discord-slash-commands-guild-context from 3305ca1070 to f6d5281af8 2026-05-25 18:02:46 +03:00 Compare
Toutsu reviewed 2026-05-25 18:12:56 +03:00
Toutsu left a comment
Author
Owner

Approved after CI fix. All issues from previous review addressed: DiscordSessionInteractionModule fixed, null guard added to GetResolvedPermissions.

Approved after CI fix. All issues from previous review addressed: DiscordSessionInteractionModule fixed, null guard added to GetResolvedPermissions.
Toutsu closed this pull request 2026-05-26 16:06:28 +03:00
Some checks are pending
PR Checks / test-and-build (pull_request) Successful in 8m46s

Pull request closed

Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Toutsu/GmRelayBot#98