Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
(property) CommandInteractionOption<CacheType>.value?: string | number | boolean Discord.js Slash command options

I'm trying to make my first Discord Bot using typescript. I made a simple dice roll command with a personal npm package, but there seems to be a problem with the Discord Slash command options. I made a sum option that is NOT required, which sums any integer to the dice roll. But when I try to code that part, it returns these errors:
1-Argument of type 'string | number | boolean' is not assignable to parameter of type 'string'
2-Type 'number' is not assignable to type 'string'.ts(2345)

here is my code:

import { Command } from "../../structures/Command";
import { inlineCode } from "@discordjs/builders";
import aimless from "aimless";

export default new Command({
    name: "roll",
    description: "Rolls a dice.",
    options: [
        {
            name: "dice",
            description: "The dice you want to roll",
            type: "STRING",
            required: true
        },
        {
            name: "sum",
            description: "The sum you want to add to your roll(s)",
            type: "INTEGER",
            required: false
        }
    ],
    run: async({ interaction }) => {
        const dice = interaction.options.get("dice").value;
        const sum = interaction.options.get("sum");
        console.log(sum)
        let rolls;

        try {
            rolls = aimless.roll(dice);
        } catch (e) {
            return interaction.followUp(`**Invalid dice! \n${inlineCode(e.toString().split(":")[2])}**`)
        }

        if (rolls.length == 1) {
            if (!sum) {
                return interaction.followUp(`**You rolled a ${inlineCode(rolls[0])}!**`);
            } else {
                return interaction.reply(`**You rolled a ${inlineCode(rolls[0])}!\n ${inlineCode(rolls[0])} + ${inlineCode(sum.value)} = ${inlineCode(+rolls[0] + +sum.value)}.**`);
            }
        }
    }
});

The errors are in this line at sum.value and +rolls[0] + +sum.value:

` return interaction.reply(`**You rolled a ${inlineCode(rolls[0])}!\n ${inlineCode(rolls[0])} + ${inlineCode(sum.value)} = ${inlineCode(+rolls[0] + +sum.value)}.**`);`

I tried the same exact command in JavaScript with the @discordjs/builder "SlashCommandBuilder()" and it worked fine.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The core problem is that inlineCode is expecting the value passed in to have a string type. However, sum.value can be either string, number or boolean, hence it could not accept due to the possibility of it being a number or boolean.

The simplest solution for your case is to wrap what you are passing into the functions inside String methods which will convert the values into strings before they are passed in:

${inlineCode(String(sum.value))}

and

inlineCode(String(+rolls[0] + +sum.value))

I would also advise changing +rolls[0] + +sum.value into rolls[0] + sum.value as the + prefix has no effect on the result of the arithmetic operation. However, I will leave this answer to adhere to the code provided in your question's example.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda