Home > database >  TypeError: (intermediate value).setName(...).setDescription(...).addStringOption(...).addChoices is
TypeError: (intermediate value).setName(...).setDescription(...).addStringOption(...).addChoices is

Time:07-22

I am trying to make a command with choices that the user can pick from in my discord bot, but I am getting this error.

  .addChoices(
   ^

TypeError: (intermediate value).setName(...).setDescription(...).addStringOption(...).addChoices is not a function
    at Object.<anonymous> (C:\Users\dhart\Desktop\Projects\ExistentialThreat\commands\coinflip.js:10:4)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at c:\Users\dhart\Desktop\Projects\ExistentialThreat\handler\index.js:13:18
    at Array.map (<anonymous>)
    at module.exports (c:\Users\dhart\Desktop\Projects\ExistentialThreat\handler\index.js:12:17)

My code is:

const { EmbedBuilder } = require("discord.js");
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
  data: new SlashCommandBuilder()
  .setName('coinflip')
  .setDescription('Returns value heads or tails.')
  .addStringOption((option) => option.setName('guess')
  .setDescription('Enter heads or tails.').setRequired(true))
  .addChoices(
    { name: 'Heads', value: 'Guess_heads'}, 
    { name: 'Tails', value: 'Guess_tails'},
    ),

  async execute(interaction) {
    let coinflip = (Math.random() * 100).toFixed(0) % 2;

I am new to discord.js and I would appreciate some help.

CodePudding user response:

The fact you put a ) after .setRequired(true) makes it that you are trying to use .addChoices on the slash command builder itself instead of the string option. A simply typo, just remove the trailing ) and put it at the end of the SlashCommandBuilder like so :

const { EmbedBuilder } = require("discord.js");
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
  data: new SlashCommandBuilder()
  .setName('coinflip')
  .setDescription('Returns value heads or tails.')
  .addStringOption((option) => option.setName('guess')
  .setDescription('Enter heads or tails.')
  .setRequired(true)
  .addChoices(
    { name: 'Heads', value: 'Guess_heads'}, 
    { name: 'Tails', value: 'Guess_tails'},
   )),
  • Related