import {z} from 'zod' export type GetApiServersGuildIdTroubleshootArgs = {guild_id: string, live?: '0' | undefined, bot?: 'pro' | undefined}; export type GetApiServersGuildIdTroubleshootOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fix = z.object({kind: z.enum(["initialise_guild", "register_webhook", "clear_log_channel", "enable_log_type", "remove_ignore", "disable_bot_ignore"]), automatic: z.boolean().optional(), category: z.enum(["mainChannel", "modlogCategory", "membersCategory", "actionsCategory", "textCategory", "voiceCategory", "filesCategory", "serverCategory", "rolesCategory", "channelsCategory", "quarkCategory"]).optional(), channelId: z.string().optional(), logType: z.number().int().optional(), ignoreType: z.number().int().optional(), value: z.string().optional(), scope: z.enum(["executors", "targets"]).optional()}); export const finding = z.object({id: z.string().optional(), severity: z.enum(["blocking", "degraded", "info"]).optional(), title: z.string().optional(), detail: z.string().optional(), scope: z.object({}).optional(), fix: z.union([fix, z.object({})]).nullable().optional()}); export const diagnosticReport = z.object({guildId: z.string().optional(), botId: z.string().optional(), generatedAt: z.number().int().optional(), status: z.enum(["broken", "degraded", "healthy"]).optional(), findings: z.array(finding).optional(), counts: z.object({blocking: z.number().int().optional(), degraded: z.number().int().optional(), info: z.number().int().optional()}).optional(), summary: z.object({configuredCategories: z.array(z.enum(["mainChannel", "modlogCategory", "membersCategory", "actionsCategory", "textCategory", "voiceCategory", "filesCategory", "serverCategory", "rolesCategory", "channelsCategory", "quarkCategory"])).optional(), firstLogAt: z.number().int().nullable().optional(), premiumLevel: z.number().int().optional(), incomplete: z.array(z.string()).optional()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Run the troubleshooter * * GET /servers/{guild_id}/troubleshoot */ export const getApiServersGuildIdTroubleshoot = async (args: GetApiServersGuildIdTroubleshootArgs, options?: GetApiServersGuildIdTroubleshootOptions) => { const {baseUrl = "https://quark.bot/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.live != null) query.append("live", String(args.live)) if (args.bot != null) query.append("bot", String(args.bot)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/servers/${encodeURIComponent(String(args.guild_id))}/troubleshoot${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(diagnosticReport, await res.json()) };