Files
FemScoreboard/discord/commands/config/edit_sysprompt.ts
2026-02-27 02:48:25 -08:00

48 lines
1.6 KiB
TypeScript

import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js';
import 'dotenv/config';
import fs = require('node:fs');
import path = require('node:path');
const syspromptCache = path.resolve(__dirname, 'sysprompt_cache');
const SAFE_NAME_REGEX = /^[\w\d]+$/;
async function editSyspromptCommand(interaction: ChatInputCommandInteraction) {
if (interaction.user.id !== process.env.ADMIN) {
await interaction.reply('You are not authorized to change model settings');
return;
}
const name = interaction.options.getString('name', true);
if (!SAFE_NAME_REGEX.test(name)) {
await interaction.reply('Failed to edit system prompt: name must be alphanumeric.');
return;
}
const contentUrl = interaction.options.getAttachment('content', true).url;
const contentDownload = await fetch(contentUrl);
const content = await contentDownload.text();
fs.writeFileSync(path.resolve(syspromptCache, `${name}.txt`), content);
await interaction.reply(`System prompt "${name}" set to \`\`\`
${content.slice(0, 1500)}${content.length > 1500 ? '...' : ''}
\`\`\``);
}
export = {
data: new SlashCommandBuilder()
.setName('edit')
.setDescription('Edit system prompts')
.addStringOption((opt) =>
opt.setName('name').setDescription('Name (must be alphanumeric)').setRequired(true)
)
.addAttachmentOption((opt) =>
opt
.setName('content')
.setDescription('Text file containing the system prompt')
.setRequired(true)
),
execute: editSyspromptCommand,
};