Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
(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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda