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.
CodePudding user response:
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.