https://github.com/lacunahub/letsfrag
Scale your Discord bot across multiple machines.
https://github.com/lacunahub/letsfrag
bot discord discord-js shard sharding
Last synced: 7 months ago
JSON representation
Scale your Discord bot across multiple machines.
- Host: GitHub
- URL: https://github.com/lacunahub/letsfrag
- Owner: LacunaHub
- License: mit
- Created: 2024-03-12T03:41:05.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-09-17T10:48:00.000Z (about 1 year ago)
- Last Synced: 2024-09-18T02:58:22.493Z (about 1 year ago)
- Topics: bot, discord, discord-js, shard, sharding
- Language: TypeScript
- Homepage: https://lacunahub.github.io/letsfrag/
- Size: 488 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# About
This package is inspired by [discord-hybrid-sharding](https://github.com/meister03/discord-hybrid-sharding) and allows you to scale your Discord bot across multiple hosts/machines.
# Links
- [Documentation](https://lacunahub.github.io/letsfrag)
# Installation
In the same directory as your `package.json` file, create or edit an `.npmrc` file:
```
@lacunahub:registry=https://npm.pkg.github.com/
```Then run:
```bash
npm install @lacunahub/letsfrag
```_Developed and tested on Node.js v18_
# Usage
## File structure
```
├── src
│ ├── client.ts
│ ├── cluster.ts
│ └── server.ts
└── package.json
````src/server.ts`
```ts
import { Server } from '@lacunahub/letsfrag'const server = new Server({
port: 5565,
authorization: 'authorization_token',
hostCount: 2,
shardCount: 10,
botToken: 'bot_token'
})server.on('connect', client => console.info(`Client "${client.id}" connected`))
server.on('disconnect', client => console.warn(`Client "${client.id}" disconnected`))
server.on('error', err => console.error(err))
server.on('ready', url => console.info(`Server is ready on url ${url}`))server.initialize()
````src/cluster.ts`
```ts
import { ClusterManager } from '@lacunahub/letsfrag'const clusterManager = new ClusterManager(`${__dirname}/Client.js`, {
server: {
host: 'localhost',
port: 5565,
authorization: 'authorization_token',
type: 'bot',
reconnect: true,
retries: 10
},
mode: 'fork',
spawnDelay: 10_000
})clusterManager.on('clusterCreate', cluster => console.info(`Cluster #${cluster.id} has been created`))
clusterManager.on('ready', manager => console.info(`Manager with clusters (${manager.clusters}) is ready`))clusterManager.spawn()
````src/client.ts`
```ts
import { ClusterShardClient } from '@lacunahub/letsfrag'
import { GatewayIntentBits } from 'discord.js'const client = new ClusterShardClient({
intents: [GatewayIntentBits.Guilds]
})client.login()
```_**Note**: The server must be started before `ClusterManager` is initialized._
### Rate limit sync
You can enable rate limit sync between shards/hosts by setting the `store` option in the `rest` object:
`src/client.ts`
```ts
import { ClusterShardClient } from '@lacunahub/letsfrag'
import { GatewayIntentBits } from 'discord.js'const client = new ClusterShardClient({
intents: [GatewayIntentBits.Guilds],
rest: {
store: 'redis://user:pass@127.0.0.1:6379'
}
})client.login()
```See [Keyv](https://www.npmjs.com/package/keyv?activeTab=readme#usage) for more details.