mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:14:33 +00:00
refactor(discord): dedupe directory and media send paths
This commit is contained in:
@@ -9,6 +9,7 @@ type DiscordGuild = { id: string; name: string };
|
||||
type DiscordUser = { id: string; username: string; global_name?: string; bot?: boolean };
|
||||
type DiscordMember = { user: DiscordUser; nick?: string | null };
|
||||
type DiscordChannel = { id: string; name?: string | null };
|
||||
type DiscordDirectoryAccess = { token: string; query: string };
|
||||
|
||||
function normalizeQuery(value?: string | null): string {
|
||||
return value?.trim().toLowerCase() ?? "";
|
||||
@@ -18,17 +19,31 @@ function buildUserRank(user: DiscordUser): number {
|
||||
return user.bot ? 0 : 1;
|
||||
}
|
||||
|
||||
export async function listDiscordDirectoryGroupsLive(
|
||||
function resolveDiscordDirectoryAccess(
|
||||
params: DirectoryConfigParams,
|
||||
): Promise<ChannelDirectoryEntry[]> {
|
||||
): DiscordDirectoryAccess | null {
|
||||
const account = resolveDiscordAccount({ cfg: params.cfg, accountId: params.accountId });
|
||||
const token = normalizeDiscordToken(account.token);
|
||||
if (!token) {
|
||||
return null;
|
||||
}
|
||||
return { token, query: normalizeQuery(params.query) };
|
||||
}
|
||||
|
||||
async function listDiscordGuilds(token: string): Promise<DiscordGuild[]> {
|
||||
const rawGuilds = await fetchDiscord<DiscordGuild[]>("/users/@me/guilds", token);
|
||||
return rawGuilds.filter((guild) => guild.id && guild.name);
|
||||
}
|
||||
|
||||
export async function listDiscordDirectoryGroupsLive(
|
||||
params: DirectoryConfigParams,
|
||||
): Promise<ChannelDirectoryEntry[]> {
|
||||
const access = resolveDiscordDirectoryAccess(params);
|
||||
if (!access) {
|
||||
return [];
|
||||
}
|
||||
const query = normalizeQuery(params.query);
|
||||
const rawGuilds = await fetchDiscord<DiscordGuild[]>("/users/@me/guilds", token);
|
||||
const guilds = rawGuilds.filter((g) => g.id && g.name);
|
||||
const { token, query } = access;
|
||||
const guilds = await listDiscordGuilds(token);
|
||||
const rows: ChannelDirectoryEntry[] = [];
|
||||
|
||||
for (const guild of guilds) {
|
||||
@@ -60,18 +75,16 @@ export async function listDiscordDirectoryGroupsLive(
|
||||
export async function listDiscordDirectoryPeersLive(
|
||||
params: DirectoryConfigParams,
|
||||
): Promise<ChannelDirectoryEntry[]> {
|
||||
const account = resolveDiscordAccount({ cfg: params.cfg, accountId: params.accountId });
|
||||
const token = normalizeDiscordToken(account.token);
|
||||
if (!token) {
|
||||
const access = resolveDiscordDirectoryAccess(params);
|
||||
if (!access) {
|
||||
return [];
|
||||
}
|
||||
const query = normalizeQuery(params.query);
|
||||
const { token, query } = access;
|
||||
if (!query) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const rawGuilds = await fetchDiscord<DiscordGuild[]>("/users/@me/guilds", token);
|
||||
const guilds = rawGuilds.filter((g) => g.id && g.name);
|
||||
const guilds = await listDiscordGuilds(token);
|
||||
const rows: ChannelDirectoryEntry[] = [];
|
||||
const limit = typeof params.limit === "number" && params.limit > 0 ? params.limit : 25;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user