【发布时间】:2021-11-28 21:20:30
【问题描述】:
我正在使用 Discord.js 和 Typescript(我是 Typescript 的新手)创建一个 Discord 机器人,并且正在制作一个命令处理程序。如果我使用的是 NodeJs,我会这样做:
const commandFiles = fs.readdirSync('./src/commands/').filter(file => file.endsWith('.ts'))
for (const file of commandFiles) {
const command = require(`./src/commands/${file}`)
commands.set(command.name, command)
}
但我想知道如何处理 Typescript。我试过这样做:
const commandFiles = fs.readdirSync('./src/commands/').filter(file => file.endsWith('.ts'))
for (const file of commandFiles) {
import(`./src/commands/${file}`).then(command => {
commands.set(command.name, command)
})
}
但我最终遇到了同样的错误:Error: Cannot find module './src/commands/pet.ts'。我知道这不是目录的问题。这是pet.ts中的代码:
export default {
name: 'pet',
description: 'description',
async execute({ interaction }) {
// doing stuff
}
}
如果这有帮助,这是我的tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"rootDir": "./src",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": false,
"skipLibCheck": true
},
"include": ["src"]
}
【问题讨论】:
-
不要编辑你解决了它。但是,您可以发布您如何解决它作为答案。
标签: typescript discord.js