Skip to content

Commit 48aa435

Browse files
authored
fix(discord): use cached author.id when DMChannel.recipientId is null (#1365)
DMChannel.recipientId can be null when client.channels.fetch() returns a DM channel with a cold cache. The inbound gate correctly uses msg.author.id, but fetchAllowedChannel relied on recipientId, so replies to allowlisted DMs intermittently failed with "channel not allowlisted" after session restart. Maintain a channelId→userId map populated during inbound handling and fall back to it when recipientId is null. Fixes anthropics/claude-code#40576 Fixes anthropics/claude-code#41647 🏠 Remote-Dev: homespace
1 parent 7e401ed commit 48aa435

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

external_plugins/discord/server.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ type GateResult =
222222
const recentSentIds = new Set<string>()
223223
const RECENT_SENT_CAP = 200
224224

225+
const dmChannelUsers = new Map<string, string>()
226+
225227
function noteSent(id: string): void {
226228
recentSentIds.add(id)
227229
if (recentSentIds.size > RECENT_SENT_CAP) {
@@ -404,7 +406,8 @@ async function fetchAllowedChannel(id: string) {
404406
const ch = await fetchTextChannel(id)
405407
const access = loadAccess()
406408
if (ch.type === ChannelType.DM) {
407-
if (access.allowFrom.includes(ch.recipientId)) return ch
409+
const userId = ch.recipientId ?? dmChannelUsers.get(id)
410+
if (userId && access.allowFrom.includes(userId)) return ch
408411
} else {
409412
const key = ch.isThread() ? ch.parentId ?? ch.id : ch.id
410413
if (key in access.groups) return ch
@@ -823,6 +826,10 @@ async function handleInbound(msg: Message): Promise<void> {
823826

824827
const chat_id = msg.channelId
825828

829+
if (msg.channel.type === ChannelType.DM) {
830+
dmChannelUsers.set(chat_id, msg.author.id)
831+
}
832+
826833
// Permission-reply intercept: if this looks like "yes xxxxx" for a
827834
// pending permission request, emit the structured event instead of
828835
// relaying as chat. The sender is already gate()-approved at this point

0 commit comments

Comments
 (0)