60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
|
import { execFileSync } from "node:child_process";
|
|
import path from "node:path";
|
|
import os from "node:os";
|
|
|
|
export function registerAddGuildCommand(api: OpenClawPluginApi): void {
|
|
api.registerCommand({
|
|
name: "add-guild",
|
|
description: "Add a Discord guild to the discord-guilds skill",
|
|
acceptsArgs: true,
|
|
handler: async (cmdCtx) => {
|
|
const args = (cmdCtx.args || "").trim();
|
|
if (!args) {
|
|
return {
|
|
text: "Usage: /add-guild <guild-id> <description>\nExample: /add-guild 123456789012345678 \"Production server\"",
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
const parts = args.split(/\s+/);
|
|
if (parts.length < 2) {
|
|
return {
|
|
text: "Error: Both guild-id and description are required.\nUsage: /add-guild <guild-id> <description>",
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
const guildId = parts[0];
|
|
const description = parts.slice(1).join(" ");
|
|
|
|
// Validate guild ID
|
|
if (!/^\d+$/.test(guildId)) {
|
|
return {
|
|
text: "Error: guild-id must be a numeric Discord snowflake (e.g., 123456789012345678)",
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
// Resolve the skill script path
|
|
const openClawDir = (api.config as Record<string, unknown>)?.["dirigentStateDir"] as string || path.join(os.homedir(), ".openclaw");
|
|
const scriptPath = path.join(openClawDir, "skills", "discord-guilds", "scripts", "add-guild");
|
|
|
|
try {
|
|
const result = execFileSync(process.execPath, [scriptPath, guildId, description], {
|
|
encoding: "utf8",
|
|
timeout: 5000,
|
|
});
|
|
return { text: result.trim() };
|
|
} catch (e: any) {
|
|
const stderr = e?.stderr?.toString?.() || "";
|
|
const stdout = e?.stdout?.toString?.() || "";
|
|
return {
|
|
text: `Failed to add guild: ${stderr || stdout || String(e)}`,
|
|
isError: true,
|
|
};
|
|
}
|
|
},
|
|
});
|
|
}
|