Error when I make a script to keep my Node.JS app alive for more than 5 minutes


#1

I have a discord bot, file is called app.js. Here is the code for it: `
const Discord = require(“discord.js”);
const API = “https://github.com/hydrabolt/discord.js/”;

const client = new Discord.Client();

// Here we load the config.json file that contains our token and our prefix values.
const config = require("./config.json");
// config.token contains the bot’s token
// config.prefix contains the message prefix.

client.on(“ready”, () => {
// This event will run if the bot starts, and logs in, successfully.
console.log(Bot has started, with ${client.users.size})
// Example of changing the bot’s playing game to something useful. client.user is what the
// docs refer to as the “ClientUser”.
client.user.setActivity(Serving ${client.guilds.size} servers);
});

client.on(“guildCreate”, guild => {
// This event triggers when the bot joins a guild.
console.log(New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!);
client.user.setActivity(Serving ${client.guilds.size} servers);
});

client.on(“disconnected”, function () {

console.log("OOF!");
process.exit(1); //exit node.js with an error

});

client.on(“message”, async message => {
const swearWords = []

if( swearWords.some(word => message.content.includes(word)) ) {
message.delete();
message.reply(“Stop that, this is a christian server!”);
// Or just do message.delete();
}

// This event will run on every single message received, from any channel or DM.

// It’s good practice to ignore other bots. This also makes your bot ignore itself
// and not get into a spam loop (we call that “botception”).
if(message.author.bot) return;

// Also good practice to ignore any message that does not start with our prefix,
// which is set in the configuration file.
if(message.content.indexOf(config.prefix) !== 0) return;

// Here we separate our “command” name, and our “arguments” for the command.
// e.g. if we have the message “+say Is this the real life?” , we’ll get the following:
// command = say
// args = [“Is”, “this”, “the”, “real”, “life?”]
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();

// Let’s go with a few common example commands! Feel free to delete or change those.

if(command === “ping”) {
// Calculates ping between sending a message and editing it, giving a nice round-trip latency.
// The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
const m = await message.channel.send(“Ping?”);
m.edit(Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms);
}
if (command === “asl”) {
let [age, sex, location] = args;
message.reply(Hello ${message.author.username}, I see you're a ${age} year old ${sex} from ${location}. Wanna date?);
}
if (command === “trend”) {
let [query, location] = args;
message.reply(https://trends.google.com/trends/explore?q=${query}&geo=US);
}

if(command === “want”) {
// Calculates ping between sending a message and editing it, giving a nice round-trip latency.
// The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
const m = await message.channel.send(“I want you to want me”);

}
if(command === “oof”) {
if(!message.member.roles.some(r=>[“Administrator”, “Mod”, “Admin”, “Moderator”, “The Team”].includes(r.name)) ){
return message.reply(“Sorry, you don’t have permissions to use this!”);
}
// Calculates ping between sending a message and editing it, giving a nice round-trip latency.
// The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
const m = await message.channel.send(“oof”);
m.edit(OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOF);
}

if(command === “allahu”) {
// Calculates ping between sending a message and editing it, giving a nice round-trip latency.
// The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
const m = await message.channel.send(“Allahu Akbar!!!”);
m.edit(Allahu Akbar! https://www.youtube.com/watch?v=qabGYz1LoH4);
}

var memes = [https://www.youtube.com/watch?v=nkCfws0jtVA, https://www.youtube.com/watch?v=kbsJVkThWbU, https://www.youtube.com/watch?v=CcnzvIM5XX0, https://www.youtube.com/watch?v=ARTzw4VEenU, https://www.youtube.com/watch?v=bfCR0dEDO1A, https://www.youtube.com/watch?v=ssIY8NYwvh4]
function rand() {
var rm = memes[Math.floor(Math.random() * memes.length)];
return rm;
}
if(command === “meme”) {
message.reply(rand());
}

if(command === “say”) {
// makes the bot say something and delete the message. As an example, it’s open to anyone to use.
// To get the “message” itself we join the args back into a string with spaces:
const sayMessage = args.join(" ");
// Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
message.delete().catch(O_o=>{});
// And we get the bot to say the thing:
message.channel.send(sayMessage);
}

if(command === “kick”) {
// This command must be limited to mods and admins. In this example we just hardcode the role names.
// Please read on Array.some() to understand this bit:
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some?
if(!message.member.roles.some(r=>[“Administrator”, “Moderator”, “RED”, “Bots”].includes(r.name)) )
return message.reply(“Sorry, you don’t have permissions to use this!”);

// Let's first check if we have a member and if we can kick them!
// message.mentions.members is a collection of people that have been mentioned, as GuildMembers.
// We can also support getting the member by ID, which would be args[0]
let member = message.mentions.members.first() || message.guild.members.get(args[0]);
if(!member)
  return message.reply("Please mention a valid member of this server");
if(!member.kickable) 
  return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");

// slice(1) removes the first part, which here should be the user mention or ID
// join(' ') takes all the various parts to make it a single string.
let reason = args.slice(1).join(' ');
if(!reason) reason = "No reason provided";

// Now, time for a swift kick in the nuts!
await member.kick(reason)
  .catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`);

}

if(command === “ban”) {
// Most of this command is identical to kick, except that here we’ll only let admins do it.
// In the real world mods could ban too, but this is just an example, right? :wink:
if(!message.member.roles.some(r=>[“Administrator”].includes(r.name)) )
return message.reply(“Sorry, you don’t have permissions to use this!”);

let member = message.mentions.members.first();
if(!member)
  return message.reply("Please mention a valid member of this server");
if(!member.bannable) 
  return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");

let reason = args.slice(1).join(' ');
if(!reason) reason = "No reason provided";

await member.ban(reason)
  .catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);

}

if(command === “purge”) {
// This command removes all messages from all users in the channel, up to 100.
if(!message.member.roles.some(r=>[“Administrator”, “Moderator”, “RED”, “Bots”].includes(r.name)) )
return message.reply(“Sorry, you don’t have permissions to use this!”);
// get the delete count, as an actual number.
const deleteCount = parseInt(args[0], 10);
// Ooooh nice, combined conditions. <3
if(!deleteCount || deleteCount < 2 || deleteCount > 100)
return message.reply(“Please provide a number between 2 and 100 for the number of messages to delete”);

// So we get our messages, and delete them. Simple enough, right?

}
});

client.login(config.token);`

when I go to the console and run $ node app.js, it works fine. Then I added this script in to the beginning of it:
var express = require('express'); var app = express(); const http = require('http'); setInterval(() => { http.get(http://{process.env.PROJECT_DOMAIN}.glitch.me/`); }, 300000)` I ran ` node app.jslike normal, but it gave me this strange error:events.js:183
throw er; // Unhandled ‘error’ event
^
Error: listen EADDRINUSE :::3000
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at Server.setupListenHandle [as _listen2] (net.js:1355:14)
at listenInCluster (net.js:1396:12)
at Server.listen (net.js:1480:7)
at Function.listen (/rbd/pnpm-volume/e017e5a2-d6d8-4a35-a8d8-b9bc8062f1d1/node_modules/.registry.npmjs.org/express/4.16.3/node_modules/express/lib/application.js:618:24)
at Object. (/app/app.js:6:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions…js (module.js:663:10)
at Module.load (module.js:565:32)`

what should I do?


#2

Already exists an app listening on port 3000, try running killall node in the console and see if it works.


#3

We recommend running your project code by using the start command in your package.json file. That way, when you make code changes and your app restarts only one instance will be run. See https://glitch.com/help/failstart/