如何在使用时更新 slashcommand 中的选项? (discord.js)

How can i update the choices in a slashcommand on use? (discord.js)

我正在尝试对使用中的 discord 命令进行更新。 slash 命令的想法是能够使用该命令删除 trello 上的卡片。斜杠命令命令由 2 个子命令(创建、删除)组成。

我正在尝试让命令重新加载并重新读取 json 文件以将新的 addChoice 选项列表添加到子命令。

这是命令的代码:

const fetch = require('node-fetch');
const fs = require('node:fs');
var lists = JSON.parse(fs.readFileSync('./data/lists.json'))
var cards = JSON.parse(fs.readFileSync(`./data/cards.json`));
const { REST } = require('@discordjs/rest');
const { clientId, guildId, token } = require('../config.json');
const { Routes } = require('discord-api-types/v9');
var commands; 
module.exports = {
    data: new SlashCommandBuilder()

        .setName('card')
        .setDescription('Card command!')
        .addSubcommand(subcommand =>
            subcommand
                .setName("create")
                .setDescription("Creates a card")
                .addStringOption(option => {
                    option.setName('list')
                    option.setDescription('Specify the list to add the card to')
                    option.setRequired(true)
                    for (let i = 0; i < lists.length; i++) {
                        option.addChoice(`${lists[i].name}`, `${lists[i].id}`)
                    }
                    return option;
                })
                .addStringOption(option => option.setName("title").setDescription("Specify the title of the card").setRequired(true)))

        .addSubcommand(subcommand =>
            subcommand
                .setName("delete")
                .setDescription("Deletes a card")
                .addStringOption(option => {
                    option.setName('list')
                    option.setDescription('Specify the list to add the card to')
                    option.setRequired(true)
                    for (let i = 0; i < cards.length; i++) {
                        option.addChoice(`${cards[i].name}`, `${cards[i].id}`)
                    }
                    return option;
                })
                ),

    async execute(interaction, client) {
        //CREATE CARD
        await (async () => {
            if (interaction.options.getSubcommand() === 'create') {
                fetch('http://127.0.0.1:3000/trello/createCard', {
                method: "POST",
                headers: {
                    'Content-Type': "application/json",
                },
                body: JSON.stringify({idList: `${interaction.options.getString('list')}`, title: `${interaction.options.getString('title')}`})
                }).then(function(res) {
                    if(res.status !== 200) {
                        console.log('COULD NOT CREATE CARD')
                        interaction.reply({content: `ERROR ${res.status}: COULD NOT CREATE CARD`,
                                ephemeral: true})
                    } else {
                        console.log('CARD CREATED')
                        interaction.reply({content: `CARD CREATED`,
                                ephemeral: true})
                    }
    
                })
            } 
            //DELETE CARD
            else if (interaction.options.getSubcommand() === 'delete') {
                fetch('http://127.0.0.1:3000/trello/deleteCard', {
                method: "POST",
                headers: {
                    'Content-Type': "application/json",
                },
                body: JSON.stringify({cardId: `${interaction.options.getString('list')}`})
                }).then(async function(res) {
                    if(res.status !== 200) {
                        console.log('COULD NOT DELETE CARD')
                        interaction.reply({content: `ERROR ${res.status}: COULD NOT DELETE CARD`,
                                ephemeral: true})
                    } else {
                        console.log('CARD DELETED')
                        interaction.reply({content: `CARD DELETED`,
                                ephemeral: true})
                    }
    
                })
            } 
            
            else {
                interaction.reply("ERROR 40: NOT A VALID SUBCOMMAND")
            }
        })()
    },
};

有什么我可以添加到 bot.js 中的,也许可以完全重新加载命令并使模块重新读取 json。

在这种情况下你唯一能做的就是re-registering用新数据斜线命令。

或者,您可以尝试使用新的自动完成交互选项类型。