【问题标题】:process.env.token not working on discord bot startup [duplicate]process.env.token 在不和谐机器人启动时不起作用[重复]
【发布时间】:2022-01-02 04:02:34
【问题描述】:

我正在尝试使用 Heroku 和 Github 让我的不和谐机器人 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

【问题讨论】:

    标签: github heroku discord.js


    【解决方案1】:

    似乎您的 Heroku 应用正在使用过时/旧版本的 Node.js,其中 ??= 不可用。由于您无法直接更新 Heroku 应用的 Node.js 版本,您可以在 package.json 中设置您需要的版本。

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

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

    【讨论】:

    • 非常感谢!不知道这就是我所要做的:D
    • 很高兴我能帮上忙 ^^
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2018-02-25
    • 2021-09-12
    • 2020-11-18
    • 1970-01-01
    相关资源
    最近更新 更多