Skip to content

Commit

Permalink
overhaul sharding to use discord hybrid sharding. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
200percentmicky committed May 14, 2024
1 parent 7dfb4f3 commit 3c900ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
23 changes: 16 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
/* Index File */

require('dotenv').config();
const { ShardingManager } = require('discord.js');
const ChadMusic = require('./src/bot.js');
const { ClusterManager } = require('discord-hybrid-sharding');
const logger = require('./src/modules/ChadLogger.js');

if (process.versions.node.split('.')[0] < 18) {
Expand All @@ -43,17 +42,27 @@ if (process.env.YOUTUBE_COOKIE) {
}

if (process.env.SHARDING) {
const manager = new ShardingManager('./src/bot.js', {
token: process.env.TOKEN,
totalShards: parseInt(process.env.SHARDS) ?? 'auto'
logger.info('Starting client with sharding enabled.');

const manager = new ClusterManager('./src/bot.js', {
totalShards: parseInt(process.env.SHARDS) ?? 'auto',
shardsPerClusters: parseInt(process.env.SHARDS_PER_CLUSTER) ?? 2,
mode: 'process'
});

manager.on('shardCreate', s => logger.info(`Shard ${s.id} launched.`));
if (manager.totalShards === 'auto') {
manager.token = process.env.TOKEN;
}

manager.on('clusterCreate', c => logger.info(`Cluster ${c.id} launched.`))
.on('clusterReady', c => logger.info(`Cluster ${c.id} is ready.`));

manager.spawn();
manager.spawn({ timeout: -1 });
} else {
logger.info('Starting client with sharding disabled.');

const ChadMusic = require('./src/bot.js');

try {
new ChadMusic().login(process.env.TOKEN);
} catch (err) {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"colon-notation": "^1.2.1",
"common-tags": "^1.8.0",
"discord-akairo": "github:200percentmicky/discord-akairo",
"discord-hybrid-sharding": "^2.2.0",
"discord.js": "^14.14.1",
"distube": "github:200percentmicky/chadtube",
"dotenv": "^8.6.0",
Expand Down
8 changes: 7 additions & 1 deletion src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const ChadUtils = require('./modules/ChadUtils');
const path = require('path');
const fs = require('fs');
const { version } = require('../package.json');
const { getInfo, ClusterClient } = require('discord-hybrid-sharding');

// Let's boogie!
class ChadMusic extends AkairoClient {
Expand All @@ -51,9 +52,14 @@ class ChadMusic extends AkairoClient {
Partials.Channel,
Partials.Message,
Partials.User
]
],
shards: process.env.SHARDING ? getInfo().SHARD_LIST : [0],
shardCount: process.env.SHARDING ? getInfo().TOTAL_SHARDS : 1
});

// Hybrid sharding
this.cluster = process.env.SHARDING ? new ClusterClient(this) : undefined;

// Calling packages that can be used throughout the client.
this.logger = logger;
this.ui = ChadUI;
Expand Down

0 comments on commit 3c900ef

Please sign in to comment.