fix(discord): fallback thread parent lookup when parentId missing

This commit is contained in:
Zongxin Yang
2026-02-23 19:20:06 -05:00
committed by Peter Steinberger
parent fd24b35449
commit d883ecade6
2 changed files with 52 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
import { ChannelType } from "@buape/carbon";
import { describe, expect, it, vi } from "vitest";
import { resolveDiscordThreadParentInfo } from "./threading.js";
describe("resolveDiscordThreadParentInfo", () => {
it("falls back to fetched thread parentId when parentId is missing in payload", async () => {
const fetchChannel = vi.fn(async (channelId: string) => {
if (channelId === "thread-1") {
return {
id: "thread-1",
type: ChannelType.PublicThread,
name: "thread-name",
parentId: "parent-1",
};
}
if (channelId === "parent-1") {
return {
id: "parent-1",
type: ChannelType.GuildText,
name: "parent-name",
};
}
return null;
});
const client = {
fetchChannel,
} as unknown as import("@buape/carbon").Client;
const result = await resolveDiscordThreadParentInfo({
client,
threadChannel: {
id: "thread-1",
parentId: undefined,
},
channelInfo: null,
});
expect(fetchChannel).toHaveBeenCalledWith("thread-1");
expect(fetchChannel).toHaveBeenCalledWith("parent-1");
expect(result).toEqual({
id: "parent-1",
name: "parent-name",
type: ChannelType.GuildText,
});
});
});

View File

@@ -131,8 +131,12 @@ export async function resolveDiscordThreadParentInfo(params: {
channelInfo: import("./message-utils.js").DiscordChannelInfo | null;
}): Promise<DiscordThreadParentInfo> {
const { threadChannel, channelInfo, client } = params;
const parentId =
let parentId =
threadChannel.parentId ?? threadChannel.parent?.id ?? channelInfo?.parentId ?? undefined;
if (!parentId && threadChannel.id) {
const threadInfo = await resolveDiscordChannelInfo(client, threadChannel.id);
parentId = threadInfo?.parentId ?? undefined;
}
if (!parentId) {
return {};
}