const { InteractionContextType, ApplicationIntegrationType, SlashCommandBuilder } = require("discord.js"); const { format } = require("node:util"); const { knex } = require("../../db.js"); const configData = { "yuri": { "description": "Automated yuri posting", "options": [{ "name": "enable", "required": true, "type": "Boolean", "description": "Should the bot post yuri?" }, { "name": "channel", "type": "Channel", "description": "Where to post yuri", }] } } const data = new SlashCommandBuilder() .setName("serverconfig") .setDescription("Manage your server settings") .setContexts([ InteractionContextType.Guild ]) .setIntegrationTypes([ ApplicationIntegrationType.GuildInstall ]); for (const cfg in configData) { const config = configData[cfg]; data.addSubcommand((builder) => { builder .setName(cfg) .setDescription(config.description); for (const opt in config.options) { const option = config.options[opt]; builder[`add${option.type}Option`](optionBuilder => optionBuilder .setName(option.name) .setRequired(option.required ?? false) .setDescription(option.description) ); } return builder; }); } module.exports = { data, async execute(interaction) { var result = await knex.select("data").from("serverconfigs").where("id", interaction.guildId).first(); if (!result) result = { data: '{}' }; const config = JSON.parse(result.data); const cfg = interaction.options.getSubcommand(true); config[cfg] = config[cfg] ?? {}; for (const option of configData[cfg].options) { config[cfg][option.name] = interaction.options[`get${option.type}`](option.name); } const data = { id: interaction.guildId, data: JSON.stringify(config) } await knex.raw(format('%s ON CONFLICT (id) DO UPDATE SET %s', knex("serverconfigs").insert(data).toString().toString(), knex("serverconfigs").update(data).whereRaw(`'serverconfigs'.id = '${interaction.guildId}'`).toString().replace(/^update\s.*\sset\s/i, '') )); interaction.reply({ content: "Settings updated!", ephemeral: true }); }, async autocomplete(interaction) { const focusedOption = interaction.options.getFocused(true); const command = interaction.options.getSubcommand(true); console.log(command, focusedOption); const id = ""; const choices = []; for (const option in configData) { if (focusedOption.name == "name" && option.startsWith(focusedOption.value)) choices.push(option); else if (focusedOption.name == "value" && (option == interaction.options.getString("name") ?? "")) choices.push(...buildChoices(option, interaction)); } await interaction.respond(choices.map(choice => ({ name: choice, value: choice }))) }, };