From eaaeb8a6fd36a06ce33ecb80940cf6fbe9520a44 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Mar 2026 10:30:34 +0800 Subject: [PATCH] fix(discord): use rctx for slash command ReplyCtx (undefined ictx) Made-with: Cursor --- platform/discord/discord.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/platform/discord/discord.go b/platform/discord/discord.go index e3bd8fc..990d8b4 100644 --- a/platform/discord/discord.go +++ b/platform/discord/discord.go @@ -539,13 +539,6 @@ func (p *Platform) handleInteraction(s *discordgo.Session, i *discordgo.Interact return } - if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, - }); err != nil { - slog.Error("discord: defer interaction failed", "error", err) - return - } - data := i.ApplicationCommandData() cmdText := reconstructCommand(data) channelID := i.ChannelID @@ -553,17 +546,28 @@ func (p *Platform) handleInteraction(s *discordgo.Session, i *discordgo.Interact slog.Debug("discord: slash command", "user", userName, "command", cmdText, "channel", channelID) sessionKey := resolveSessionKeyForChannel(channelID, userID, p.shareSessionInChannel, p.threadIsolation, sessionThreadOps{session: p.session}) - ictx := &interactionReplyCtx{ + + // Try to defer the interaction response. If it fails (e.g. 404 Unknown interaction + // when the interaction was already acknowledged by another bot instance), fall back + // to sending replies as ordinary channel messages. + var rctx any + rctx = &interactionReplyCtx{ interaction: i.Interaction, channelID: channelID, } + if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, + }); err != nil { + slog.Warn("discord: defer interaction failed, falling back to channel message", "error", err) + rctx = replyContext{channelID: channelID} + } msg := &core.Message{ SessionKey: sessionKey, Platform: "discord", MessageID: i.ID, UserID: userID, UserName: userName, ChatName: p.resolveChannelName(channelID), - Content: cmdText, ReplyCtx: ictx, + Content: cmdText, ReplyCtx: rctx, } p.handler(p, msg) }