我在嘗試為我的機器人設定命令時遇到 TypeError。嘗試使用 commands.set 時會發生這種情況
function setCommands(): void {
var slashCommandFiles = fs.readdirSync('./commands').filter((file: string) => (file.endsWith('.js') && !forbiddenCommands.includes(file.slice(0, -3))));
for (const file of slashCommandFiles) {
const command = require(`./commands/${file}`);
slashCommands.push(command.data.toJSON());
if (client.application)
client.application.commands.set(command.data.name, command); //happens here
else {
console.error(client);
throw new Error("Unable to load the commands");
}
}
}
錯誤訊息如下:
C:\Overbot\node_modules\discord.js\src\managers\ApplicationCommandManager.js:147
data: commands.map(c => this.constructor.transformCommand(c)),
^
TypeError: commands.map is not a function
at ApplicationCommandManager.set (C:\Overbot\node_modules\discord.js\src\managers\ApplicationCommandManager.js:147:22)
at setCommands (C:\Overbot\index.js:68:41)
at C:\Overbot\index.js:108:17
at step (C:\Overbot\index.js:34:23)
at Object.next (C:\Overbot\index.js:15:53)
at C:\Overbot\index.js:9:71
at new Promise (<anonymous>)
at __awaiter (C:\Overbot\index.js:5:12)
at Client.<anonymous> (C:\Overbot\index.js:98:43)
at Object.onceWrapper (node:events:514:26)
事實是,它絕對是一個確實存在的函式,因為我驗證了代碼并發現它沒有問題。我也不認為這是一個分號惡作劇,因為我已經在任何地方用分號和不用分號對其進行了測驗,結果相同。
有什么辦法可以使這項作業?
uj5u.com熱心網友回復:
這不是您設定命令的方式。改用這個:
client.application.commands.set(slashCommands)
//I assume slashCommands is an array of ApplicationCommandData, according to your code
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370690.html
標籤:javascript 打字稿 不和谐.js
