feat(tool): register optional discord_control tool in whispergate plugin and align defaults
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
||||
import { evaluateDecision, type Decision, type WhisperGateConfig } from "./rules.js";
|
||||
|
||||
type DiscordControlAction = "channel-private-create" | "channel-private-update" | "member-list";
|
||||
|
||||
type DecisionRecord = {
|
||||
decision: Decision;
|
||||
createdAt: number;
|
||||
@@ -53,11 +55,103 @@ function shouldInjectEndMarker(reason: string): boolean {
|
||||
return reason === "bypass_sender" || reason.startsWith("end_symbol:");
|
||||
}
|
||||
|
||||
function pickDefined(input: Record<string, unknown>) {
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [k, v] of Object.entries(input)) {
|
||||
if (v !== undefined) out[k] = v;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export default {
|
||||
id: "whispergate",
|
||||
name: "WhisperGate",
|
||||
register(api: OpenClawPluginApi) {
|
||||
const config = (api.pluginConfig || {}) as WhisperGateConfig;
|
||||
const config = (api.pluginConfig || {}) as WhisperGateConfig & {
|
||||
enableDiscordControlTool?: boolean;
|
||||
discordControlApiBaseUrl?: string;
|
||||
discordControlApiToken?: string;
|
||||
discordControlCallerId?: string;
|
||||
};
|
||||
|
||||
if (config.enableDiscordControlTool !== false) {
|
||||
api.registerTool(
|
||||
{
|
||||
name: "discord_control",
|
||||
description: "Discord admin extension actions: private channel create/update and member list.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
action: {
|
||||
type: "string",
|
||||
enum: ["channel-private-create", "channel-private-update", "member-list"],
|
||||
},
|
||||
guildId: { type: "string" },
|
||||
name: { type: "string" },
|
||||
type: { type: "number" },
|
||||
parentId: { type: "string" },
|
||||
topic: { type: "string" },
|
||||
position: { type: "number" },
|
||||
nsfw: { type: "boolean" },
|
||||
allowedUserIds: { type: "array", items: { type: "string" } },
|
||||
allowedRoleIds: { type: "array", items: { type: "string" } },
|
||||
allowMask: { type: "string" },
|
||||
denyEveryoneMask: { type: "string" },
|
||||
channelId: { type: "string" },
|
||||
mode: { type: "string", enum: ["merge", "replace"] },
|
||||
addUserIds: { type: "array", items: { type: "string" } },
|
||||
addRoleIds: { type: "array", items: { type: "string" } },
|
||||
removeTargetIds: { type: "array", items: { type: "string" } },
|
||||
denyMask: { type: "string" },
|
||||
limit: { type: "number" },
|
||||
after: { type: "string" },
|
||||
fields: { anyOf: [{ type: "string" }, { type: "array", items: { type: "string" } }] },
|
||||
dryRun: { type: "boolean" },
|
||||
},
|
||||
required: ["action", "guildId"],
|
||||
},
|
||||
async execute(_id: string, params: Record<string, unknown>) {
|
||||
const action = String(params.action || "") as DiscordControlAction;
|
||||
const baseUrl = (config.discordControlApiBaseUrl || "http://127.0.0.1:8790").replace(/\/$/, "");
|
||||
const body = pickDefined({ ...params, action });
|
||||
|
||||
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
||||
if (config.discordControlApiToken) headers.Authorization = `Bearer ${config.discordControlApiToken}`;
|
||||
if (config.discordControlCallerId) headers["X-OpenClaw-Caller-Id"] = config.discordControlCallerId;
|
||||
|
||||
const r = await fetch(`${baseUrl}/v1/discord/action`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const text = await r.text();
|
||||
|
||||
if (!r.ok) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `discord_control failed (${r.status}): ${text}`,
|
||||
},
|
||||
],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text,
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
},
|
||||
{ optional: true },
|
||||
);
|
||||
}
|
||||
|
||||
api.registerHook("message:received", async (event, ctx) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user