为什么我的代码 return "Unexpected token ="?
Why does my code return "Unexpected token ="?
我正在开发 Discord.TS 机器人,但是当我使用 ts-node src/index.ts
启动它时,我看到了错误
/home/negative/Dev/JS/Bot/src/structures/Client.ts:10
commands = new discord_js_1.Collection();
^
SyntaxError: Unexpected token =
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Module.m._compile (/home/negative/Dev/JS
...
这是我的代码 (Client.ts)
import {
ApplicationCommandDataResolvable,
Client,
ClientEvents,
Collection
} from "discord.js";
import { CommandType } from "../typings/Command";
import glob from "glob";
import { promisify } from "util";
import { RegisterCommandsOptions } from "../typings/client";
import { Event } from "./Event";
const globPromise = promisify(glob);
export class ExtendedClient extends Client {
commands: Collection<string, CommandType> = new Collection();
constructor() {
super({ intents: 32767 });
}
start() {
this.registerModules();
this.login(process.env.botToken);
}
async importFile(filePath: string) {
return (await import(filePath))?.default;
}
async registerCommands({ commands, guildId }: RegisterCommandsOptions) {
if (guildId) {
this.guilds.cache.get(guildId)?.commands.set(commands);
console.log(`Registering commands to ${guildId}`);
} else {
this.application?.commands.set(commands);
console.log("Registering global commands");
}
}
async registerModules() {
// Commands
const slashCommands: ApplicationCommandDataResolvable[] = [];
const commandFiles = await globPromise(
`${__dirname}/../commands/*/*{.ts,.js}`
);
commandFiles.forEach(async (filePath) => {
const command: CommandType = await this.importFile(filePath);
if (!command.name) return;
console.log(command);
this.commands.set(command.name, command);
slashCommands.push(command);
});
this.on("ready", () => {
this.registerCommands({
commands: slashCommands,
guildId: process.env.guildId
});
});
// Event
const eventFiles = await globPromise(
`${__dirname}/../events/*{.ts,.js}`
);
eventFiles.forEach(async (filePath) => {
const event: Event<keyof ClientEvents> = await this.importFile(
filePath
);
this.on(event.event, event.run);
});
}
}
Visual Studio 代码说我的代码没有错误,但我无法修复这个。有人可以告诉我如何解决吗?我一直在研究这个问题......
如果你知道如何解决这个问题,请告诉我,我会很高兴的。
此错误是因为您的目标 JavaScript 版本不受您安装的 Node.js 版本的支持。您可以更改目标 JavaScript 版本,或更新 Node.js 版本。
选项 1:更新 Node.js 版本
确保您至少安装了 Node.js 版本 16.11.0
(根据 node.green this is the first version to fully support instance class fields)。
您可以通过在终端中输入 node -v
来检查您的节点版本。
$ node -v
v17.1.0
然后您可以从他们的网站安装最新的 Node.js 版本:https://nodejs.org/
选项 2:更改目标 JavaScript 版本
打开您的 tsconfig.json
文件并将 target field 更改为您当前 Node.js 版本支持的版本。
您可以通过在终端中输入 node -v
来检查您的节点版本。
$ node -v
v17.1.0
请参考 node.green 以根据您当前的 Node.js 版本确定您可以定位的 JavaScript 版本。
我正在开发 Discord.TS 机器人,但是当我使用 ts-node src/index.ts
启动它时,我看到了错误
/home/negative/Dev/JS/Bot/src/structures/Client.ts:10
commands = new discord_js_1.Collection();
^
SyntaxError: Unexpected token =
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Module.m._compile (/home/negative/Dev/JS
...
这是我的代码 (Client.ts)
import {
ApplicationCommandDataResolvable,
Client,
ClientEvents,
Collection
} from "discord.js";
import { CommandType } from "../typings/Command";
import glob from "glob";
import { promisify } from "util";
import { RegisterCommandsOptions } from "../typings/client";
import { Event } from "./Event";
const globPromise = promisify(glob);
export class ExtendedClient extends Client {
commands: Collection<string, CommandType> = new Collection();
constructor() {
super({ intents: 32767 });
}
start() {
this.registerModules();
this.login(process.env.botToken);
}
async importFile(filePath: string) {
return (await import(filePath))?.default;
}
async registerCommands({ commands, guildId }: RegisterCommandsOptions) {
if (guildId) {
this.guilds.cache.get(guildId)?.commands.set(commands);
console.log(`Registering commands to ${guildId}`);
} else {
this.application?.commands.set(commands);
console.log("Registering global commands");
}
}
async registerModules() {
// Commands
const slashCommands: ApplicationCommandDataResolvable[] = [];
const commandFiles = await globPromise(
`${__dirname}/../commands/*/*{.ts,.js}`
);
commandFiles.forEach(async (filePath) => {
const command: CommandType = await this.importFile(filePath);
if (!command.name) return;
console.log(command);
this.commands.set(command.name, command);
slashCommands.push(command);
});
this.on("ready", () => {
this.registerCommands({
commands: slashCommands,
guildId: process.env.guildId
});
});
// Event
const eventFiles = await globPromise(
`${__dirname}/../events/*{.ts,.js}`
);
eventFiles.forEach(async (filePath) => {
const event: Event<keyof ClientEvents> = await this.importFile(
filePath
);
this.on(event.event, event.run);
});
}
}
Visual Studio 代码说我的代码没有错误,但我无法修复这个。有人可以告诉我如何解决吗?我一直在研究这个问题...... 如果你知道如何解决这个问题,请告诉我,我会很高兴的。
此错误是因为您的目标 JavaScript 版本不受您安装的 Node.js 版本的支持。您可以更改目标 JavaScript 版本,或更新 Node.js 版本。
选项 1:更新 Node.js 版本
确保您至少安装了 Node.js 版本 16.11.0
(根据 node.green this is the first version to fully support instance class fields)。
您可以通过在终端中输入 node -v
来检查您的节点版本。
$ node -v
v17.1.0
然后您可以从他们的网站安装最新的 Node.js 版本:https://nodejs.org/
选项 2:更改目标 JavaScript 版本
打开您的 tsconfig.json
文件并将 target field 更改为您当前 Node.js 版本支持的版本。
您可以通过在终端中输入 node -v
来检查您的节点版本。
$ node -v
v17.1.0
请参考 node.green 以根据您当前的 Node.js 版本确定您可以定位的 JavaScript 版本。