mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 06:27:39 +00:00
perf: reduce hotspot test startup and timeout costs
This commit is contained in:
63
src/discord/monitor/gateway-plugin.ts
Normal file
63
src/discord/monitor/gateway-plugin.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { GatewayIntents, GatewayPlugin } from "@buape/carbon/gateway";
|
||||
import { HttpsProxyAgent } from "https-proxy-agent";
|
||||
import WebSocket from "ws";
|
||||
import type { DiscordAccountConfig } from "../../config/types.js";
|
||||
import type { RuntimeEnv } from "../../runtime.js";
|
||||
import { danger } from "../../globals.js";
|
||||
|
||||
export function resolveDiscordGatewayIntents(
|
||||
intentsConfig?: import("../../config/types.discord.js").DiscordIntentsConfig,
|
||||
): number {
|
||||
let intents =
|
||||
GatewayIntents.Guilds |
|
||||
GatewayIntents.GuildMessages |
|
||||
GatewayIntents.MessageContent |
|
||||
GatewayIntents.DirectMessages |
|
||||
GatewayIntents.GuildMessageReactions |
|
||||
GatewayIntents.DirectMessageReactions;
|
||||
if (intentsConfig?.presence) {
|
||||
intents |= GatewayIntents.GuildPresences;
|
||||
}
|
||||
if (intentsConfig?.guildMembers) {
|
||||
intents |= GatewayIntents.GuildMembers;
|
||||
}
|
||||
return intents;
|
||||
}
|
||||
|
||||
export function createDiscordGatewayPlugin(params: {
|
||||
discordConfig: DiscordAccountConfig;
|
||||
runtime: RuntimeEnv;
|
||||
}): GatewayPlugin {
|
||||
const intents = resolveDiscordGatewayIntents(params.discordConfig?.intents);
|
||||
const proxy = params.discordConfig?.proxy?.trim();
|
||||
const options = {
|
||||
reconnect: { maxAttempts: 50 },
|
||||
intents,
|
||||
autoInteractions: true,
|
||||
};
|
||||
|
||||
if (!proxy) {
|
||||
return new GatewayPlugin(options);
|
||||
}
|
||||
|
||||
try {
|
||||
const agent = new HttpsProxyAgent<string>(proxy);
|
||||
|
||||
params.runtime.log?.("discord: gateway proxy enabled");
|
||||
|
||||
class ProxyGatewayPlugin extends GatewayPlugin {
|
||||
constructor() {
|
||||
super(options);
|
||||
}
|
||||
|
||||
createWebSocket(url: string) {
|
||||
return new WebSocket(url, { agent });
|
||||
}
|
||||
}
|
||||
|
||||
return new ProxyGatewayPlugin();
|
||||
} catch (err) {
|
||||
params.runtime.error?.(danger(`discord: invalid gateway proxy: ${String(err)}`));
|
||||
return new GatewayPlugin(options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user