process.env.token 不支持 discord 机器人启动

process.env.token not working on discord bot startup

我正在尝试使用 Heroku 和 Github 让我的 discord 机器人 24/7 全天候在线。但是每当我尝试 运行 我的代码时,我都会收到此错误。

2022-01-01T03:56:43.950025+00:00 app[Worker.1]: /app/node_modules/discord.js/src/rest/APIRequest.js:33
2022-01-01T03:56:43.950043+00:00 app[Worker.1]:     agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true });
2022-01-01T03:56:43.950043+00:00 app[Worker.1]:           ^^^
2022-01-01T03:56:43.950044+00:00 app[Worker.1]: 
2022-01-01T03:56:43.950044+00:00 app[Worker.1]: SyntaxError: Unexpected token '??='

我有两个放置令牌的位置,我在这些位置使用 process.env.token。 第一个是我的 index.js 文件

(async () => {
    for (file of functions) {
        require(`./src/functions/${file}`)(client);
    }
    client.handleEvents(eventFiles, ".src/events");
    client.handleCommands(commandFolders, "./src/commands");
    client.login(process.env.token);
})();

第二个是我的 handlecommands.js 文件

module.exports = (client) => {
    client.handleCommands = async(commandFolders, path) => {
        client.commandArray = [];
        for (folder of commandFolders) {
            const commandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));

            for (const file of commandFiles) {
                const command = require(`../commands/${folder}/${file}`);
                // Set a new item in the Collection
                // With the key as the command name and the value as the exported module
                client.commands.set(command.data.name, command);
                client.commandArray.push(command.data.toJSON());
            }
        }
        const rest = new REST({
             version: '9' 
        }).setToken(process.env.token);

我该如何解决这个问题? (快速免责声明,我没有 .env 文件,但我有一个 procfile 告诉 Heroku 做什么) Worker: node index.js

您的 Heroku 应用似乎使用了 Node.js 的过时/旧版本,其中 ??= 不可用。由于您无法直接更新 Heroku 应用程序的 Node.js 版本,因此您可以在 package.json.

中设置您需要的版本

只需在之后添加以下行,例如"description",对象:

"engines": {
    "node": "16.x" (or another version, depending on which one you need)
}