diff --git a/src/discord/guilds.ts b/src/discord/guilds.ts new file mode 100644 index 00000000000..bfe106ed00a --- /dev/null +++ b/src/discord/guilds.ts @@ -0,0 +1,29 @@ +import { fetchDiscord } from "./api.js"; +import { normalizeDiscordSlug } from "./monitor/allow-list.js"; + +export type DiscordGuildSummary = { + id: string; + name: string; + slug: string; +}; + +export async function listGuilds( + token: string, + fetcher: typeof fetch, +): Promise { + const raw = await fetchDiscord>( + "/users/@me/guilds", + token, + fetcher, + ); + return raw + .filter( + (guild): guild is { id: string; name: string } => + typeof guild.id === "string" && typeof guild.name === "string", + ) + .map((guild) => ({ + id: guild.id, + name: guild.name, + slug: normalizeDiscordSlug(guild.name), + })); +} diff --git a/src/discord/resolve-channels.ts b/src/discord/resolve-channels.ts index e9778a1bb09..857b2e47533 100644 --- a/src/discord/resolve-channels.ts +++ b/src/discord/resolve-channels.ts @@ -1,13 +1,8 @@ import { fetchDiscord } from "./api.js"; +import { listGuilds, type DiscordGuildSummary } from "./guilds.js"; import { normalizeDiscordSlug } from "./monitor/allow-list.js"; import { normalizeDiscordToken } from "./token.js"; -type DiscordGuildSummary = { - id: string; - name: string; - slug: string; -}; - type DiscordChannelSummary = { id: string; name: string; @@ -73,24 +68,6 @@ function parseDiscordChannelInput(raw: string): { return { guild: trimmed, guildOnly: true }; } -async function listGuilds(token: string, fetcher: typeof fetch): Promise { - const raw = await fetchDiscord>( - "/users/@me/guilds", - token, - fetcher, - ); - return raw - .filter( - (guild): guild is { id: string; name: string } => - typeof guild.id === "string" && typeof guild.name === "string", - ) - .map((guild) => ({ - id: guild.id, - name: guild.name, - slug: normalizeDiscordSlug(guild.name), - })); -} - async function listGuildChannels( token: string, fetcher: typeof fetch, diff --git a/src/discord/resolve-users.ts b/src/discord/resolve-users.ts index e9feb8d44d7..68c9e1f2034 100644 --- a/src/discord/resolve-users.ts +++ b/src/discord/resolve-users.ts @@ -1,13 +1,8 @@ import { fetchDiscord } from "./api.js"; +import { listGuilds, type DiscordGuildSummary } from "./guilds.js"; import { normalizeDiscordSlug } from "./monitor/allow-list.js"; import { normalizeDiscordToken } from "./token.js"; -type DiscordGuildSummary = { - id: string; - name: string; - slug: string; -}; - type DiscordUser = { id: string; username: string; @@ -61,24 +56,6 @@ function parseDiscordUserInput(raw: string): { return { userName: trimmed.replace(/^@/, "") }; } -async function listGuilds(token: string, fetcher: typeof fetch): Promise { - const raw = await fetchDiscord>( - "/users/@me/guilds", - token, - fetcher, - ); - return raw - .filter( - (guild): guild is { id: string; name: string } => - typeof guild.id === "string" && typeof guild.name === "string", - ) - .map((guild) => ({ - id: guild.id, - name: guild.name, - slug: normalizeDiscordSlug(guild.name), - })); -} - function scoreDiscordMember(member: DiscordMember, query: string): number { const q = query.toLowerCase(); const user = member.user;