Skip to content

Commit 23df579

Browse files
authored
fix(discord): classify announcement threads consistently (#128568)
* refactor(discord): deduplicate thread channel classification * fix(discord): classify announcement threads during channel resolution
1 parent d1e4435 commit 23df579

13 files changed

Lines changed: 42 additions & 75 deletions

‎extensions/discord/src/actions/runtime.guild.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Discord plugin module implements runtime.guild behavior.
2-
import { ChannelType, PermissionFlagsBits } from "discord-api-types/v10";
2+
import { PermissionFlagsBits } from "discord-api-types/v10";
33
import type { AgentToolResult } from "openclaw/plugin-sdk/agent-core";
44
import { resolveDefaultDiscordAccountId } from "../accounts.js";
5+
import { isDiscordThreadChannelType } from "../channel-type.js";
56
import { getGateway } from "../monitor/gateway-registry.js";
67
import { getPresence } from "../monitor/presence-cache.js";
78
import {
@@ -106,14 +107,6 @@ const guildAdminActionGuards: Partial<Record<string, GuildAdminActionGuard>> = {
106107
channelPermissionRemove: channelPermissionGuard,
107108
};
108109

109-
function isThreadChannelType(channelType: number | undefined) {
110-
return (
111-
channelType === ChannelType.GuildNewsThread ||
112-
channelType === ChannelType.GuildPublicThread ||
113-
channelType === ChannelType.GuildPrivateThread
114-
);
115-
}
116-
117110
function isLockedThreadChannel(channel: unknown) {
118111
if (!channel || typeof channel !== "object") {
119112
return false;
@@ -186,7 +179,7 @@ async function resolveGuildAdminActionPermissions(params: {
186179
createDiscordActionOptions({ cfg: params.cfg, accountId: params.accountId }),
187180
);
188181
const channelType = "type" in channel ? channel.type : undefined;
189-
if (!isThreadChannelType(channelType)) {
182+
if (!isDiscordThreadChannelType(channelType)) {
190183
return params.guard.permissions;
191184
}
192185

‎extensions/discord/src/actions/runtime.messaging.send.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Discord plugin module implements runtime.messaging.send behavior.
22
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
3+
import { isDiscordThreadChannelType } from "../channel-type.js";
34
import {
45
createReusableDiscordReplyReference,
56
resolveDiscordReplyReference,
@@ -13,7 +14,6 @@ import {
1314
readStringParam,
1415
} from "../runtime-api.js";
1516
import { DiscordThreadInitialMessageError } from "../send.js";
16-
import { isThreadChannelType } from "../send.permissions.js";
1717
import type { DiscordSendComponents, DiscordSendEmbeds } from "../send.shared.js";
1818
import { discordMessagingActionRuntime } from "./runtime.messaging.runtime.js";
1919
import type { DiscordMessagingActionContext } from "./runtime.messaging.shared.js";
@@ -144,7 +144,7 @@ async function appendDiscordThreadRenameResult(
144144
channelId,
145145
ctx.withOpts(),
146146
);
147-
if (!isThreadChannelType(channel.type)) {
147+
if (!isDiscordThreadChannelType(channel.type)) {
148148
return {
149149
...params.payload,
150150
warning: "Discord threadName was ignored because the send target is not a thread.",

‎extensions/discord/src/actions/runtime.messaging.shared.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ChannelMessageActionContext } from "openclaw/plugin-sdk/channel-co
44
// Discord plugin module implements runtime.messaging.shared behavior.
55
import { resolveOpenProviderRuntimeGroupPolicy } from "openclaw/plugin-sdk/runtime-group-policy";
66
import { mergeDiscordAccountConfig, resolveDefaultDiscordAccountId } from "../accounts.js";
7+
import { isDiscordThreadChannelType } from "../channel-type.js";
78
import { createDiscordRuntimeAccountContext } from "../client.js";
89
import {
910
isDiscordGroupAllowedByPolicy,
@@ -192,8 +193,7 @@ function readDiscordChannelType(value: unknown): number | undefined {
192193
}
193194

194195
function isDiscordThreadChannel(value: unknown): boolean {
195-
const type = readDiscordChannelType(value);
196-
return type === 10 || type === 11 || type === 12;
196+
return isDiscordThreadChannelType(readDiscordChannelType(value));
197197
}
198198

199199
function isDiscordReadAncestryAllowed(params: {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ChannelType } from "discord-api-types/v10";
2+
3+
export function isDiscordThreadChannelType(channelType: unknown): boolean {
4+
return (
5+
channelType === ChannelType.AnnouncementThread ||
6+
channelType === ChannelType.PublicThread ||
7+
channelType === ChannelType.PrivateThread
8+
);
9+
}

‎extensions/discord/src/monitor/agent-components-context.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { ChannelType } from "discord-api-types/v10";
33
import { logError } from "openclaw/plugin-sdk/logging-core";
44
import { resolveAgentRoute } from "openclaw/plugin-sdk/routing";
5+
import { isDiscordThreadChannelType } from "../channel-type.js";
56
import type {
67
AgentComponentContext,
78
AgentComponentInteraction,
@@ -19,14 +20,6 @@ function formatUsername(user: { username: string; discriminator?: string | null
1920
return user.username;
2021
}
2122

22-
function isThreadChannelType(channelType: number | undefined): boolean {
23-
return (
24-
channelType === ChannelType.PublicThread ||
25-
channelType === ChannelType.PrivateThread ||
26-
channelType === ChannelType.AnnouncementThread
27-
);
28-
}
29-
3023
export function resolveAgentComponentRoute(params: {
3124
ctx: AgentComponentContext;
3225
rawGuildId: string | undefined;
@@ -86,7 +79,7 @@ export function resolveDiscordChannelContext(
8679
const channelSlug = channelName ? normalizeDiscordSlug(channelName) : "";
8780
const displayChannelSlug = channelName ? normalizeDiscordDisplaySlug(channelName) : "";
8881
const channelType = channelInfo.type;
89-
const isThread = isThreadChannelType(channelType);
82+
const isThread = isDiscordThreadChannelType(channelType);
9083

9184
let parentId: string | undefined;
9285
let parentName: string | undefined;

‎extensions/discord/src/monitor/message-handler.preflight-helpers.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
} from "openclaw/plugin-sdk/channel-inbound";
66
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
77
import { findCodeRegions, isInsideCode } from "openclaw/plugin-sdk/text-chunking";
8-
import { ChannelType, type Message } from "../internal/discord.js";
8+
import { isDiscordThreadChannelType } from "../channel-type.js";
9+
import type { Message } from "../internal/discord.js";
910
import type { DiscordMessagePreflightParams } from "./message-handler.preflight.types.js";
1011
import type { DiscordChannelInfo } from "./message-utils.js";
1112

@@ -33,14 +34,6 @@ type BoundThreadLookupRecordLike = {
3334
};
3435
};
3536

36-
function isDiscordThreadChannelType(type: ChannelType | undefined): boolean {
37-
return (
38-
type === ChannelType.PublicThread ||
39-
type === ChannelType.PrivateThread ||
40-
type === ChannelType.AnnouncementThread
41-
);
42-
}
43-
4437
export function isDiscordThreadChannelMessage(params: {
4538
isGuildMessage: boolean;
4639
message: Message;

‎extensions/discord/src/monitor/thread-bindings.discord-api.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Discord API module exposes the plugin public contract.
2-
import { ChannelType } from "discord-api-types/v10";
32
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
43
import { parseStrictNonNegativeInteger } from "openclaw/plugin-sdk/number-runtime";
54
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
65
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
6+
import { isDiscordThreadChannelType } from "../channel-type.js";
77
import { createDiscordRestClient } from "../client.js";
88
import { createChannelWebhook, getChannel } from "../internal/discord.js";
99
import { sendMessageDiscord, sendWebhookMessageDiscord } from "../send.js";
@@ -47,14 +47,6 @@ export function isThreadArchived(raw: unknown): boolean {
4747
return false;
4848
}
4949

50-
function isThreadChannelType(type: unknown): boolean {
51-
return (
52-
type === ChannelType.PublicThread ||
53-
type === ChannelType.PrivateThread ||
54-
type === ChannelType.AnnouncementThread
55-
);
56-
}
57-
5850
function normalizeDiscordBindingChannelId(raw?: string | null): string | null {
5951
const trimmed = normalizeOptionalString(raw) ?? "";
6052
if (!trimmed) {
@@ -263,7 +255,7 @@ export async function resolveChannelIdForBinding(params: {
263255
const parentId = normalizeOptionalString(channelInfo.parentId) ?? "";
264256
// Only thread channels should resolve to their parent channel.
265257
// Non-thread channels (text/forum/media) must keep their own ID.
266-
if (parentId && isThreadChannelType(type)) {
258+
if (parentId && isDiscordThreadChannelType(type)) {
267259
return parentId;
268260
}
269261
return channelId || null;

‎extensions/discord/src/monitor/thread-channel-context.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Discord plugin module implements thread channel context behavior.
2-
import { ChannelType } from "../internal/discord.js";
2+
import { isDiscordThreadChannelType } from "../channel-type.js";
3+
import type { ChannelType } from "../internal/discord.js";
34
import { normalizeDiscordSlug } from "./allow-list.js";
45
import {
56
resolveDiscordChannelIdSafe,
@@ -26,14 +27,6 @@ type DiscordThreadLikeChannelContext = {
2627
channelInfo: DiscordChannelInfo | null;
2728
};
2829

29-
function isDiscordThreadChannelType(type: ChannelType | number | undefined): boolean {
30-
return (
31-
type === ChannelType.PublicThread ||
32-
type === ChannelType.PrivateThread ||
33-
type === ChannelType.AnnouncementThread
34-
);
35-
}
36-
3730
function buildFetchedChannelInfo(channel: unknown): DiscordChannelInfo | null {
3831
const channelInfo = resolveDiscordChannelInfoSafe(channel);
3932
if (channelInfo.type === undefined) {

‎extensions/discord/src/monitor/threading.starter.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ReplyToMode } from "openclaw/plugin-sdk/config-contracts";
33
import { createReplyReferencePlanner } from "openclaw/plugin-sdk/reply-reference";
44
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
55
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
6+
import { isDiscordThreadChannelType } from "../channel-type.js";
67
import { ChannelType, getChannelMessage, type Client } from "../internal/discord.js";
78
import {
89
resolveDiscordChannelIdSafe,
@@ -31,14 +32,6 @@ import type {
3132
DiscordThreadStarterRestMessage,
3233
} from "./threading.types.js";
3334

34-
function isDiscordThreadType(type: ChannelType | undefined): boolean {
35-
return (
36-
type === ChannelType.PublicThread ||
37-
type === ChannelType.PrivateThread ||
38-
type === ChannelType.AnnouncementThread
39-
);
40-
}
41-
4235
function isDiscordForumParentType(parentType: ChannelType | undefined): boolean {
4336
return parentType === ChannelType.GuildForum || parentType === ChannelType.GuildMedia;
4437
}
@@ -63,7 +56,7 @@ export function resolveDiscordThreadChannel(params: {
6356
if (isThreadChannel) {
6457
return channel as unknown as DiscordThreadChannel;
6558
}
66-
if (!isDiscordThreadType(channelInfo?.type)) {
59+
if (!isDiscordThreadChannelType(channelInfo?.type)) {
6760
return null;
6861
}
6962
const messageChannelId =

‎extensions/discord/src/resolve-channels.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Discord tests cover resolve channels plugin behavior.
2+
import { ChannelType } from "discord-api-types/v10";
23
import { withFetchPreconnect } from "openclaw/plugin-sdk/test-env";
34
import { describe, expect, it } from "vitest";
45
import { resolveDiscordChannelAllowlist } from "./resolve-channels.js";
@@ -67,14 +68,19 @@ describe("resolveDiscordChannelAllowlist", () => {
6768
expect(res[0]?.guildId).toBe("111");
6869
}
6970

70-
it("resolves guild/channel by name", async () => {
71+
it.each([
72+
["announcement", ChannelType.AnnouncementThread],
73+
["public", ChannelType.PublicThread],
74+
["private", ChannelType.PrivateThread],
75+
])("prefers a matching guild channel over a %s thread", async (_kind, threadType) => {
7176
const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
7277
const url = urlToString(input);
7378
if (url.endsWith("/users/@me/guilds")) {
7479
return jsonResponse([{ id: "g1", name: "My Guild" }]);
7580
}
7681
if (url.endsWith("/guilds/g1/channels")) {
7782
return jsonResponse([
83+
{ id: "thread-1", name: "general", guild_id: "g1", type: threadType },
7884
{ id: "c1", name: "general", guild_id: "g1", type: 0 },
7985
{ id: "c2", name: "random", guild_id: "g1", type: 0 },
8086
]);

0 commit comments

Comments
 (0)