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

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

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

问题

以下是您要翻译的内容:

  1. 我有一个需要用户提供3个选择的斜杠命令team1team2和游戏数量我将它们添加为命令的参数如下所示
  2. data: new SlashCommandBuilder()
  3. .setName('addgame')
  4. .setDescription('设置一个用于下注的游戏。')
  5. .addStringOption(option =>
  6. option.setName('team1')
  7. .setDescription('队伍1')
  8. .setRequired(true)
  9. .addChoices(
  10. { name: 'Bilibili Gaming', value: 'Bilibili Gaming' },
  11. { name: 'JDG Gaming', value: 'JDG Gaming' },
  12. { name: 'Gen.G', value: 'Gen.G' },
  13. { name: 'T1', value: 'T1' },
  14. { name: 'Cloud9', value: 'Cloud9' },
  15. { name: 'Golden Guardians', value: 'Golden Guardians' },
  16. { name: 'G2 Esports', value: 'G2 Esports' },
  17. ))
  18. .addStringOption(option =>
  19. option.setName('team2')
  20. .setDescription('队伍2')
  21. .setRequired(true)
  22. .addChoices(
  23. { name: 'Bilibili Gaming', value: 'Bilibili Gaming' },
  24. { name: 'JDG Gaming', value: 'JDG Gaming' },
  25. { name: 'Gen.G', value: 'Gen.G' },
  26. { name: 'T1', value: 'T1' },
  27. { name: 'Cloud9', value: 'Cloud9' },
  28. { name: 'Golden Guardians', value: 'Golden Guardians' },
  29. { name: 'G2 Esports', value: 'G2 Esports' },
  30. ))
  31. .addStringOption(option =>
  32. option.setName('series')
  33. .setDescription('游戏数量')
  34. .setRequired(true)
  35. .addChoices(
  36. { name: '1', value: '1' },
  37. { name: '3', value: '3' },
  38. { name: '5', value: '5' },
  39. )),
  40. async execute(interaction) {
  41. // 设置队伍信息
  42. const teamList = await getTeams();
  43. const team1 = interaction.options.getString('team1');
  44. const team2 = interaction.options.getString('team2');
  45. // ...
  46. }

我只翻译了您提供的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:

  1. data: new SlashCommandBuilder()
  2. .setName('addgame')
  3. .setDescription('Sets up a game for placing bets.')
  4. .addStringOption(option =>
  5. option.setName('team1')
  6. .setDescription('Team 1')
  7. .setRequired(true)
  8. .addChoices(
  9. { name: 'Bilibili Gaming', value: 'Bilibili Gaming'},
  10. { name: 'JDG Gaming', value: 'JDG Gaming'},
  11. { name: 'Gen.G', value: 'Gen.G'},
  12. { name: 'T1', value: 'T1'},
  13. { name: 'Cloud9', value: 'Cloud9'},
  14. { name: 'Golden Guardians', value: 'Golden Guardians'},
  15. { name: 'G2 Esports', value: 'G2 Esports'},
  16. ))
  17. .addStringOption(option =>
  18. option.setName('team2')
  19. .setDescription('Team 2')
  20. .setRequired(true)
  21. .addChoices(
  22. { name: 'Bilibili Gaming', value: 'Bilibili Gaming'},
  23. { name: 'JDG Gaming', value: 'JDG Gaming'},
  24. { name: 'Gen.G', value: 'Gen.G'},
  25. { name: 'T1', value: 'T1'},
  26. { name: 'Cloud9', value: 'Cloud9'},
  27. { name: 'Golden Guardians', value: 'Golden Guardians'},
  28. { name: 'G2 Esports', value: 'G2 Esports'},
  29. ))
  30. .addStringOption(option =>
  31. option.setName('series')
  32. .setDescription('Number of games')
  33. .setRequired(true)
  34. .addChoices(
  35. { name: '1', value: '1'},
  36. { name: '3', value: '3'},
  37. { name: '5', value: '5'},
  38. )),
  39. async execute(interaction) {
  40. // Set up team information
  41. const teamList = await getTeams();
  42. const team1 = interaction.options.getString('team1');
  43. const team2 = interaction.options.getString('team2');
  44. ...

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:

  1. const teamList = await getTeams();
  2. const choices = teamList.map(team => ({ name: team.name, value: team.value }));
  3. ...
  4. .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:

  1. async function fetchTeamList() {
  2. const teamList = await getTeams();
  3. const teams = teamList.map(team => ({ name: team.Name, value: team.Name }));
  4. return teams;
  5. }
  6. ...
  7. fetchTeamList().then(teams => {
  8. console.log(teams);
  9. module.exports.teams = teams;
  10. });

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:

  1. data: new SlashCommandBuilder()
  2. .setName('addgame')
  3. .setDescription('Sets up a game for placing bets.')
  4. .addStringOption(async option =>{
  5. const teamList = await getTeams();
  6. const choices = teamList.map(team => ({ name: team.name, value: team.value }));
  7. return option
  8. .setName('team1')
  9. .setDescription('Team 1')
  10. .setRequired(true)
  11. .addChoices(...choices)
  12. })

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函数设为异步,并在函数内部像这样获取团队信息

  1. .addStringOption(async option => {
  2. const teamList = await getTeams();
  3. const choices = teamList.map(team => ({ name: team.name, value: team.value }));
  4. return option
  5. .setName('team1')
  6. .setDescription('Team 1')
  7. .setRequired(true)
  8. .addChoices(...choices)
  9. }),
英文:

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

  1. .addStringOption(async option => {
  2. const teamList = await getTeams();
  3. const choices = teamList.map(team => ({ name: team.name, value: team.value }));
  4. return option
  5. .setName('team1')
  6. .setDescription('Team 1')
  7. .setRequired(true)
  8. .addChoices(...choices)
  9. }),
  10. </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:

确定