Discord.js 使用 .addChoices() 动态添加选项到斜杠命令。

huangapple go评论90阅读模式
英文:

Discord.js dynamically add options to slash command with .addChoices()

问题

以下是您要翻译的内容:

我有一个需要用户提供3个选择的斜杠命令team1team2和游戏数量我将它们添加为命令的参数如下所示

data: new SlashCommandBuilder()
    .setName('addgame')
    .setDescription('设置一个用于下注的游戏。')
    .addStringOption(option =>
        option.setName('team1')
            .setDescription('队伍1')
            .setRequired(true)
            .addChoices(
                { name: 'Bilibili Gaming', value: 'Bilibili Gaming' },
                { name: 'JDG Gaming', value: 'JDG Gaming' },
                { name: 'Gen.G', value: 'Gen.G' },
                { name: 'T1', value: 'T1' },
                { name: 'Cloud9', value: 'Cloud9' },
                { name: 'Golden Guardians', value: 'Golden Guardians' },
                { name: 'G2 Esports', value: 'G2 Esports' },
            ))
    .addStringOption(option =>
        option.setName('team2')
            .setDescription('队伍2')
            .setRequired(true)
            .addChoices(
                { name: 'Bilibili Gaming', value: 'Bilibili Gaming' },
                { name: 'JDG Gaming', value: 'JDG Gaming' },
                { name: 'Gen.G', value: 'Gen.G' },
                { name: 'T1', value: 'T1' },
                { name: 'Cloud9', value: 'Cloud9' },
                { name: 'Golden Guardians', value: 'Golden Guardians' },
                { name: 'G2 Esports', value: 'G2 Esports' },
            ))
    .addStringOption(option =>
        option.setName('series')
            .setDescription('游戏数量')
            .setRequired(true)
            .addChoices(
                { name: '1', value: '1' },
                { name: '3', value: '3' },
                { name: '5', value: '5' },
            )),
    async execute(interaction) {
        // 设置队伍信息
        const teamList = await getTeams();
        const team1 = interaction.options.getString('team1');
        const team2 = interaction.options.getString('team2');
        // ...
    }

我只翻译了您提供的JavaScript代码部分,没有包括其他内容。如果您需要更多帮助,请告诉我。

英文:

I have a slash command that requires 3 choices from the user: team1, team2, and how many games. I'm adding those as parameters for the command like this:

data: new SlashCommandBuilder()
.setName('addgame')
.setDescription('Sets up a game for placing bets.')
.addStringOption(option =>
option.setName('team1')
.setDescription('Team 1')
.setRequired(true)
.addChoices(
{ name: 'Bilibili Gaming', value: 'Bilibili Gaming'},
{ name: 'JDG Gaming', value: 'JDG Gaming'},
{ name: 'Gen.G', value: 'Gen.G'},
{ name: 'T1', value: 'T1'},
{ name: 'Cloud9', value: 'Cloud9'},
{ name: 'Golden Guardians', value: 'Golden Guardians'},
{ name: 'G2 Esports', value: 'G2 Esports'},
))
.addStringOption(option =>
option.setName('team2')
.setDescription('Team 2')
.setRequired(true)
.addChoices(
{ name: 'Bilibili Gaming', value: 'Bilibili Gaming'},
{ name: 'JDG Gaming', value: 'JDG Gaming'},
{ name: 'Gen.G', value: 'Gen.G'},
{ name: 'T1', value: 'T1'},
{ name: 'Cloud9', value: 'Cloud9'},
{ name: 'Golden Guardians', value: 'Golden Guardians'},
{ name: 'G2 Esports', value: 'G2 Esports'},
))
.addStringOption(option =>
option.setName('series')
.setDescription('Number of games')
.setRequired(true)
.addChoices(
{ name: '1', value: '1'},
{ name: '3', value: '3'},
{ name: '5', value: '5'},
)),
async execute(interaction) {
// Set up team information
const teamList = await getTeams();
const team1 = interaction.options.getString('team1');
const team2 = interaction.options.getString('team2');
...

Not only are the .addChoices() properties redundant here and harder to maintain, but I actually get those same values from the database later in the slash command using a getTeams() function. I'm wondering how I could call that getTeams async function and use the result to populate addChoices()?

I could do something like this:

const teamList = await getTeams();
const choices = teamList.map(team => ({ name: team.name, value: team.value }));
...
.addChoices(...choices)

However, because getTeams() is an async function I can't use await outside of the async execute(interaction) method.

Is what I'm wanting to do not possible? If I had to restructure the slash command to achieve this, what would that look like? Any advice appreciated.

EDIT:
I even tried doing something like this in my index.js:

async function fetchTeamList() {
const teamList = await getTeams();
const teams = teamList.map(team => ({ name: team.Name, value: team.Name }));
return teams;
}
...
fetchTeamList().then(teams => {
console.log(teams);
module.exports.teams = teams;
});

And then importing teams in my slash command file but that didn't work either. I'm not sure that what I'm trying to do is possible...
Using Node.js v18.17.0 and discord.js: ^14.11.0

EDIT:

I have tried this as well:

data: new SlashCommandBuilder()
.setName('addgame')
.setDescription('Sets up a game for placing bets.')
.addStringOption(async option =>{
const teamList = await getTeams();
const choices = teamList.map(team => ({ name: team.name, value: team.value }));
return option
.setName('team1')
.setDescription('Team 1')
.setRequired(true)
.addChoices(...choices)
})

As was recommended in an answer but this throws an error dealing with the ```@sapphire/shapeshifpackage. I believe this isn't working because I'm using an asynchronous callback foraddStringOption```` to fetch the team choices dynamically but the subsequent options are added synchronously... which might cause a problem?

答案1

得分: 1

你可以将addStringOption函数设为异步,并在函数内部像这样获取团队信息

.addStringOption(async option => {
    const teamList = await getTeams();
    const choices = teamList.map(team => ({ name: team.name, value: team.value }));

    return option
        .setName('team1')
        .setDescription('Team 1')
        .setRequired(true)
        .addChoices(...choices)
}),
英文:

You can make the addStringOpton function async and get the teams inside the function like this

.addStringOption(async option => {
    const teamList = await getTeams();
    const choices = teamList.map(team => ({ name: team.name, value: team.value }));

    return option
        .setName('team1')
        .setDescription('Team 1')
        .setRequired(true)
        .addChoices(...choices)
}),

</details>



huangapple
  • 本文由 发表于 2023年8月9日 09:19:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863994-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定