diff --git a/.env.example b/.env.example index 59891e2..584c803 100644 --- a/.env.example +++ b/.env.example @@ -30,6 +30,9 @@ OUTPUT= # Put temporary image dir here (make sure it's accessible via a web server), leave blank to disable TEMPDIR= +# Set this to true if you're using PM2 to manage the bot +PMTWO=false + # Enable/disable Twitter bot (true/false) TWITTER=false # Put Twitter username here diff --git a/app.js b/app.js index a51e9e7..2e0eece 100644 --- a/app.js +++ b/app.js @@ -44,7 +44,21 @@ async function init() { if (process.env.NODE_ENV === "production") { require("./utils/dbl.js"); } + + // handle ctrl+c and pm2 stop + process.on("SIGINT", () => { + logger.log("info", "SIGINT detected, shutting down..."); + client.editStatus("dnd", { + name: "Restarting/shutting down..." + }); + for (const command of commands) { + handler.unload(command); + } + client.disconnect(); + require("./utils/database.js").end(); + process.exit(0); + }); } // launch the bot -init(); +init(); \ No newline at end of file diff --git a/commands/leak.js b/commands/leak.js index 828b90f..2d81348 100644 --- a/commands/leak.js +++ b/commands/leak.js @@ -6,7 +6,6 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make a Super Smash Bros. leak meme!`; const buffer = await promisify(magick.leak)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); - //const buffer = await gm(template).out("-background").out("white").out("-gravity").out("Center").out("(").out("-clone").out("0").out("(").out(image.path).out("-virtual-pixel").out("white").out("-resize").out("640x360!").rotate("white", 15).out(")").out("-geometry").out("+450-200").out("-composite").out(")").out("+swap").out("-composite").out("-alpha").out("remove").out("-alpha").out("off").bufferPromise(image.type, image.delay); return { file: buffer, name: `leak.${image.type}` diff --git a/events/ready.js b/events/ready.js index b850284..07ddc2b 100644 --- a/events/ready.js +++ b/events/ready.js @@ -12,6 +12,7 @@ const helpGenerator = process.env.OUTPUT !== "" ? require("../utils/help.js") : null; const twitter = process.env.TWITTER === "true" ? require("../utils/twitter.js") : null; +const first = process.env.PMTWO === "true" ? process.env.NODE_APP_INSTANCE === "0" : true; let run = false; // run when ready @@ -65,7 +66,7 @@ module.exports = async () => { } } - if (!run) { + if (!run && first) { const job = new cron.CronJob("0 0 * * 0", async () => { logger.log("Deleting stale guild entries in database..."); const guildDB = await database.query("SELECT * FROM guilds"); @@ -100,7 +101,7 @@ module.exports = async () => { } // generate docs - if (helpGenerator) await helpGenerator(process.env.OUTPUT); + if (helpGenerator && first) await helpGenerator(process.env.OUTPUT); // set activity (a.k.a. the gamer code) (async function activityChanger() { @@ -111,7 +112,7 @@ module.exports = async () => { })(); // tweet stuff - if (twitter !== null && twitter.active === false) { + if (twitter !== null && twitter.active === false && first) { const blocks = await twitter.client.blocks.ids(); const tweet = async () => { const tweetContent = await misc.getTweet(twitter.tweets); @@ -158,6 +159,7 @@ module.exports = async () => { } } + if (process.env.PMTWO === "true") process.send("ready"); logger.log(`Successfully started ${client.user.username}#${client.user.discriminator} with ${client.users.size} users in ${client.guilds.size} servers.`); run = true; };