refactor #22
59
plugin/commands/add-guild-command.ts
Normal file
59
plugin/commands/add-guild-command.ts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import { registerBeforePromptBuildHook } from "./hooks/before-prompt-build.js";
|
|||||||
import { registerBeforeMessageWriteHook } from "./hooks/before-message-write.js";
|
import { registerBeforeMessageWriteHook } from "./hooks/before-message-write.js";
|
||||||
import { registerMessageSentHook } from "./hooks/message-sent.js";
|
import { registerMessageSentHook } from "./hooks/message-sent.js";
|
||||||
import { registerDirigentCommand } from "./commands/dirigent-command.js";
|
import { registerDirigentCommand } from "./commands/dirigent-command.js";
|
||||||
|
import { registerAddGuildCommand } from "./commands/add-guild-command.js";
|
||||||
import { registerDirigentTools } from "./tools/register-tools.js";
|
import { registerDirigentTools } from "./tools/register-tools.js";
|
||||||
import { ensurePolicyStateLoaded, persistPolicies, policyState } from "./policy/store.js";
|
import { ensurePolicyStateLoaded, persistPolicies, policyState } from "./policy/store.js";
|
||||||
import { buildAgentIdentity, buildUserIdToAccountIdMap, resolveAccountId } from "./core/identity.js";
|
import { buildAgentIdentity, buildUserIdToAccountIdMap, resolveAccountId } from "./core/identity.js";
|
||||||
@@ -201,6 +202,9 @@ export default {
|
|||||||
ensurePolicyStateLoaded,
|
ensurePolicyStateLoaded,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Register add-guild command
|
||||||
|
registerAddGuildCommand(api);
|
||||||
|
|
||||||
// Handle NO_REPLY detection before message write
|
// Handle NO_REPLY detection before message write
|
||||||
registerBeforeMessageWriteHook({
|
registerBeforeMessageWriteHook({
|
||||||
api,
|
api,
|
||||||
|
|||||||
Reference in New Issue
Block a user