我正在嘗試使用打字稿制作我的第一個 Discord Bot。我用個人 npm 包做了一個簡單的擲骰子命令,但 Discord Slash 命令選項似乎有問題。我做了一個sum不需要的選項,它將任何整數與擲骰子相加。但是當我嘗試對該部分進行編碼時,它會回傳以下錯誤:
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()”嘗試了相同的命令,它運行良好。
uj5u.com熱心網友回復:
核心問題是inlineCode期望傳入的值具有string型別。但是,sum.value可以是或string,因此它不能接受,因為它可能是或。numberbooleannumberboolean
對于您的情況,最簡單的解決方案是將您傳遞的內容包裝到方法中的函式中,String這些方法將在傳入之前將值轉換為字串:
${inlineCode(String(sum.value))}
和
inlineCode(String( rolls[0] sum.value))
我還建議更改 rolls[0] sum.value為rolls[0] sum.value,因為 前綴對算術運算的結果沒有影響。但是,我將保留此答案以遵守您問題示例中提供的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/463577.html
標籤:javascript 打字稿 不和谐.js
上一篇:構建Docker容器并將其部署到CloudRun時遇到問題
下一篇:以編程方式關閉作業表
