An efficient package, which syncs ratelimits of your Discord Bot on all Machines/Clusters.

Overview

Discord server

Discord-cross-ratelimit

An efficient package, which syncs ratelimits of your Discord Bot on all Machines/Clusters.

Why?

When Sharding/Scaling your Bot over many processes or even machines, every client has it own ratelimits, which are not synced with shards/clusters or machines.

Bots which encounter a high load, often hit the (global) ratelimit, because the Shards are not informed about existing ratelimits in other Shards.

Hitting less Global Ratelimits is essential, inorder to not get "cooldowned" by the ratelimit or even getting banned on too many requests.

This Package syncs the ratelimits of your Bot over Machines and Clusters with no Breaking Changes, when using Discord.js v13

Note: You have to switch to discord-hybrid-sharding, which just needs some small changes and even allow Clustering and resource saving and has all functions of the ShardingManager.

Features:

  • Syncs ratelimits of your Bot over Machines and Clusters
  • No Breaking Changes for Discord.js
  • Can be used in combination with discord-hybrid-sharding & discord-cross-hosting
  • Fast Hash and Handlers fetching for allowing a fast processing of rest requests

Todo:

  • Add invalidRequestWarningInterval inorder to get informed about 4xx Errors and 10k Invalid Requests, which causes a ban
  • Add restGlobalRateLimit & rejectOnRateLimit

How does it Work?

When doing a rest request on the Discord Api, the package fetches Hashes and Handlers from the Manager, which can be a Bridge (discord-cross-hosting) or a Cluster.Manager (discord-hybrid-sharding)

The Manager saves on which route a request has been done and caches the sublimit, method, route and the remaining requests, which can be done without a ratelimit.

When executing a rest request and a hash/handler exists on the manager, the request will be queued inorder to not hit a ratelimit.

This is very similar to Discord.js Rest Ratelimits handling, the only difference is that it is saved globally instead locally.

Guide

If you need help feel free to join our discord server. We will provied you all help

Download

You can download it from npm:

npm i discord-cross-ratelimit

1. Using the Package just with Discord-Hybrid-Sharding

2. Using the Package with Discord-Cross-Hosting

3. Api Reference

1. Using the Package just with Discord-Hybrid-Sharding:

  • When you are not familiar with the Package, you can take a look at the Package's Readme
  • You need two files, one for the Cluster Manager (cluster.js) and one for the Client (bot.js).

Cluster.js:

const Cluster = require("discord-hybrid-sharding");
let {token} = require("./config.json");
const manager = new Cluster.Manager(`${__dirname}/bot.js`,{
//Check the Package's Readme for more options
                                       totalShards: 8 , 
                                       shardsPerClusters: 2, 
                                       mode: "process" ,  
                                       token: token,
                                    })

//Cache the Hashes and Handlers:
const {RatelimitManager} = require("discord-cross-ratelimit");
new RatelimitManager(manager);

manager.on('clusterCreate', cluster => console.log(`Launched Cluster ${cluster.id}`));
manager.spawn({timeout: -1});

Bot.js:

const Cluster = require("discord-hybrid-sharding");
const Discord = require("discord.js");
const client = new Discord.Client({
   //@ts-ignore
 	shards: Cluster.data.SHARD_LIST,        //  A Array of Shard list, which will get spawned
	shardCount: Cluster.data.TOTAL_SHARDS, // The Number of Total Shards
});
client.cluster = new Cluster.Client(client); 

///Overwrite Request Manager for accounting global ratelimits:
const {RequestManager} = require("discord-cross-ratelimit");
client.rest = new RequestManager(client, client.cluster); //Init the Request Manager

client.login("Your_Token");
  • Now go ahead and start node cluster.js, all ratelimits should now be synced between all Clusters in the Cluster Manager.
  • When you are interested in syncing the ratelimits over all Machines, then scroll down.

2. Using the Package with Discord-Cross-Hosting:

  • When you are not familiar with the Package, you can take a look at the Package's Readme
  • You need three files, one for the Bridge (server.js), the Cluster Manager (cluster.js) and one for the Client (bot.js).

Server.js:

    const {Bridge} = require('discord-cross-hosting');

    const server = new Bridge({ 
        port: 4444, //The Port of the Server | Proxy Connection (Replit) needs Port 443
        authToken: 'Your_auth_token_same_in_cluster.js', 
        totalShards: 40, //The Total Shards of the Bot or 'auto'
        totalMachines: 2, //The Total Machines, where the Clusters will run
        shardsPerCluster: 2, //The amount of Internal Shards, which are in one Cluster
        token: 'Your_Bot_Token',
    });

    //Cache the Handlers and Hashes:
    const {RatelimitManager} = require("discord-cross-ratelimit");
    new RatelimitManager(server);

    server.on('debug', console.log)
    server.start();
    server.on('ready', (url) => console.log('Server is ready' + url));

