mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 22:54:33 +00:00
refactor(discord): share client rest helpers
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import type { RequestClient } from "@buape/carbon";
|
||||
import type { APIChannel, APIGuild, APIGuildMember, APIRole } from "discord-api-types/v10";
|
||||
import { RequestClient } from "@buape/carbon";
|
||||
import { ChannelType, PermissionFlagsBits, Routes } from "discord-api-types/v10";
|
||||
import type { RetryConfig } from "../infra/retry.js";
|
||||
import type { DiscordPermissionsSummary, DiscordReactOpts } from "./send.types.js";
|
||||
import { loadConfig } from "../config/config.js";
|
||||
import { resolveDiscordAccount } from "./accounts.js";
|
||||
import { normalizeDiscordToken } from "./token.js";
|
||||
import { resolveDiscordRest } from "./client.js";
|
||||
|
||||
const PERMISSION_ENTRIES = Object.entries(PermissionFlagsBits).filter(
|
||||
([, value]) => typeof value === "bigint",
|
||||
@@ -13,43 +10,6 @@ const PERMISSION_ENTRIES = Object.entries(PermissionFlagsBits).filter(
|
||||
const ALL_PERMISSIONS = PERMISSION_ENTRIES.reduce((acc, [, value]) => acc | value, 0n);
|
||||
const ADMINISTRATOR_BIT = PermissionFlagsBits.Administrator;
|
||||
|
||||
type DiscordClientOpts = {
|
||||
token?: string;
|
||||
accountId?: string;
|
||||
rest?: RequestClient;
|
||||
retry?: RetryConfig;
|
||||
verbose?: boolean;
|
||||
};
|
||||
|
||||
function resolveToken(params: { explicit?: string; accountId: string; fallbackToken?: string }) {
|
||||
const explicit = normalizeDiscordToken(params.explicit);
|
||||
if (explicit) {
|
||||
return explicit;
|
||||
}
|
||||
const fallback = normalizeDiscordToken(params.fallbackToken);
|
||||
if (!fallback) {
|
||||
throw new Error(
|
||||
`Discord bot token missing for account "${params.accountId}" (set discord.accounts.${params.accountId}.token or DISCORD_BOT_TOKEN for default).`,
|
||||
);
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function resolveRest(token: string, rest?: RequestClient) {
|
||||
return rest ?? new RequestClient(token);
|
||||
}
|
||||
|
||||
function resolveDiscordRest(opts: DiscordClientOpts) {
|
||||
const cfg = loadConfig();
|
||||
const account = resolveDiscordAccount({ cfg, accountId: opts.accountId });
|
||||
const token = resolveToken({
|
||||
explicit: opts.token,
|
||||
accountId: account.accountId,
|
||||
fallbackToken: account.token,
|
||||
});
|
||||
return resolveRest(token, opts.rest);
|
||||
}
|
||||
|
||||
function addPermissionBits(base: bigint, add?: string) {
|
||||
if (!add) {
|
||||
return base;
|
||||
|
||||
Reference in New Issue
Block a user