Skip to content

Commit

Permalink
Bot cluster, restart on crash
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonhardTissen committed May 1, 2023
1 parent 92b00cc commit ed64a11
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 38 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -22,7 +18,7 @@ npm install canvas
"color": "23F843"
}
```
### Run the bot
### Run the bot master
```bash
node bot.js
```
Expand Down
44 changes: 12 additions & 32 deletions bot.js
Original file line number Diff line number Diff line change
@@ -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);
if (cluster.isWorker) {
startBot();
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"dependencies": {
"canvas": "^2.11.2",
"cluster": "^0.7.7",
"discord.js": "^14.9.0",
"sqlite3": "^5.1.6"
}
}
}
38 changes: 38 additions & 0 deletions start.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit ed64a11

Please sign in to comment.