Cluster.js:

const client = new Client({
    agent: 'bot', 
    host: "localhost", ///Domain without https
    port: 4444, ///Proxy Connection (Replit) needs Port 443
    //handshake: true, When Replit or any other Proxy is used
    authToken: 'theauthtoken',
    rollingRestarts: false, //Enable, when bot should respawn when cluster list changes.
});
client.on('debug', console.log)
client.connect();

const Cluster = require("discord-hybrid-sharding");
const manager = new Cluster.Manager(`${__dirname}/bot.js`,{totalShards: 1 ,totalClusters: 'auto'}) //Some dummy Data
manager.on('clusterCreate', cluster => console.log(`Launched Cluster ${cluster.id}`));
manager.on('debug', console.log)

client.listen(manager);
client.requestShardData().then(e => {
    if(!e) return;
    if(!e.shardList) return;
    manager.totalShards = e.totalShards;
    manager.totalClusters = e.shardList.length;
    manager.shardList = e.shardList;
    manager.clusterList = e.clusterList;
    manager.spawn({timeout: -1})
}).catch(e => console.log(e));

Bot.js:

const Cluster = require("discord-hybrid-sharding");
const Discord = require("discord.js");
const client = new Discord.Client({
    intents: ['GUILDS'], //Your Intents
 	shards: Cluster.data.SHARD_LIST,        //  A Array of Shard list, which will get spawned
	shardCount: Cluster.data.TOTAL_SHARDS, // The Number of Total Shards
});

client.cluster = new Cluster.Client(client); 

const {Shard}= require('discord-cross-hosting');
client.machine = new Shard(client.cluster); //Initalize Cluster

/***********************************************************/
///Overwrite Request Manager for accounting global ratelimits:
const {RequestManager} = require("discord-cross-ratelimit");
client.rest = new RequestManager(client, client.machine); //Init the Request Manager
/***********************************************************/
client.on('ready', () => console.log('ready'))

client.login(process.env.token);
  • Start the Bridge at first and the Cluster (on all machines you want) with node Server.js and node Cluster.js
  • All ratelimits should now be synced over all Machines and Clusters.

3. Api Reference:

  • Api Reference is on work, in the meanwhile you can look here or in the typings/code...

new RatelimitManager(manager | bridge, options)

Option Type Default Description
manager class required The Bridge/Cluster Manager to send the Ratelimit Data over the IPC
options object { inactiveTimeout: 240000, requestOffset: 500 } Options, which should be respected, when saving and sending hashes/handers

new RequestManager(client, ClusterClient | MachineShard)

Option Type Default Description
client class required The Discord.js Client, which is required for the http.options
instance class required The ClusterClient/MachineShard for requesting the Hashes and Handlers via the IPC

Bugs, Glitches, Issues and Feature Requests

If you encounter any problems feel free to open an issue in our github repository or join the discord server.

Credits

Credits goes to Azuma, since this has been heavily inspired from it and the discord.js libary (See the Changes.md)

You might also like...

Advanced Music Bot It is an advance type of discord music bot which plays high quality of music with spotify

Advanced Music Bot It is an advance type of discord music bot which plays high quality of music with spotify, apple music support . You can save your songs and play it. It also has DJ mode system.

Dec 25, 2022

A bot that will snipe ETH or BSC pairs when liq added. Fast efficient, simple and free

Buy early gems with custom gas fee, slippage, amount. Auto approve token after buy. Sell buyed token with custom gas fee, slippage, amount. Sell token

Dec 23, 2022

A simple & easy2use API for obtaining information about a discord user, discord bot or discord guild and their use for some purpose on websites!

discord-web-api A simple & easy2use API for obtaining information about a discord user, discord bot or discord guild and their use for some purpose on

Jun 28, 2022

Discord Neura - a Discord bot framework built on discord.js

Discord Neura Description Discord Neura is a Discord bot framework built on discord.js. Features Command Handler, Arguments, Preconditions and Listene

Mar 23, 2022

A discord bot made using discord.js and discord-player

A discord bot made using discord.js and discord-player

Musx A custom discord bot that can play music in your server 🎯 Add the bot to your server If you are looking for a music bot for your Discord server

Mar 28, 2022

This bot is a cool Discord bot made in discord.js using Node.JS

This bot is a cool Discord bot made in discord.js using Node.JS

Anti-Crosspost Discord Bot This bot is a cool Discord bot made in discord.js using Node.JS. It detects when a user cross-posts a message in multiple c

May 31, 2022

New base bot WhatsApp 🈴 bukan self bot lagi atau buka bot yang bisa di pakai oleh bot sendiri 😎

