【发布时间】:2021-09-21 22:03:40
【问题描述】:
我正在将我的机器人转换为 ES6 语法。由于我不能使用:const command = require(`./commands/${file}`),我尝试使用import command from `./commands/${file}`;。它没有用,它给了我一个奇怪的错误:
file:///D:/Documenti/Coding/Discord/XayBot/XayBotMain/src/index.js:15
import command from `./commands/${file}`;
^^^^^^^
SyntaxError: Unexpected identifier
这是它正在运行的代码:
import { Client, Collection } from "discord.js";
import { readdir } from "fs-extra";
const client = new Client({
intents: ["GUILDS", "GUILD_MEMBERS", "GUILD_MESSAGES"],
});
client.commands = new Collection();
client.once("ready", async () => {
try {
const commandFiles = readdir("./src/commands").filter(
file.endsWith(".js"),
);
commandFiles.forEach(async (file) => {
import command from `./commands/${file}`;
client.commands.set(command.name, command);
});
} catch (err) {
console.error(err);
}
});
client.login(process.env.D_TOKEN);
我在 package.json 中使用"type": "module",所以这不是问题,我想我可能使用错了,如果是,我该如何解决?
Nodejs 版本:16.9.0 | Npm 版本:7.22 | Discord.js 版本:13.1.0
【问题讨论】:
标签: node.js ecmascript-6 discord.js