-
Notifications
You must be signed in to change notification settings - Fork 3
Cluster
A Cluster is a core component of the Galactic framework that manages a subset of Discord shards for your bot. Clustering allows you to distribute the load of your bot across multiple processes or machines, improving performance and reliability.
To create a cluster, use the Cluster.initial method. This method sets up the cluster with the appropriate shard assignments and configurations.
import {Cluster} from "galactic.ts";
import {Client, ClientOptions} from "discord.js";
// Extend the Discord Client to include a reference to its Cluster
export class ExtendedClient extends Client {
cluster: Cluster<ExtendedClient>;
constructor(options: ClientOptions, cluster: Cluster<ExtendedClient>) {
super(options);
this.cluster = cluster;
}
}
// Initialize the Cluster
const cluster = Cluster.initial<ExtendedClient>();
const client = new ExtendedClient({
shards: cluster.shardList,
shardCount: cluster.totalShards,
intents: cluster.intents,
}, cluster);
cluster.client = client;
client.login(cluster.token).then(r => {
cluster.triggerReady(client.guilds.cache.size, client.guilds.cache.values().map(g => g.memberCount).reduce((a, b) => a + b, 0));
}).catch(e => {
console.error('Failed to login:', e);
cluster.triggerError(e);
})If the file from the cluster is in the same file as the ManagedInstance (which can happen if the code is started in built state), the code may start additional ManagedInstances. To get around this, code like this will do the trick:
if (process.env.CLUSTER_ID !== undefined) {
// Bot logic here
}Thats it. Your cluster is now set up and ready to manage its assigned shards. You can now implement your bot's logic using the client instance as you normally would with discord.js.
| Name | Signature | Occurance |
|---|---|---|
| 'message' | (message: unknown) => void |
When the cluster receives a message. |
| 'request' | (message: unknown, resolve: (data: unknown) => void, reject: (error: any) => void) => void |
When the cluster receives a request message. |
| CLUSTER_READY | () => void |
When the cluster process reports that it is ready. |
