mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 19:18:26 +00:00
refactor(discord): centralize target parsing
Co-authored-by: Jonathan Rhyne <jonathan@pspdfkit.com>
This commit is contained in:
@@ -12,6 +12,7 @@ import { resolveDiscordAccount } from "./accounts.js";
|
||||
import { chunkDiscordText } from "./chunk.js";
|
||||
import { fetchChannelPermissionsDiscord, isThreadChannelType } from "./send.permissions.js";
|
||||
import { DiscordSendError } from "./send.types.js";
|
||||
import { parseDiscordTarget } from "./targets.js";
|
||||
import { normalizeDiscordToken } from "./token.js";
|
||||
|
||||
const DISCORD_TEXT_LIMIT = 2000;
|
||||
@@ -90,36 +91,13 @@ function normalizeReactionEmoji(raw: string) {
|
||||
}
|
||||
|
||||
function parseRecipient(raw: string): DiscordRecipient {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) {
|
||||
const target = parseDiscordTarget(raw, {
|
||||
ambiguousMessage: `Ambiguous Discord recipient "${raw.trim()}". Use "user:${raw.trim()}" for DMs or "channel:${raw.trim()}" for channel messages.`,
|
||||
});
|
||||
if (!target) {
|
||||
throw new Error("Recipient is required for Discord sends");
|
||||
}
|
||||
const mentionMatch = trimmed.match(/^<@!?(\d+)>$/);
|
||||
if (mentionMatch) {
|
||||
return { kind: "user", id: mentionMatch[1] };
|
||||
}
|
||||
if (trimmed.startsWith("user:")) {
|
||||
return { kind: "user", id: trimmed.slice("user:".length) };
|
||||
}
|
||||
if (trimmed.startsWith("channel:")) {
|
||||
return { kind: "channel", id: trimmed.slice("channel:".length) };
|
||||
}
|
||||
if (trimmed.startsWith("discord:")) {
|
||||
return { kind: "user", id: trimmed.slice("discord:".length) };
|
||||
}
|
||||
if (trimmed.startsWith("@")) {
|
||||
const candidate = trimmed.slice(1);
|
||||
if (!/^\d+$/.test(candidate)) {
|
||||
throw new Error("Discord DMs require a user id (use user:<id> or a <@id> mention)");
|
||||
}
|
||||
return { kind: "user", id: candidate };
|
||||
}
|
||||
if (/^\d+$/.test(trimmed)) {
|
||||
throw new Error(
|
||||
`Ambiguous Discord recipient "${trimmed}". Use "user:${trimmed}" for DMs or "channel:${trimmed}" for channel messages.`,
|
||||
);
|
||||
}
|
||||
return { kind: "channel", id: trimmed };
|
||||
return { kind: target.kind, id: target.id };
|
||||
}
|
||||
|
||||
function normalizeStickerIds(raw: string[]) {
|
||||
|
||||
75
src/discord/targets.test.ts
Normal file
75
src/discord/targets.test.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { normalizeDiscordMessagingTarget } from "../channels/plugins/normalize-target.js";
|
||||
import { parseDiscordTarget, resolveDiscordChannelId } from "./targets.js";
|
||||
|
||||
describe("parseDiscordTarget", () => {
|
||||
it("parses user mention and prefixes", () => {
|
||||
expect(parseDiscordTarget("<@123>")).toMatchObject({
|
||||
kind: "user",
|
||||
id: "123",
|
||||
normalized: "user:123",
|
||||
});
|
||||
expect(parseDiscordTarget("<@!456>")).toMatchObject({
|
||||
kind: "user",
|
||||
id: "456",
|
||||
normalized: "user:456",
|
||||
});
|
||||
expect(parseDiscordTarget("user:789")).toMatchObject({
|
||||
kind: "user",
|
||||
id: "789",
|
||||
normalized: "user:789",
|
||||
});
|
||||
expect(parseDiscordTarget("discord:987")).toMatchObject({
|
||||
kind: "user",
|
||||
id: "987",
|
||||
normalized: "user:987",
|
||||
});
|
||||
});
|
||||
|
||||
it("parses channel targets", () => {
|
||||
expect(parseDiscordTarget("channel:555")).toMatchObject({
|
||||
kind: "channel",
|
||||
id: "555",
|
||||
normalized: "channel:555",
|
||||
});
|
||||
expect(parseDiscordTarget("general")).toMatchObject({
|
||||
kind: "channel",
|
||||
id: "general",
|
||||
normalized: "channel:general",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects ambiguous numeric ids without a default kind", () => {
|
||||
expect(() => parseDiscordTarget("123")).toThrow(/Ambiguous Discord recipient/);
|
||||
});
|
||||
|
||||
it("accepts numeric ids when a default kind is provided", () => {
|
||||
expect(parseDiscordTarget("123", { defaultKind: "channel" })).toMatchObject({
|
||||
kind: "channel",
|
||||
id: "123",
|
||||
normalized: "channel:123",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects non-numeric @ mentions", () => {
|
||||
expect(() => parseDiscordTarget("@bob")).toThrow(/Discord DMs require a user id/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveDiscordChannelId", () => {
|
||||
it("strips channel: prefix and accepts raw ids", () => {
|
||||
expect(resolveDiscordChannelId("channel:123")).toBe("123");
|
||||
expect(resolveDiscordChannelId("123")).toBe("123");
|
||||
});
|
||||
|
||||
it("rejects user targets", () => {
|
||||
expect(() => resolveDiscordChannelId("user:123")).toThrow(/channel id is required/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeDiscordMessagingTarget", () => {
|
||||
it("defaults raw numeric ids to channels", () => {
|
||||
expect(normalizeDiscordMessagingTarget("123")).toBe("channel:123");
|
||||
});
|
||||
});
|
||||
78
src/discord/targets.ts
Normal file
78
src/discord/targets.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
export type DiscordTargetKind = "user" | "channel";
|
||||
|
||||
export type DiscordTarget = {
|
||||
kind: DiscordTargetKind;
|
||||
id: string;
|
||||
raw: string;
|
||||
normalized: string;
|
||||
};
|
||||
|
||||
type DiscordTargetParseOptions = {
|
||||
defaultKind?: DiscordTargetKind;
|
||||
ambiguousMessage?: string;
|
||||
};
|
||||
|
||||
function normalizeTargetId(kind: DiscordTargetKind, id: string) {
|
||||
return `${kind}:${id}`.toLowerCase();
|
||||
}
|
||||
|
||||
function buildTarget(kind: DiscordTargetKind, id: string, raw: string): DiscordTarget {
|
||||
return {
|
||||
kind,
|
||||
id,
|
||||
raw,
|
||||
normalized: normalizeTargetId(kind, id),
|
||||
};
|
||||
}
|
||||
|
||||
export function parseDiscordTarget(
|
||||
raw: string,
|
||||
options: DiscordTargetParseOptions = {},
|
||||
): DiscordTarget | undefined {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return undefined;
|
||||
const mentionMatch = trimmed.match(/^<@!?(\d+)>$/);
|
||||
if (mentionMatch) {
|
||||
return buildTarget("user", mentionMatch[1], trimmed);
|
||||
}
|
||||
if (trimmed.startsWith("user:")) {
|
||||
const id = trimmed.slice("user:".length).trim();
|
||||
return id ? buildTarget("user", id, trimmed) : undefined;
|
||||
}
|
||||
if (trimmed.startsWith("channel:")) {
|
||||
const id = trimmed.slice("channel:".length).trim();
|
||||
return id ? buildTarget("channel", id, trimmed) : undefined;
|
||||
}
|
||||
if (trimmed.startsWith("discord:")) {
|
||||
const id = trimmed.slice("discord:".length).trim();
|
||||
return id ? buildTarget("user", id, trimmed) : undefined;
|
||||
}
|
||||
if (trimmed.startsWith("@")) {
|
||||
const candidate = trimmed.slice(1).trim();
|
||||
if (!/^\d+$/.test(candidate)) {
|
||||
throw new Error("Discord DMs require a user id (use user:<id> or a <@id> mention)");
|
||||
}
|
||||
return buildTarget("user", candidate, trimmed);
|
||||
}
|
||||
if (/^\d+$/.test(trimmed)) {
|
||||
if (options.defaultKind) {
|
||||
return buildTarget(options.defaultKind, trimmed, trimmed);
|
||||
}
|
||||
throw new Error(
|
||||
options.ambiguousMessage ??
|
||||
`Ambiguous Discord recipient "${trimmed}". Use "user:${trimmed}" for DMs or "channel:${trimmed}" for channel messages.`,
|
||||
);
|
||||
}
|
||||
return buildTarget("channel", trimmed, trimmed);
|
||||
}
|
||||
|
||||
export function resolveDiscordChannelId(raw: string): string {
|
||||
const target = parseDiscordTarget(raw, { defaultKind: "channel" });
|
||||
if (!target) {
|
||||
throw new Error("Discord channel id is required.");
|
||||
}
|
||||
if (target.kind !== "channel") {
|
||||
throw new Error("Discord channel id is required (use channel:<id>).");
|
||||
}
|
||||
return target.id;
|
||||
}
|
||||
Reference in New Issue
Block a user