New base bot WhatsApp 🈴 bukan self bot lagi atau buka bot yang bisa di pakai oleh bot sendiri 😎

Installation • Thanks to • Donate Official Group Bot • Settings Instalasi Heroku Buildpack Click the deploy icon below ! heroku/nodejs https://g

Feb 9, 2022

An simple package to create an Activity in Discord Voice Channel using Discord.js

An simple package to create an Activity in Discord Voice Channel using Discord.js

discordjs-activity An simple package to create an Activity in Discord Voice Channel using Discord.js 📂 | Installation npm install discordjs-activity

Nov 15, 2022

A Simple Music Bot Made Using Discord-Player Package - (Beta)

A Simple Music Bot Made Using Discord-Player Package - (Beta)

Our Music Bot A Highly Powerful Music Bot Without Lavalink Which Is Gonna Have Setup, A Specific Text Channel For Bot Command Feature, And DJ System S

Dec 14, 2022
Comments
  •  TypeError: this.initializeShardData is not a function

    TypeError: this.initializeShardData is not a function

    /var/lib/pterodactyl/volumes/6ac16c7b-e59f-4356-8929-88fac63dfbbc/node_modules/discord-cross-hosting/src/Managers/Server.js:121 if (!this.standAlone) this.initializeShardData(); ^

    TypeError: this.initializeShardData is not a function at Timeout._onTimeout (/var/lib/pterodactyl/volumes/6ac16c7b-e59f-4356-8929-88fac63dfbbc/node_modules/discord-cross-hosting/src/Managers/Server.js:121:40) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7)

    opened by FxckingAngel 10
Owner
meister03
Hi, I am a JS Dev, which especially works on efficiency, vertical scaling & crosshosting Check my Public Repos for getting a overview of my projects.
meister03
Co-Pilot is a discord Bot designed to be the all-in-one, open-source Discord bot that handles all your server needs.

Welcome to Co-Pilot Bot repo! ?? ?? Co-Pilot (All-in-one Discord Bot) Co-Pilot is a discord Bot designed to be the all-in-one, open-source Discord bot

YMafalha 19 Nov 11, 2022
Gitlift Discord Bot is a discord bot which is listening Discord Messages to reply with user gitlift profile and total contributions.

Remoklify - Gitlift Discord Bot Gitlift Discord Bot is a discord bot which is listening Discord Messages to reply with user gitlift profile and total

Remoklify 3 Mar 20, 2022
𓄿 CrowBot Remade - It's a multipurpose bot which is a remade of a py discord bot which is selled for 5e but this is in js !

CrowBot Remade - It's a multipurpose bot which is a remade of a py discord bot which is selled for 5e but this is in js ! CrowBot Remade is in french

baby 37 Dec 31, 2022
Discord-Bot - You can use the discord bot codes that are updated in every video of the codes I use in the discord bot making series that I have published on my youtube channel.

Discord-Bot You can use the discord bot codes that are updated in every video of the codes I use in the discord bot making series that I have publishe

Umut Bayraktar 114 Jan 3, 2023
About Discord bot draft that does not contain ready-made commands, compatible with discord.js v14. Create your own discord bot with this command handler.

discordJS-V14 About Discord bot draft that does not contain ready-made commands, compatible with discord.js v14. Create your own discord bot with this

Umut Bayraktar 36 Dec 28, 2022
It is a discord bot bot which can play lofi song in different language 24/7. It has premium system and cool embed looks with buttons. It can play youtube songs, playlists. This bot code was made by Supreme#2401. It uses djs V12

Lofi-Radio-Music-Bot It is a discord bot bot which can play lofi song in different language 24/7. It has premium system and cool embed looks with butt

Diwas Atreya 89 Jan 2, 2023
A Discord bot project made with the npm package discord.js version 14

A Discord bot project made with the npm package discord.js version 14 and it's job to manage mails on a server, and this project includes only one Database: Quick.db. This project also handles Slash commands.

T.F.A 33 Jan 6, 2023
A utility package for making discord-bot commands much easier to write with discord.js.

Cordcommand About A utility package for making discord-bot commands much easier to write with discord.js. Usage Example // initiate discord.js client

Reinforz 15 Sep 15, 2022
A utility package for making discord-bot commands much easier to write with discord.js.

Cordmand About A utility package for making discord-bot commands much easier to write with discord.js. Usage Example Install this package: npm i @rein

Reinforz 15 Sep 28, 2022
Discord.js V14 Slash-Command and Context-Menu Handler. Most efficient and advanced Handler out there!

Base discord.js v14 Command Handler Written by Tomato6966 Made for Deezcord Features ✔ Slash Commands ✔ Sub-Slash Commands ✔ Group-Sub-Slash Commands

Tomato6966 9 Dec 21, 2022