(属性) CommandInteractionOption<CacheType>.value?: 字符串 |编号 | boolean Discord.js 斜杠命令选项
(property) CommandInteractionOption<CacheType>.value?: string | number | boolean Discord.js Slash command options
我正在尝试使用打字稿制作我的第一个 Discord Bot。
我用个人 npm 包制作了一个简单的掷骰子命令,但 Discord Slash 命令选项似乎有问题。
我做了一个不需要的 sum
选项,它将任何整数加到掷骰子上。但是当我尝试编写该部分的代码时,它 returns 这些错误:
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)
这是我的代码:
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)}.**`);
}
}
}
});
错误在 sum.value
和 +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)}.**`);`
我在 JavaScript 中使用 @discordjs/builder
“SlashCommandBuilder()”尝试了完全相同的命令,它工作正常。
核心问题是 inlineCode
期望传入的值具有 string
类型。但是,sum.value
可以是 string
、number
或 boolean
,因此它不能接受,因为它可能是 number
或 [=17] =].
针对您的情况最简单的解决方案是将您传递的内容包装到 String
方法中的函数中,这些方法将在传递值之前将其转换为字符串:
${inlineCode(String(sum.value))}
和
inlineCode(String(+rolls[0] + +sum.value))
我还建议将 +rolls[0] + +sum.value
更改为 rolls[0] + sum.value
,因为 +
前缀对算术运算的结果没有影响。但是,我将保留此答案以遵守您问题示例中提供的代码。
我正在尝试使用打字稿制作我的第一个 Discord Bot。
我用个人 npm 包制作了一个简单的掷骰子命令,但 Discord Slash 命令选项似乎有问题。
我做了一个不需要的 sum
选项,它将任何整数加到掷骰子上。但是当我尝试编写该部分的代码时,它 returns 这些错误:
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)
这是我的代码:
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)}.**`);
}
}
}
});
错误在 sum.value
和 +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)}.**`);`
我在 JavaScript 中使用 @discordjs/builder
“SlashCommandBuilder()”尝试了完全相同的命令,它工作正常。
核心问题是 inlineCode
期望传入的值具有 string
类型。但是,sum.value
可以是 string
、number
或 boolean
,因此它不能接受,因为它可能是 number
或 [=17] =].
针对您的情况最简单的解决方案是将您传递的内容包装到 String
方法中的函数中,这些方法将在传递值之前将其转换为字符串:
${inlineCode(String(sum.value))}
和
inlineCode(String(+rolls[0] + +sum.value))
我还建议将 +rolls[0] + +sum.value
更改为 rolls[0] + sum.value
,因为 +
前缀对算术运算的结果没有影响。但是,我将保留此答案以遵守您问题示例中提供的代码。