mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 21:01:43 +03:00
refactor(discord): dedupe guild listing
This commit is contained in:
@@ -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<DiscordGuildSummary[]> {
|
||||||
|
const raw = await fetchDiscord<Array<{ id?: string; name?: string }>>(
|
||||||
|
"/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),
|
||||||
|
}));
|
||||||
|
}
|
||||||
@@ -1,13 +1,8 @@
|
|||||||
import { fetchDiscord } from "./api.js";
|
import { fetchDiscord } from "./api.js";
|
||||||
|
import { listGuilds, type DiscordGuildSummary } from "./guilds.js";
|
||||||
import { normalizeDiscordSlug } from "./monitor/allow-list.js";
|
import { normalizeDiscordSlug } from "./monitor/allow-list.js";
|
||||||
import { normalizeDiscordToken } from "./token.js";
|
import { normalizeDiscordToken } from "./token.js";
|
||||||
|
|
||||||
type DiscordGuildSummary = {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
slug: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type DiscordChannelSummary = {
|
type DiscordChannelSummary = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -73,24 +68,6 @@ function parseDiscordChannelInput(raw: string): {
|
|||||||
return { guild: trimmed, guildOnly: true };
|
return { guild: trimmed, guildOnly: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function listGuilds(token: string, fetcher: typeof fetch): Promise<DiscordGuildSummary[]> {
|
|
||||||
const raw = await fetchDiscord<Array<{ id?: string; name?: string }>>(
|
|
||||||
"/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(
|
async function listGuildChannels(
|
||||||
token: string,
|
token: string,
|
||||||
fetcher: typeof fetch,
|
fetcher: typeof fetch,
|
||||||
|
|||||||
@@ -1,13 +1,8 @@
|
|||||||
import { fetchDiscord } from "./api.js";
|
import { fetchDiscord } from "./api.js";
|
||||||
|
import { listGuilds, type DiscordGuildSummary } from "./guilds.js";
|
||||||
import { normalizeDiscordSlug } from "./monitor/allow-list.js";
|
import { normalizeDiscordSlug } from "./monitor/allow-list.js";
|
||||||
import { normalizeDiscordToken } from "./token.js";
|
import { normalizeDiscordToken } from "./token.js";
|
||||||
|
|
||||||
type DiscordGuildSummary = {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
slug: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type DiscordUser = {
|
type DiscordUser = {
|
||||||
id: string;
|
id: string;
|
||||||
username: string;
|
username: string;
|
||||||
@@ -61,24 +56,6 @@ function parseDiscordUserInput(raw: string): {
|
|||||||
return { userName: trimmed.replace(/^@/, "") };
|
return { userName: trimmed.replace(/^@/, "") };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function listGuilds(token: string, fetcher: typeof fetch): Promise<DiscordGuildSummary[]> {
|
|
||||||
const raw = await fetchDiscord<Array<{ id?: string; name?: string }>>(
|
|
||||||
"/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 {
|
function scoreDiscordMember(member: DiscordMember, query: string): number {
|
||||||
const q = query.toLowerCase();
|
const q = query.toLowerCase();
|
||||||
const user = member.user;
|
const user = member.user;
|
||||||
|
|||||||
Reference in New Issue
Block a user