I updated a resolution constant for my discord bot earlier for some web screenshoter using the nodejs module webshot
. Then memory usage skyrocketed to a peak of 2gb
and the glitch project started a series of restarts because of memory.
So then I completed commented out the web screenshoting part of my bot and it is still running out of memory every time it starts. I can’t post all of my code but I can post snippets of it.
// Package.json
{
"name": "Ascension",
"version": "0.0.2",
"description": "An amazing bot",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.17.1",
"discord.js": "^11.5.2",
"dotenv": "^8.2.0",
"ytdl-core": "^2.0.0",
"subtitles-parser": "^0.0.2",
"node-opus": "^0.3.2",
"yt-xml2srt": "^1.1.0",
"phantomjs-prebuilt": "^2.1.1",
"mathjs": "^6.6.1",
"sodium": "^3.0.2",
"bufferutil": "^4.0.1",
"zlib-sync": "^0.1.6",
"erlpack": "^0.1.3",
"opusscript": "^0.0.7"
},
"engines": {
"node": "8.x"
},
"repository": {
"url": "https://glitch.com/edit/#!/template-discord-bot"
},
"license": "MIT",
"keywords": [
"node",
"glitch",
"express"
]
}
Yes, I used a template from somewhere
// Server.js part 1
console.log("Init!");
// Reset
// Create some constant values...
const discord = require("discord.js");
const bot = new discord.Client();
const dotenv = require("dotenv");
const fs = require("fs");
const ytdl = require("ytdl-core");
const path = require("path");
const https = require("https");
const parser = require("subtitles-parser");
const { evaluate,parse} = require('mathjs')
const {createEmbed} = require("./embeds")
const mathQuestion = require("./math");
console.log("Loaded");
var difficulty = "super-easy";
var curMSG; // Restart required
const storeData = (data, path) => {
try {
fs.writeFileSync(path, JSON.stringify(data));
} catch (err) {
console.error(err);
}
};
const loadData = path => {
try {
return fs.readFileSync(path, "utf8");
} catch (err) {
console.error(err);
return false;
}
};
Part 2:
// Server.js part 2
// Load from the .env file
console.log("ENV Set");
dotenv.config();
console.log("Loading db");
var db = loadData("database.json");
var keys = Object.keys(db);
if (!("test" in keys)) {
console.log("Creating test");
db["test"] = {};
}
// Grab the bot token from the .env file
console.log("Got token");
const botToken = process.env.BOT_TOKEN;
Lots of event handler and bot code after that
Part 3:
// server.js part 3
// End of bot.on('message')
// CLEANUP
console.log("Cleanup handlers");
process.stdin.resume(); //so the program will not close instantly
function exitHandler(options, exitCode) {
// Cleanup here
console.log("Cleaning Up");
storeData(db, "database.json");
if (options.cleanup) console.log("clean");
if (exitCode || exitCode === 0) console.log(exitCode);
if (options.exit) process.exit();
}
//do something when app is closing
process.on("exit", exitHandler.bind(null, { cleanup: true }));
//catches ctrl+c event
process.on("SIGINT", exitHandler.bind(null, { exit: true }));
// catches "kill pid" (for example: nodemon restart)
process.on("SIGUSR1", exitHandler.bind(null, { exit: true }));
process.on("SIGUSR2", exitHandler.bind(null, { exit: true }));
//catches uncaught exceptions
process.on("uncaughtException", exitHandler.bind(null, { exit: true }));
// Since there is no actual preview for the bot (unless you add your own website code)
// It will be an error or continious refresh loop, consider this part of the code that keeps the bot alive
// Though you will still need an uptime robot in order to truely keep the bot alive
// This is NOT my code, the code is directly from this page: https://anidiotsguide_old.gitbooks.io/discord-js-bot-guide/content/other-guides/hosting-on-glitchcom.html
console.log("Start self server");
const http = require("http");
const express = require("express");
const app = express();
app.get("/", (request, response) => {
console.log(Date.now() + " Ping Received");
response.sendStatus(200);
});
app.listen(process.env.PORT);
app.use(express.static("."));
setInterval(() => {
http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 280000);
// Bot token
// Located in .env file
bot.login(botToken);