简体   繁体   中英

Deploying Discord Bot to Heroku and Github

I'm having a trouble about deploying my own Discord bot to heroku. When I deploying it my bot still offline. I just came back about coding bots and deploying to heroku sorry!

In my bot.js this is what i put:

require("dotenv").config();

let ver = process.env.NODE_ENV;

client.once("ready", async () => {
  if (ver === "production") {
    client.user.setActivity(`in code land`, { type: "PLAYING" });
  } else {
    client.user.setActivity(`over ${client.guilds.cache.size} server(s)`, {
      type: "WATCHING",
      status: "IDLE",
    });
  }
  console.log(`Logged in as ${client.user.tag}!`);
  console.log(`the prefix is ` + prefix);
});

and the bottom part of bot.js is:

client.login(process.env.NODE_ENV);

in.env this is my code:

NODE_ENV = TOKEN

in package.json this is what I put in scripts:

"scripts": {
    "start": "node .",
    "test": "echo \"Error: no test specified\" && exit 1",
    "production": "NODE_ENV=production&&npm start",
    "development": "set NODE_ENV=development&&npm start"
}

I made file named Procfile then put this code: Note: I changed the code in Procfile npm start to node bot.js , bot still offline

workers: node bot.js

I followed the instruction in heroku and github using git but I really don't understand why It's not working. If I need to edit my post to send picture please tell me. Thanks!

Update: Dec 22, 2021: I looked at Heroku Application Logs, Here's what it said:

SyntaxError: Unexpected token '??='

Maybe this is the reason why my bot still offline?

The error SyntaxError: Unexpected token '??=' is to do with the default node version for heroku and the version deiscord.js runs on.

Heroku uses node 14 and discord.js uses node 16, where the syntax for ?? was introduced in node 15.
There is however a simple fix for this solution.

In your package.json file add:

"engines": {
   "node": "16.7"
},

Heroku will now know to use node 16.7 and so it will have the syntax for ?? available as well as work correctly for discord.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM