Discord: auto-register slash command
This commit is contained in:
committed by
Peter Steinberger
parent
b135b3efb9
commit
fff9efe8a8
@@ -844,6 +844,58 @@ function resolveSlashPrompt(
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function ensureSlashCommand(
|
||||||
|
client: Client,
|
||||||
|
slashCommand: Required<DiscordSlashCommandConfig>,
|
||||||
|
runtime: RuntimeEnv,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const appCommands = client.application?.commands;
|
||||||
|
if (!appCommands) {
|
||||||
|
runtime.error?.(danger("discord slash commands unavailable"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const existing = await appCommands.fetch();
|
||||||
|
let hasCommand = false;
|
||||||
|
for (const entry of existing.values()) {
|
||||||
|
if (entry.name === slashCommand.name) {
|
||||||
|
hasCommand = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
await entry.delete();
|
||||||
|
}
|
||||||
|
if (hasCommand) return;
|
||||||
|
await appCommands.create({
|
||||||
|
name: slashCommand.name,
|
||||||
|
description: "Ask Clawdis a question",
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: "prompt",
|
||||||
|
description: "What should Clawdis help with?",
|
||||||
|
type: ApplicationCommandOptionType.String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
runtime.log?.(`registered discord slash command /${slashCommand.name}`);
|
||||||
|
} catch (err) {
|
||||||
|
const status = (err as { status?: number | string })?.status;
|
||||||
|
const code = (err as { code?: number | string })?.code;
|
||||||
|
const message = String(err);
|
||||||
|
const isRateLimit =
|
||||||
|
status === 429 ||
|
||||||
|
code === 429 ||
|
||||||
|
/rate ?limit/i.test(message);
|
||||||
|
const text = `discord slash command setup failed: ${message}`;
|
||||||
|
if (isRateLimit) {
|
||||||
|
logVerbose(text);
|
||||||
|
runtime.error?.(warn(text));
|
||||||
|
} else {
|
||||||
|
runtime.error?.(danger(text));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function findFirstStringOption(
|
function findFirstStringOption(
|
||||||
options: readonly CommandInteractionOption[],
|
options: readonly CommandInteractionOption[],
|
||||||
): string | undefined {
|
): string | undefined {
|
||||||
|
|||||||
Reference in New Issue
Block a user