Search code examples
discorddiscord.js

My bot has been offline with no error, it was working yesterday


I get an error, which is below, this worked great yesterday, it's just not working today.

const express = require("express");
const app = express();

    app.listen(3000, () => {
      console.log("Project is running!");
    })
    
    app.get("/", (req, res) => {
      res.send("Hello World!");
    })
    
    const Discord = require("discord.js")
    const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
    
    client.on("messageCreate", message => {
      if(message.content === "jobs") {
        message.channel.send("fortnite battle royale");
      }
    })
    
    client.on("messageCreate", message => {
      if(message.content === "ping") {
        message.channel.send("pong, you idiot, you should know the rest");
      }
    })
    
    client.on("messageCreate", message => {
      if(message.content === "pls dont help") {
        message.channel.send(message.author.toString() + " " +"ok, i wont, all you had to do was ask... also dank memer stole this command");
      }
    })
    
    
    client.on("messageCreate", message => {
       if (message.author.bot) return;
      if(message.content.includes("dream")) {
       var msgnumber= (Math.floor((Math.random() * 2) + 1));
        console.log(msgnumber);
        if (msgnumber===1) {
             message.channel.send("did someone say dream!?");
        } else if (msgnumber===2) {
             message.channel.send("why we talkin' about dream... huh!?")
        }
      }
    })
    
    client.on('messageCreate', message => {
      if (message.content === '!ping') {  
        message.channel.send(`🏓Latency is ${Date.now() - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
      }
    });
    
    client.on("messageCreate", message => {
      if(message.content === "username") {
        message.channel.send(message.author.username);
      }
    })
    
    client.on('ready', () => {
      console.log('Bot is online!')
      client.user.setActivity('not being entertained', {type : 'PLAYING'} )
    })
    
    client.login(process.env.token);

This was working great yesterday, earlier it was online yet not responding to commands.

I am hosting this in Replit. Am I doing something wrong? I do not get a message stating my bot is online, as I was yesterday when it was working.

Update: I am now getting an error, which in my opinion is a good sign.

    node:events:504
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1330:16)
    at listenInCluster (node:net:1378:12)
    at Server.listen (node:net:1465:7)
    at Function.listen (/home/runner/DiscordBot/node_modules/express/lib/application.js:635:24)
    at Object.<anonymous> (/home/runner/DiscordBot/index.js:4:5)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1357:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 3000
}
exit status 1

Solution

  • You are hosting on Replit, a service that runs hundreds of servers, often with several users' projects running on the same machine (especially for free users). You cannot simply choose an arbitrary port and expect it to be free. Any other user on Replit that happens to be on the same machine as you could have already claimed the port 3000, if that port is even available to Replit's users at all. There may not have been a user running on port 3000 on the same Replit server as you earlier, but now there is. This is why you are getting this error. Port 3000 is also a very popular port, so it is not a good idea to try and use that as your port on Replit.

    Luckily, mass-hosting services like Replit account for this by providing you with an environment variable that contains an open port designated to your project. You can access it using process.env.PORT, like so:

    app.listen(process.env.PORT, () => {
        console.log("Project is running!");
    })
    

    This will guarantee you avoid the port already-in-use error you are getting. This also avoids the mess involved with trying to close ports (which you do not really have control over on Replit anyways), making your other question unnecessary.