@kodelyth/discord 2026.5.39 → 2026.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/dist/account-inspect-Dqw-enky.js +81 -0
  2. package/dist/account-inspect-api.js +10 -0
  3. package/dist/accounts-B7OBFePq.js +224 -0
  4. package/dist/action-runtime-api.js +2 -0
  5. package/dist/agent-components.runtime-DVY_1VB4.js +4 -0
  6. package/dist/allow-list-B0s7evD7.js +354 -0
  7. package/dist/api-CXAcv9nZ.js +130 -0
  8. package/dist/api.js +23 -0
  9. package/dist/approval-handler.runtime-B9xUAF3n.js +426 -0
  10. package/dist/audit-DoiK49WO.js +24 -0
  11. package/dist/audit-core-BGrq3G7r.js +105 -0
  12. package/dist/channel-U_aeoFwW.js +795 -0
  13. package/dist/channel-actions-BxEBnEuv.js +173 -0
  14. package/dist/channel-actions.runtime-CPtpH-yl.js +263 -0
  15. package/dist/channel-api-BfjklLby.js +21 -0
  16. package/dist/channel-config-api.js +2 -0
  17. package/dist/channel-plugin-api.js +2 -0
  18. package/dist/channel.setup-BUSC0apv.js +337 -0
  19. package/dist/components-luonoe13.js +909 -0
  20. package/dist/config-api-DSYGqaLQ.js +2 -0
  21. package/dist/config-schema-DIqJBGwC.js +357 -0
  22. package/dist/configured-state.js +6 -0
  23. package/dist/contract-api.js +8 -0
  24. package/dist/conversation-identity-DXAm0_Mk.js +270 -0
  25. package/dist/directory-config-CYbuMmPS.js +49 -0
  26. package/dist/directory-contract-api.js +2 -0
  27. package/dist/directory-live-DX4dLRpJ.js +159 -0
  28. package/dist/doctor-bbKSvGVD.js +244 -0
  29. package/dist/doctor-contract-Btjt6NJD.js +383 -0
  30. package/dist/doctor-contract-api.js +2 -0
  31. package/dist/gateway-registry-BKSpa4GB.js +74 -0
  32. package/dist/handle-action.guild-admin-B5BArS2n.js +286 -0
  33. package/dist/inbound-context-WAOqhGlT.js +48 -0
  34. package/dist/inbound-event-delivery-C-1Ji3WP.js +65 -0
  35. package/dist/index.js +26 -0
  36. package/dist/manager.runtime-DXHynKE4.js +2356 -0
  37. package/dist/message-handler-mXzc3tA_.js +381 -0
  38. package/dist/message-handler.preflight-BPD1a347.js +1113 -0
  39. package/dist/message-handler.process-GUa3aV8z.js +1438 -0
  40. package/dist/message-utils-dUbem16p.js +549 -0
  41. package/dist/outbound-adapter-C18OAc1y.js +536 -0
  42. package/dist/pluralkit-D1Q2x0w5.js +22 -0
  43. package/dist/preflight-audio-CZtpWcIm.js +72 -0
  44. package/dist/preflight-audio.runtime-Brx_0_xW.js +7 -0
  45. package/dist/preview-streaming-D_slNIiO.js +8 -0
  46. package/dist/probe-D--Ca4JF.js +139 -0
  47. package/dist/probe.runtime-DQBchZzv.js +2 -0
  48. package/dist/provider-B2-31CIT.js +9565 -0
  49. package/dist/provider-session.runtime-BwzzSsrH.js +6 -0
  50. package/dist/provider.runtime-CP3oHLls.js +2 -0
  51. package/dist/resolve-allowlist-common-CqxPLcJO.js +34 -0
  52. package/dist/resolve-channels-0LX4pUbB.js +265 -0
  53. package/dist/resolve-users-CztOv0Qs.js +120 -0
  54. package/dist/runtime-DUaw66V_.js +1073 -0
  55. package/dist/runtime-api.actions.js +3 -0
  56. package/dist/runtime-api.js +30 -0
  57. package/dist/runtime-api.lookup.js +7 -0
  58. package/dist/runtime-api.monitor-CvVKvEXW.js +5 -0
  59. package/dist/runtime-api.monitor.js +8 -0
  60. package/dist/runtime-api.send.js +6 -0
  61. package/dist/runtime-api.threads.js +6 -0
  62. package/dist/runtime-fC6f4UF2.js +8 -0
  63. package/dist/runtime-setter-api.js +2 -0
  64. package/dist/secret-config-contract-B6WW5V88.js +115 -0
  65. package/dist/secret-contract-api.js +2 -0
  66. package/dist/security-audit-CnyIQKz6.js +120 -0
  67. package/dist/security-audit-contract-api.js +2 -0
  68. package/dist/security-audit.runtime-CQSkjNLu.js +2 -0
  69. package/dist/security-contract-DLvYOgLM.js +26 -0
  70. package/dist/security-contract-api.js +2 -0
  71. package/dist/security-doctor-DepqtNCI.js +18 -0
  72. package/dist/send-DCtPCHGk.js +881 -0
  73. package/dist/send.components-Bcgxvm52.js +474 -0
  74. package/dist/send.outbound-S9t0UuHc.js +330 -0
  75. package/dist/send.receipt-CDn3GBWC.js +3119 -0
  76. package/dist/send.shared-D4iBnAmn.js +669 -0
  77. package/dist/sender-identity-CxCe3_1a.js +43 -0
  78. package/dist/session-contract-Dwhw3RTY.js +6 -0
  79. package/dist/session-key-api.js +2 -0
  80. package/dist/session-key-normalization-CP8dPUid.js +23 -0
  81. package/dist/setup-entry.js +11 -0
  82. package/dist/setup-plugin-api.js +2 -0
  83. package/dist/shared-AIlvuZXt.js +171 -0
  84. package/dist/subagent-hooks-8bK-mgiU.js +120 -0
  85. package/dist/subagent-hooks-api.js +22 -0
  86. package/dist/system-events-Ba1TklaL.js +34 -0
  87. package/dist/target-resolver-BrtFQtoK.js +82 -0
  88. package/dist/targets-DWLLZE2l.js +3 -0
  89. package/dist/test-api.js +45 -0
  90. package/dist/thread-binding-api.js +4 -0
  91. package/dist/thread-bindings-9aKRmZv0.js +255 -0
  92. package/dist/thread-bindings.discord-api-ssGH5wc2.js +244 -0
  93. package/dist/thread-bindings.manager-0YBHGemk.js +534 -0
  94. package/dist/thread-bindings.session-updates-DJZGIwaU.js +54 -0
  95. package/dist/thread-bindings.state-eTFl-PqJ.js +318 -0
  96. package/dist/timeouts-CEwuGaWT.js +52 -0
  97. package/dist/timeouts.js +2 -0
  98. package/dist/typing-BmJKRpCS.js +14 -0
  99. package/package.json +19 -7
  100. package/account-inspect-api.js +0 -7
  101. package/action-runtime-api.js +0 -7
  102. package/api.js +0 -7
  103. package/channel-config-api.js +0 -7
  104. package/channel-plugin-api.js +0 -7
  105. package/configured-state.js +0 -7
  106. package/contract-api.js +0 -7
  107. package/directory-contract-api.js +0 -7
  108. package/doctor-contract-api.js +0 -7
  109. package/index.js +0 -7
  110. package/runtime-api.actions.js +0 -7
  111. package/runtime-api.js +0 -7
  112. package/runtime-api.lookup.js +0 -7
  113. package/runtime-api.monitor.js +0 -7
  114. package/runtime-api.send.js +0 -7
  115. package/runtime-api.threads.js +0 -7
  116. package/runtime-setter-api.js +0 -7
  117. package/secret-contract-api.js +0 -7
  118. package/security-audit-contract-api.js +0 -7
  119. package/security-contract-api.js +0 -7
  120. package/session-key-api.js +0 -7
  121. package/setup-entry.js +0 -7
  122. package/setup-plugin-api.js +0 -7
  123. package/subagent-hooks-api.js +0 -7
  124. package/test-api.js +0 -7
  125. package/thread-binding-api.js +0 -7
  126. package/timeouts.js +0 -7
