From ed64a11791397faa30e9045b4081cde3e6680631 Mon Sep 17 00:00:00 2001 From: Warze Date: Mon, 1 May 2023 12:21:35 +0200 Subject: [PATCH] Bot cluster, restart on crash --- README.md | 6 +----- bot.js | 44 ++++++++++++-------------------------------- package.json | 4 +++- start.js | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 start.js diff --git a/README.md b/README.md index f67fddd..73c3bb0 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,6 @@ You can try the bot over on my Discord server: https://discord.gg/jtcqgvkZY7 ```bash npm install ``` -### You may also need to install canvas -```bash -npm install canvas -``` ### Enter your bot's token and discord user ID in "/settings.json" (You can rename "/sample_settings.json): ```json { @@ -22,7 +18,7 @@ npm install canvas "color": "23F843" } ``` -### Run the bot +### Run the bot master ```bash node bot.js ``` diff --git a/bot.js b/bot.js index 88a2b4e..599648f 100644 --- a/bot.js +++ b/bot.js @@ -1,35 +1,15 @@ -const { commands } = require('./commands'); -const { createClient } = require('./utils/client'); -const { getPrefix } = require('./utils/getprefix'); -const settings = require('./settings.json'); +const cluster = require('cluster'); +const { startBot } = require('./start'); +if (cluster.isMaster) { + cluster.fork(); -const client = createClient(); + cluster.on('exit', function(worker, code, signal) { + console.log("Bot restarting!") -// Event handler for when a message is received -client.on('messageCreate', (message) => { + cluster.fork(); + }); +} - // Ignore messages from bots - if (message.author.bot) return; - - // Ignore messages outside of the designated channel - if (message.channel.id != settings.channel) return; - - // Get the preferred prefix of the user, but default to the bot prefix - getPrefix(message.author.id).then((preferred_prefix) => { - - // Only act on commands starting with the prefix - if (!message.content.startsWith(preferred_prefix)) return; - - // Convert message to lowercase array and go through all the registered commands - const args = message.content.substring(preferred_prefix.length).toLowerCase().split(' '); - commands.forEach((command) => { - // If the message matches any of the aliases, execute the corresponding function - if (command.aliases.includes(args[0])) { - command.func(message, args[1], args[2], args[3]) - } - }) - }) -}); - -// Log in the bot using the token -client.login(settings.token); \ No newline at end of file +if (cluster.isWorker) { + startBot(); +} \ No newline at end of file diff --git a/package.json b/package.json index b4dcb46..208bbdc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,8 @@ { "dependencies": { + "canvas": "^2.11.2", + "cluster": "^0.7.7", "discord.js": "^14.9.0", "sqlite3": "^5.1.6" } -} \ No newline at end of file +} diff --git a/start.js b/start.js new file mode 100644 index 0000000..baa8f78 --- /dev/null +++ b/start.js @@ -0,0 +1,38 @@ +const { commands } = require('./commands'); +const { createClient } = require('./utils/client'); +const { getPrefix } = require('./utils/getprefix'); +const settings = require('./settings.json'); + +function startBot() { + const client = createClient(); + + // Event handler for when a message is received + client.on('messageCreate', (message) => { + + // Ignore messages from bots + if (message.author.bot) return; + + // Ignore messages outside of the designated channel + if (message.channel.id != settings.channel) return; + + // Get the preferred prefix of the user, but default to the bot prefix + getPrefix(message.author.id).then((preferred_prefix) => { + + // Only act on commands starting with the prefix + if (!message.content.startsWith(preferred_prefix)) return; + + // Convert message to lowercase array and go through all the registered commands + const args = message.content.substring(preferred_prefix.length).toLowerCase().split(' '); + commands.forEach((command) => { + // If the message matches any of the aliases, execute the corresponding function + if (command.aliases.includes(args[0])) { + command.func(message, args[1], args[2], args[3]) + } + }) + }) + }); + + // Log in the bot using the token + client.login(settings.token); +} +exports.startBot = startBot; \ No newline at end of file