@@ -0,0 +1,669 @@
1
+ import { At as getGuildMember, C as RateLimitError, M as serializePayload, kt as getGuild, nt as createUserDmChannel, pt as getChannel, rt as getCurrentUser, s as chunkDiscordTextWithMode, st as createChannelMessage, v as Embed, x as RequestClient } from "./send.receipt-CDn3GBWC.js";
2
+ import { a as mergeDiscordAccountConfig, p as normalizeDiscordToken, s as resolveDiscordAccount } from "./accounts-B7OBFePq.js";
3
+ import { t as parseAndResolveDiscordTarget } from "./target-resolver-BrtFQtoK.js";
4
+ import { normalizeLowercaseStringOrEmpty, normalizeOptionalString } from "klaw/plugin-sdk/string-coerce-runtime";
5
+ import { normalizeAccountId } from "klaw/plugin-sdk/routing";
6
+ import { ChannelType, MessageFlags, PermissionFlagsBits } from "discord-api-types/v10";
7
+ import { PollLayoutType } from "discord-api-types/payloads/v10";
8
+ import { buildOutboundMediaLoadOptions, extensionForMime, normalizePollDurationHours, normalizePollInput } from "klaw/plugin-sdk/media-runtime";
9
+ import { requireRuntimeConfig } from "klaw/plugin-sdk/plugin-config-runtime";
10
+ import { resolveTextChunksWithFallback } from "klaw/plugin-sdk/reply-payload";
11
+ import { loadWebMedia } from "klaw/plugin-sdk/web-media";
12
+ import { isIP } from "node:net";
13
+ import { makeProxyFetch } from "klaw/plugin-sdk/fetch-runtime";
14
+ import { danger } from "klaw/plugin-sdk/runtime-env";
15
+ import { collectErrorGraphCandidates, extractErrorCode, formatErrorMessage, readErrorName } from "klaw/plugin-sdk/error-runtime";
16
+ import { createRateLimitRetryRunner } from "klaw/plugin-sdk/retry-runtime";
17
+ //#region extensions/discord/src/proxy-fetch.ts
18
+ function resolveDiscordProxyUrl(account, cfg) {
19
+ const accountProxy = account.config.proxy?.trim();
20
+ if (accountProxy) return accountProxy;
21
+ const channelProxy = cfg?.channels?.discord?.proxy;
22
+ if (typeof channelProxy !== "string") return;
23
+ return channelProxy.trim() || void 0;
24
+ }
25
+ function resolveDiscordProxyFetchByUrl(proxyUrl, runtime) {
26
+ return withValidatedDiscordProxy(proxyUrl, runtime, (proxy) => makeProxyFetch(proxy));
27
+ }
28
+ function resolveDiscordProxyFetchForAccount(account, cfg, runtime) {
29
+ return resolveDiscordProxyFetchByUrl(resolveDiscordProxyUrl(account, cfg), runtime);
30
+ }
31
+ function withValidatedDiscordProxy(proxyUrl, runtime, createValue) {
32
+ const proxy = proxyUrl?.trim();
33
+ if (!proxy) return;
34
+ try {
35
+ validateDiscordProxyUrl(proxy);
36
+ return createValue(proxy);
37
+ } catch (err) {
38
+ runtime?.error?.(danger(`discord: invalid rest proxy: ${String(err)}`));
39
+ return;
40
+ }
41
+ }
42
+ function validateDiscordProxyUrl(proxyUrl) {
43
+ let parsed;
44
+ try {
45
+ parsed = new URL(proxyUrl);
46
+ } catch {
47
+ throw new Error("Proxy URL must be a valid http or https URL");
48
+ }
49
+ if (!["http:", "https:"].includes(parsed.protocol)) throw new Error("Proxy URL must use http or https");
50
+ if (!isLoopbackProxyHostname(parsed.hostname)) throw new Error("Proxy URL must target a loopback host");
51
+ return proxyUrl;
52
+ }
53
+ function isLoopbackProxyHostname(hostname) {
54
+ const normalized = normalizeLowercaseStringOrEmpty(hostname);
55
+ if (!normalized) return false;
56
+ const bracketless = normalized.startsWith("[") && normalized.endsWith("]") ? normalized.slice(1, -1) : normalized;
57
+ if (bracketless === "localhost") return true;
58
+ const ipFamily = isIP(bracketless);
59
+ if (ipFamily === 4) return bracketless.startsWith("127.");
60
+ if (ipFamily === 6) return bracketless === "::1" || bracketless === "0:0:0:0:0:0:0:1";
61
+ return false;
62
+ }
63
+ //#endregion
64
+ //#region extensions/discord/src/proxy-request-client.ts
65
+ const DISCORD_REST_TIMEOUT_MS = 15e3;
66
+ function createDiscordRequestClient(token, options) {
67
+ if (!options?.fetch) return new RequestClient(token, options);
68
+ return new RequestClient(token, {
69
+ runtimeProfile: "persistent",
70
+ maxQueueSize: 1e3,
71
+ timeout: DISCORD_REST_TIMEOUT_MS,
72
+ ...options,
73
+ fetch: options.fetch
74
+ });
75
+ }
76
+ //#endregion
77
+ //#region extensions/discord/src/retry.ts
78
+ const DISCORD_RETRY_DEFAULTS = {
79
+ attempts: 3,
80
+ minDelayMs: 500,
81
+ maxDelayMs: 3e4,
82
+ jitter: .1
83
+ };
84
+ const DISCORD_RETRYABLE_STATUS_CODES = new Set([408, 429]);
85
+ const DISCORD_RETRYABLE_ERROR_CODES = new Set([
86
+ "EAI_AGAIN",
87
+ "ECONNREFUSED",
88
+ "ECONNRESET",
89
+ "ENETUNREACH",
90
+ "ENOTFOUND",
91
+ "EPIPE",
92
+ "ETIMEDOUT",
93
+ "UND_ERR_BODY_TIMEOUT",
94
+ "UND_ERR_CONNECT_TIMEOUT",
95
+ "UND_ERR_HEADERS_TIMEOUT",
96
+ "UND_ERR_SOCKET"
97
+ ]);
98
+ const DISCORD_TRANSIENT_MESSAGE_RE = /\b(?:bad gateway|fetch failed|network error|networkerror|service unavailable|socket hang up|temporarily unavailable|timed out|timeout)\b|connection (?:closed|reset|refused)/i;
99
+ function readDiscordErrorStatus(err) {
100
+ if (!err || typeof err !== "object") return;
101
+ const raw = "status" in err && err.status !== void 0 ? err.status : "statusCode" in err && err.statusCode !== void 0 ? err.statusCode : void 0;
102
+ if (typeof raw === "number" && Number.isFinite(raw)) return raw;
103
+ if (typeof raw === "string" && /^\d+$/.test(raw)) return Number(raw);
104
+ }
105
+ function isRetryableDiscordTransientError(err) {
106
+ if (err instanceof RateLimitError) return true;
107
+ for (const candidate of collectErrorGraphCandidates(err, (current) => [current.cause, current.error])) {
108
+ const status = readDiscordErrorStatus(candidate);
109
+ if (status !== void 0 && (DISCORD_RETRYABLE_STATUS_CODES.has(status) || status >= 500)) return true;
110
+ const code = extractErrorCode(candidate);
111
+ if (code && DISCORD_RETRYABLE_ERROR_CODES.has(code.toUpperCase())) return true;
112
+ if (readErrorName(candidate) === "AbortError") return true;
113
+ if ((candidate instanceof Error || candidate !== null && typeof candidate === "object") && DISCORD_TRANSIENT_MESSAGE_RE.test(formatErrorMessage(candidate))) return true;
114
+ }
115
+ return false;
116
+ }
117
+ function createDiscordRetryRunner(params) {
118
+ return createRateLimitRetryRunner({
119
+ ...params,
120
+ defaults: DISCORD_RETRY_DEFAULTS,
121
+ logLabel: "discord",
122
+ shouldRetry: isRetryableDiscordTransientError,
123
+ retryAfterMs: (err) => err instanceof RateLimitError ? err.retryAfter * 1e3 : void 0
124
+ });
125
+ }
126
+ //#endregion
127
+ //#region extensions/discord/src/client.ts
128
+ function createDiscordRuntimeAccountContext(params) {
129
+ return {
130
+ cfg: params.cfg,
131
+ accountId: normalizeAccountId(params.accountId)
132
+ };
133
+ }
134
+ function resolveDiscordClientAccountContext(opts, runtime) {
135
+ const resolvedCfg = requireRuntimeConfig(opts.cfg, "Discord client");
136
+ const account = resolveAccountWithoutToken({
137
+ cfg: resolvedCfg,
138
+ accountId: opts.accountId
139
+ });
140
+ return {
141
+ cfg: resolvedCfg,
142
+ account,
143
+ proxyFetch: resolveDiscordProxyFetchForAccount(account, resolvedCfg, runtime)
144
+ };
145
+ }
146
+ function resolveToken(params) {
147
+ const fallback = normalizeDiscordToken(params.fallbackToken, "channels.discord.token");
148
+ if (!fallback) {
149
+ if (params.account.tokenStatus === "configured_unavailable") throw new Error(`Discord bot token configured for account "${params.accountId}" is unavailable; resolve SecretRefs against the active runtime snapshot before using this account.`);
150
+ throw new Error(`Discord bot token missing for account "${params.accountId}" (set discord.accounts.${params.accountId}.token or DISCORD_BOT_TOKEN for default).`);
151
+ }
152
+ return fallback;
153
+ }
154
+ function resolveRest(token, account, cfg, rest, proxyFetch) {
155
+ if (rest) return rest;
156
+ const resolvedProxyFetch = proxyFetch ?? resolveDiscordProxyFetchForAccount(account, cfg);
157
+ return createDiscordRequestClient(token, resolvedProxyFetch ? { fetch: resolvedProxyFetch } : void 0);
158
+ }
159
+ function resolveAccountWithoutToken(params) {
160
+ const accountId = normalizeAccountId(params.accountId);
161
+ const merged = mergeDiscordAccountConfig(params.cfg, accountId);
162
+ const baseEnabled = params.cfg.channels?.discord?.enabled !== false;
163
+ const accountEnabled = merged.enabled !== false;
164
+ return {
165
+ accountId,
166
+ enabled: baseEnabled && accountEnabled,
167
+ name: normalizeOptionalString(merged.name),
168
+ token: "",
169
+ tokenSource: "none",
170
+ tokenStatus: "missing",
171
+ config: merged
172
+ };
173
+ }
174
+ function createDiscordRestClient(opts) {
175
+ const explicitToken = normalizeDiscordToken(opts.token, "channels.discord.token");
176
+ const proxyContext = resolveDiscordClientAccountContext(opts);
177
+ const resolvedCfg = proxyContext.cfg;
178
+ const account = explicitToken ? proxyContext.account : resolveDiscordAccount({
179
+ cfg: resolvedCfg,
180
+ accountId: opts.accountId
181
+ });
182
+ const token = explicitToken ?? resolveToken({
183
+ account,
184
+ accountId: account.accountId,
185
+ fallbackToken: account.token
186
+ });
187
+ return {
188
+ token,
189
+ rest: resolveRest(token, account, resolvedCfg, opts.rest, proxyContext.proxyFetch),
190
+ account
191
+ };
192
+ }
193
+ function createDiscordClient(opts) {
194
+ const { token, rest, account } = createDiscordRestClient(opts);
195
+ return {
196
+ token,
197
+ rest,
198
+ request: createDiscordRetryRunner({
199
+ retry: opts.retry,
200
+ configRetry: account.config.retry,
201
+ verbose: opts.verbose
202
+ })
203
+ };
204
+ }
205
+ function resolveDiscordRest(opts) {
206
+ return createDiscordRestClient(opts).rest;
207
+ }
208
+ //#endregion
209
+ //#region extensions/discord/src/recipient-resolution.ts
210
+ async function parseAndResolveRecipient(raw, cfg, accountId, parseOptions = {}) {
211
+ if (!cfg) throw new Error("Discord recipient resolution requires a resolved runtime config. Load and resolve config at the command or gateway boundary, then pass cfg through the runtime path.");
212
+ const resolvedCfg = requireRuntimeConfig(cfg, "Discord recipient resolution");
213
+ const resolved = await parseAndResolveDiscordTarget(raw, {
214
+ cfg: resolvedCfg,
215
+ accountId: resolveDiscordAccount({
216
+ cfg: resolvedCfg,
217
+ accountId
218
+ }).accountId
219
+ }, parseOptions);
220
+ return {
221
+ kind: resolved.kind,
222
+ id: resolved.id
223
+ };
224
+ }
225
+ //#endregion
226
+ //#region extensions/discord/src/send.permissions.ts
227
+ const PERMISSION_ENTRIES = Object.entries(PermissionFlagsBits).filter(([, value]) => typeof value === "bigint");
228
+ const ALL_PERMISSIONS = PERMISSION_ENTRIES.reduce((acc, [, value]) => acc | value, 0n);
229
+ const ADMINISTRATOR_BIT = PermissionFlagsBits.Administrator;
230
+ function addPermissionBits(base, add) {
231
+ if (!add) return base;
232
+ return base | BigInt(add);
233
+ }
234
+ function removePermissionBits(base, deny) {
235
+ if (!deny) return base;
236
+ return base & ~BigInt(deny);
237
+ }
238
+ function bitfieldToPermissions(bitfield) {
239
+ return PERMISSION_ENTRIES.filter(([, value]) => (bitfield & value) === value).map(([name]) => name).toSorted();
240
+ }
241
+ function hasAdministrator(bitfield) {
242
+ return (bitfield & ADMINISTRATOR_BIT) === ADMINISTRATOR_BIT;
243
+ }
244
+ function hasPermissionBit(bitfield, permission) {
245
+ return (bitfield & permission) === permission;
246
+ }
247
+ function isThreadChannelType(channelType) {
248
+ return channelType === ChannelType.GuildNewsThread || channelType === ChannelType.GuildPublicThread || channelType === ChannelType.GuildPrivateThread;
249
+ }
250
+ async function fetchBotUserId(rest) {
251
+ const me = await getCurrentUser(rest);
252
+ if (!me?.id) throw new Error("Failed to resolve bot user id");
253
+ return me.id;
254
+ }
255
+ function resolveMemberGuildPermissionBits(params) {
256
+ const rolesById = new Map((params.guild.roles ?? []).map((role) => [role.id, role]));
257
+ const everyoneRole = rolesById.get(params.guild.id);
258
+ let permissions = 0n;
259
+ if (everyoneRole?.permissions) permissions = addPermissionBits(permissions, everyoneRole.permissions);
260
+ for (const roleId of params.member.roles ?? []) {
261
+ const role = rolesById.get(roleId);
262
+ if (role?.permissions) permissions = addPermissionBits(permissions, role.permissions);
263
+ }
264
+ return permissions;
265
+ }
266
+ function resolveMemberChannelPermissionBits(params) {
267
+ let permissions = resolveMemberGuildPermissionBits({
268
+ guild: params.guild,
269
+ member: params.member
270
+ });
271
+ if (hasAdministrator(permissions)) return ALL_PERMISSIONS;
272
+ const overwrites = "permission_overwrites" in params.channel ? params.channel.permission_overwrites ?? [] : [];
273
+ for (const overwrite of overwrites) if (overwrite.id === params.guildId) {
274
+ permissions = removePermissionBits(permissions, overwrite.deny ?? "0");
275
+ permissions = addPermissionBits(permissions, overwrite.allow ?? "0");
276
+ }
277
+ let roleDeny = 0n;
278
+ let roleAllow = 0n;
279
+ for (const overwrite of overwrites) if (params.member.roles?.includes(overwrite.id)) {
280
+ roleDeny = addPermissionBits(roleDeny, overwrite.deny ?? "0");
281
+ roleAllow = addPermissionBits(roleAllow, overwrite.allow ?? "0");
282
+ }
283
+ permissions = permissions & ~roleDeny;
284
+ permissions = permissions | roleAllow;
285
+ for (const overwrite of overwrites) if (overwrite.id === params.userId) {
286
+ permissions = removePermissionBits(permissions, overwrite.deny ?? "0");
287
+ permissions = addPermissionBits(permissions, overwrite.allow ?? "0");
288
+ }
289
+ return permissions;
290
+ }
291
+ /**
292
+ * Fetch guild-level permissions for a user. This does not include channel-specific overwrites.
293
+ */
294
+ async function fetchMemberGuildPermissionsDiscord(guildId, userId, opts) {
295
+ const rest = resolveDiscordRest(opts);
296
+ try {
297
+ const [guild, member] = await Promise.all([getGuild(rest, guildId), getGuildMember(rest, guildId, userId)]);
298
+ return resolveMemberGuildPermissionBits({
299
+ guild,
300
+ member
301
+ });
302
+ } catch {
303
+ return null;
304
+ }
305
+ }
306
+ async function canViewDiscordGuildChannel(guildId, channelId, userId, opts) {
307
+ const rest = resolveDiscordRest(opts);
308
+ try {
309
+ const channel = await getChannel(rest, channelId);
310
+ if (("guild_id" in channel ? channel.guild_id : void 0) !== guildId) return false;
311
+ const [guild, member] = await Promise.all([getGuild(rest, guildId), getGuildMember(rest, guildId, userId)]);
312
+ return hasPermissionBit(resolveMemberChannelPermissionBits({
313
+ guildId,
314
+ userId,
315
+ guild,
316
+ member,
317
+ channel
318
+ }), PermissionFlagsBits.ViewChannel);
319
+ } catch {
320
+ return false;
321
+ }
322
+ }
323
+ /**
324
+ * Returns true when the user has ADMINISTRATOR or required permission bits
325
+ * matching the provided predicate.
326
+ */
327
+ async function hasGuildPermissionsDiscord(guildId, userId, requiredPermissions, check, opts) {
328
+ const permissions = await fetchMemberGuildPermissionsDiscord(guildId, userId, opts);
329
+ if (permissions === null) return false;
330
+ if (hasAdministrator(permissions)) return true;
331
+ return check(permissions, requiredPermissions);
332
+ }
333
+ /**
334
+ * Returns true when the user has ADMINISTRATOR or any required permission bit.
335
+ */
336
+ async function hasAnyGuildPermissionDiscord(guildId, userId, requiredPermissions, opts) {
337
+ return await hasGuildPermissionsDiscord(guildId, userId, requiredPermissions, (permissions, required) => required.some((permission) => hasPermissionBit(permissions, permission)), opts);
338
+ }
339
+ /**
340
+ * Returns true when the user has ADMINISTRATOR or all required permission bits.
341
+ */
342
+ async function hasAllGuildPermissionsDiscord(guildId, userId, requiredPermissions, opts) {
343
+ return await hasGuildPermissionsDiscord(guildId, userId, requiredPermissions, (permissions, required) => required.every((permission) => hasPermissionBit(permissions, permission)), opts);
344
+ }
345
+ async function fetchChannelPermissionsDiscord(channelId, opts) {
346
+ const rest = resolveDiscordRest(opts);
347
+ const channel = await getChannel(rest, channelId);
348
+ const channelType = "type" in channel ? channel.type : void 0;
349
+ const guildId = "guild_id" in channel ? channel.guild_id : void 0;
350
+ if (!guildId) return {
351
+ channelId,
352
+ permissions: [],
353
+ raw: "0",
354
+ isDm: true,
355
+ channelType
356
+ };
357
+ const botId = await fetchBotUserId(rest);
358
+ const [guild, member] = await Promise.all([getGuild(rest, guildId), getGuildMember(rest, guildId, botId)]);
359
+ const permissions = resolveMemberChannelPermissionBits({
360
+ guildId,
361
+ userId: botId,
362
+ guild,
363
+ member,
364
+ channel
365
+ });
366
+ return {
367
+ channelId,
368
+ guildId,
369
+ permissions: bitfieldToPermissions(permissions),
370
+ raw: permissions.toString(),
371
+ isDm: false,
372
+ channelType
373
+ };
374
+ }
375
+ //#endregion
376
+ //#region extensions/discord/src/send.types.ts
377
+ var DiscordSendError = class extends Error {
378
+ constructor(message, opts) {
379
+ super(message);
380
+ this.name = "DiscordSendError";
381
+ if (opts) Object.assign(this, opts);
382
+ }
383
+ toString() {
384
+ return this.message;
385
+ }
386
+ };
387
+ const DISCORD_MAX_EMOJI_BYTES = 256 * 1024;
388
+ const DISCORD_MAX_STICKER_BYTES = 512 * 1024;
389
+ const DISCORD_MAX_EVENT_COVER_BYTES = 8 * 1024 * 1024;
390
+ //#endregion
391
+ //#region extensions/discord/src/send.message-request.ts
392
+ const SUPPRESS_EMBEDS_FLAG = MessageFlags.SuppressEmbeds;
393
+ const SUPPRESS_NOTIFICATIONS_FLAG = MessageFlags.SuppressNotifications;
394
+ function resolveDiscordSendComponents(params) {
395
+ if (!params.components || !params.isFirst) return;
396
+ return typeof params.components === "function" ? params.components(params.text) : params.components;
397
+ }
398
+ function normalizeDiscordEmbeds(embeds) {
399
+ if (!embeds?.length) return;
400
+ return embeds.map((embed) => embed instanceof Embed ? embed : new Embed(embed));
401
+ }
402
+ function resolveDiscordSendEmbeds(params) {
403
+ if (!params.embeds || !params.isFirst) return;
404
+ return normalizeDiscordEmbeds(params.embeds);
405
+ }
406
+ function buildDiscordMessagePayload(params) {
407
+ const payload = {};
408
+ const hasV2 = hasV2Components(params.components);
409
+ const trimmed = params.text.trim();
410
+ if (!hasV2 && trimmed) payload.content = params.text;
411
+ if (params.components?.length) payload.components = params.components;
412
+ if (!hasV2 && params.embeds?.length) payload.embeds = params.embeds;
413
+ if (params.flags !== void 0) payload.flags = params.flags;
414
+ if (params.files?.length) payload.files = params.files;
415
+ return payload;
416
+ }
417
+ function resolveDiscordMessageFlags(params) {
418
+ let flags = 0;
419
+ if (params.suppressEmbeds) flags |= SUPPRESS_EMBEDS_FLAG;
420
+ if (params.silent) flags |= SUPPRESS_NOTIFICATIONS_FLAG;
421
+ return flags || void 0;
422
+ }
423
+ function buildDiscordMessageRequest(params) {
424
+ return stripUndefinedFields({
425
+ ...serializePayload(buildDiscordMessagePayload(params)),
426
+ ...params.replyTo ? { message_reference: {
427
+ message_id: params.replyTo,
428
+ fail_if_not_exists: false
429
+ } } : {}
430
+ });
431
+ }
432
+ function stripUndefinedFields(value) {
433
+ return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== void 0));
434
+ }
435
+ function hasV2Components(components) {
436
+ return Boolean(components?.some((component) => "isV2" in component && component.isV2));
437
+ }
438
+ //#endregion
439
+ //#region extensions/discord/src/send.shared.ts
440
+ const DISCORD_TEXT_LIMIT = 2e3;
441
+ const DISCORD_MAX_STICKERS = 3;
442
+ const DISCORD_POLL_MAX_ANSWERS = 10;
443
+ const DISCORD_POLL_MAX_DURATION_HOURS = 768;
444
+ const DISCORD_MISSING_PERMISSIONS = 50013;
445
+ const DISCORD_CANNOT_DM = 50007;
446
+ function normalizeReactionEmoji(raw) {
447
+ const trimmed = raw.trim();
448
+ if (!trimmed) throw new Error("emoji required");
449
+ const customMatch = trimmed.match(/^<a?:([^:>]+):(\d+)>$/);
450
+ const identifier = customMatch ? `${customMatch[1]}:${customMatch[2]}` : trimmed.replace(/[\uFE0E\uFE0F]/g, "");
451
+ return encodeURIComponent(identifier);
452
+ }
453
+ function normalizeStickerIds(raw) {
454
+ const ids = raw.map((entry) => entry.trim()).filter(Boolean);
455
+ if (ids.length === 0) throw new Error("At least one sticker id is required");
456
+ if (ids.length > DISCORD_MAX_STICKERS) throw new Error("Discord supports up to 3 stickers per message");
457
+ return ids;
458
+ }
459
+ function normalizeEmojiName(raw, label) {
460
+ const name = raw.trim();
461
+ if (!name) throw new Error(`${label} is required`);
462
+ return name;
463
+ }
464
+ function normalizeDiscordPollInput(input) {
465
+ const poll = normalizePollInput(input, { maxOptions: DISCORD_POLL_MAX_ANSWERS });
466
+ const duration = normalizePollDurationHours(poll.durationHours, {
467
+ defaultHours: 24,
468
+ maxHours: DISCORD_POLL_MAX_DURATION_HOURS
469
+ });
470
+ return {
471
+ question: { text: poll.question },
472
+ answers: poll.options.map((answer) => ({ poll_media: { text: answer } })),
473
+ duration,
474
+ allow_multiselect: poll.maxSelections > 1,
475
+ layout_type: PollLayoutType.Default
476
+ };
477
+ }
478
+ function getDiscordErrorCode(err) {
479
+ if (!err || typeof err !== "object") return;
480
+ const candidate = "code" in err && err.code !== void 0 ? err.code : "rawError" in err && err.rawError && typeof err.rawError === "object" ? err.rawError.code : void 0;
481
+ if (typeof candidate === "number") return candidate;
482
+ if (typeof candidate === "string" && /^\d+$/.test(candidate)) return Number(candidate);
483
+ }
484
+ function getDiscordErrorStatus(err) {
485
+ if (!err || typeof err !== "object") return;
486
+ const candidate = "status" in err && err.status !== void 0 ? err.status : "statusCode" in err && err.statusCode !== void 0 ? err.statusCode : void 0;
487
+ if (typeof candidate === "number" && Number.isFinite(candidate)) return candidate;
488
+ if (typeof candidate === "string" && /^\d+$/.test(candidate)) return Number(candidate);
489
+ }
490
+ async function buildDiscordSendError(err, ctx) {
491
+ if (err instanceof DiscordSendError) return err;
492
+ const code = getDiscordErrorCode(err);
493
+ if (code === DISCORD_CANNOT_DM) return new DiscordSendError(`discord dm failed: user blocks dms or privacy settings disallow it (code=${code})`, {
494
+ kind: "dm-blocked",
495
+ discordCode: code,
496
+ status: getDiscordErrorStatus(err)
497
+ });
498
+ if (code !== DISCORD_MISSING_PERMISSIONS) return err;
499
+ let missing = [];
500
+ let probedChannelType;
501
+ try {
502
+ const permissions = await fetchChannelPermissionsDiscord(ctx.channelId, {
503
+ rest: ctx.rest,
504
+ token: ctx.token,
505
+ cfg: ctx.cfg
506
+ });
507
+ probedChannelType = permissions.channelType;
508
+ const current = new Set(permissions.permissions);
509
+ const required = ["ViewChannel", "SendMessages"];
510
+ if (isThreadChannelType(probedChannelType)) required.push("SendMessagesInThreads");
511
+ if (ctx.hasMedia) required.push("AttachFiles");
512
+ missing = required.filter((permission) => !current.has(permission));
513
+ } catch {}
514
+ const status = getDiscordErrorStatus(err);
515
+ const apiDetails = [`code=${code}`, status != null ? `status=${status}` : void 0].filter(Boolean).join(" ");
516
+ const probedPermissions = ["ViewChannel", "SendMessages"];
517
+ if (isThreadChannelType(probedChannelType)) probedPermissions.push("SendMessagesInThreads");
518
+ if (ctx.hasMedia) probedPermissions.push("AttachFiles");
519
+ const probeSummary = probedPermissions.join("/");
520
+ return new DiscordSendError(`${missing.length ? `discord missing permissions in channel ${ctx.channelId}: ${missing.join(", ")}` : `discord missing permissions in channel ${ctx.channelId}; permission probe did not identify missing ${probeSummary}`} (${apiDetails}). bot might be blocked by channel/thread overrides, archived thread state, reply target visibility, or app-role position`, {
521
+ kind: "missing-permissions",
522
+ channelId: ctx.channelId,
523
+ missingPermissions: missing,
524
+ discordCode: code,
525
+ status
526
+ });
527
+ }
528
+ async function resolveChannelId(rest, recipient, request) {
529
+ if (recipient.kind === "channel") return { channelId: recipient.id };
530
+ const dmChannel = await request(() => createUserDmChannel(rest, recipient.id), "dm-channel");
531
+ if (!dmChannel?.id) throw new Error("Failed to create Discord DM channel");
532
+ return {
533
+ channelId: dmChannel.id,
534
+ dm: true
535
+ };
536
+ }
537
+ async function resolveDiscordTargetChannelId(raw, opts) {
538
+ const recipient = await parseAndResolveRecipient(raw, requireRuntimeConfig(opts.cfg, "Discord target channel resolution"), opts.accountId, { defaultKind: "channel" });
539
+ const { rest, request } = createDiscordClient(opts);
540
+ return await resolveChannelId(rest, recipient, request);
541
+ }
542
+ async function resolveDiscordChannelType(rest, channelId) {
543
+ try {
544
+ return (await getChannel(rest, channelId))?.type;
545
+ } catch {
546
+ return;
547
+ }
548
+ }
549
+ function buildDiscordTextChunks(text, opts = {}) {
550
+ if (!text) return [];
551
+ return resolveTextChunksWithFallback(text, chunkDiscordTextWithMode(text, {
552
+ maxChars: opts.maxChars ?? DISCORD_TEXT_LIMIT,
553
+ maxLines: opts.maxLinesPerMessage,
554
+ chunkMode: opts.chunkMode
555
+ }));
556
+ }
557
+ function toDiscordFileBlob(data) {
558
+ if (data instanceof Blob) return data;
559
+ const arrayBuffer = new ArrayBuffer(data.byteLength);
560
+ new Uint8Array(arrayBuffer).set(data);
561
+ return new Blob([arrayBuffer]);
562
+ }
563
+ async function sendDiscordText(rest, channelId, text, replyTo, request, maxLinesPerMessage, components, embeds, chunkMode, silent, suppressEmbeds, maxChars) {
564
+ if (!text.trim()) throw new Error("Message must be non-empty for Discord sends");
565
+ const chunks = buildDiscordTextChunks(text, {
566
+ maxLinesPerMessage,
567
+ chunkMode,
568
+ maxChars
569
+ });
570
+ const sendChunk = async (chunk, isFirst) => {
571
+ const chunkComponents = resolveDiscordSendComponents({
572
+ components,
573
+ text: chunk,
574
+ isFirst
575
+ });
576
+ const chunkEmbeds = resolveDiscordSendEmbeds({
577
+ embeds,
578
+ isFirst
579
+ });
580
+ const body = buildDiscordMessageRequest({
581
+ text: chunk,
582
+ components: chunkComponents,
583
+ embeds: chunkEmbeds,
584
+ flags: resolveDiscordMessageFlags({
585
+ silent,
586
+ suppressEmbeds: suppressEmbeds && !chunkEmbeds?.length
587
+ }),
588
+ replyTo
589
+ });
590
+ return await request(() => createChannelMessage(rest, channelId, { body }), "text");
591
+ };
592
+ if (chunks.length === 1) {
593
+ const result = await sendChunk(chunks[0], true);
594
+ return {
595
+ ...result,
596
+ platformMessageIds: result.id ? [result.id] : []
597
+ };
598
+ }
599
+ const platformMessageIds = [];
600
+ let last = null;
601
+ for (const [index, chunk] of chunks.entries()) {
602
+ last = await sendChunk(chunk, index === 0);
603
+ if (last.id) platformMessageIds.push(last.id);
604
+ }
605
+ if (!last) throw new Error("Discord send failed (empty chunk result)");
606
+ return {
607
+ ...last,
608
+ platformMessageIds
609
+ };
610
+ }
611
+ async function sendDiscordMedia(rest, channelId, text, mediaUrl, filename, mediaAccess, mediaLocalRoots, mediaReadFile, maxBytes, replyTo, request, maxLinesPerMessage, components, embeds, chunkMode, silent, suppressEmbeds, maxChars) {
612
+ const media = await loadWebMedia(mediaUrl, buildOutboundMediaLoadOptions({
613
+ maxBytes,
614
+ mediaAccess,
615
+ mediaLocalRoots,
616
+ mediaReadFile
617
+ }));
618
+ const resolvedFileName = filename?.trim() || media.fileName || (media.contentType ? `upload${extensionForMime(media.contentType) ?? ""}` : "") || "upload";
619
+ const chunks = text ? buildDiscordTextChunks(text, {
620
+ maxLinesPerMessage,
621
+ chunkMode,
622
+ maxChars
623
+ }) : [];
624
+ const caption = chunks[0] ?? "";
625
+ const fileData = toDiscordFileBlob(media.buffer);
626
+ const captionComponents = resolveDiscordSendComponents({
627
+ components,
628
+ text: caption,
629
+ isFirst: true
630
+ });
631
+ const captionEmbeds = resolveDiscordSendEmbeds({
632
+ embeds,
633
+ isFirst: true
634
+ });
635
+ const body = buildDiscordMessageRequest({
636
+ text: caption,
637
+ components: captionComponents,
638
+ embeds: captionEmbeds,
639
+ flags: resolveDiscordMessageFlags({
640
+ silent,
641
+ suppressEmbeds: suppressEmbeds && !captionEmbeds?.length
642
+ }),
643
+ replyTo,
644
+ files: [{
645
+ data: fileData,
646
+ name: resolvedFileName
647
+ }]
648
+ });
649
+ const res = await request(() => createChannelMessage(rest, channelId, { body }), "media");
650
+ const platformMessageIds = res.id ? [res.id] : [];
651
+ for (const chunk of chunks.slice(1)) {
652
+ if (!chunk.trim()) continue;
653
+ const followup = await sendDiscordText(rest, channelId, chunk, replyTo, request, maxLinesPerMessage, void 0, void 0, chunkMode, silent, suppressEmbeds, maxChars);
654
+ for (const id of followup.platformMessageIds) if (id) platformMessageIds.push(id);
655
+ }
656
+ return {
657
+ ...res,
658
+ platformMessageIds
659
+ };
660
+ }
661
+ function buildReactionIdentifier(emoji) {
662
+ if (emoji.id && emoji.name) return `${emoji.name}:${emoji.id}`;
663
+ return emoji.name ?? "";
664
+ }
665
+ function formatReactionEmoji(emoji) {
666
+ return buildReactionIdentifier(emoji);
667
+ }
668
+ //#endregion
669
+ export { isThreadChannelType as A, DISCORD_MAX_STICKER_BYTES as C, fetchMemberGuildPermissionsDiscord as D, fetchChannelPermissionsDiscord as E, resolveDiscordClientAccountContext as F, resolveDiscordRest as I, DISCORD_REST_TIMEOUT_MS as L, createDiscordClient as M, createDiscordRestClient as N, hasAllGuildPermissionsDiscord as O, createDiscordRuntimeAccountContext as P, validateDiscordProxyUrl as R, DISCORD_MAX_EVENT_COVER_BYTES as S, canViewDiscordGuildChannel as T, resolveDiscordMessageFlags as _, normalizeDiscordPollInput as a, stripUndefinedFields as b, normalizeStickerIds as c, resolveDiscordTargetChannelId as d, sendDiscordMedia as f, buildDiscordMessageRequest as g, SUPPRESS_NOTIFICATIONS_FLAG as h, formatReactionEmoji as i, parseAndResolveRecipient as j, hasAnyGuildPermissionDiscord as k, resolveChannelId as l, toDiscordFileBlob as m, buildDiscordTextChunks as n, normalizeEmojiName as o, sendDiscordText as p, buildReactionIdentifier as r, normalizeReactionEmoji as s, buildDiscordSendError as t, resolveDiscordChannelType as u, resolveDiscordSendComponents as v, DiscordSendError as w, DISCORD_MAX_EMOJI_BYTES as x, resolveDiscordSendEmbeds as y, withValidatedDiscordProxy as z };