meister03 / discord-hybrid-sharding

Cluster your Discord Bot to save a lot of resources. The ultimate Package, which combines the Sharding Manager & Internal Sharding.
Other
202 stars 52 forks source link

Get rate limited when login to the bot. #55

Closed RainyXeon closed 2 years ago

RainyXeon commented 2 years ago

I have an issue that when adding sharding system in my bot I always login to the bot 2 times and get rate limited, how to fix this? Here is my code file:

index.js:

const Cluster = require('discord-hybrid-sharding');
require("dotenv").config();
const logger = require('./plugins/logger.js')

const manager = new Cluster.Manager(`./main/login.js`, {
    totalShards: 'auto', // or 'auto'
    shardsPerClusters: 2,
    mode: 'process', // you can also choose "worker"
    token: process.env.TOKEN,
});

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

login.js

const MainClient = require("./bot.js");
const client = new MainClient();

client.connect()
module.exports = client; 

bot.js

const { Client, Intents, Collection } = require("discord.js");
const { Manager } = require("erela.js");
const Spotify = require("erela.js-spotify")
const Deezer = require("../plugins/Deezer");
const AppleMusic = require("erela.js-apple")
const Facebook = require("erela.js-facebook");
const { I18n } = require("locale-parser")
const Cluster = require('discord-hybrid-sharding');

class MainClient extends Client {
     constructor() {
        super({
          shards: Cluster.data.SHARD_LIST,
          shardCount: Cluster.data.TOTAL_SHARDS,
            allowedMentions: {
                parse: ["roles", "users", "everyone"],
                repliedUser: false
            },
            intents: [
                Intents.FLAGS.GUILDS,
                Intents.FLAGS.GUILD_VOICE_STATES,
                Intents.FLAGS.GUILD_MESSAGES,
            ]
        });
    this.config = require("../plugins/config.js");
    this.owner = this.config.OWNER_ID;
    this.dev = this.config.DEV_ID;
    this.color = this.config.EMBED_COLOR;
    this.i18n = new I18n(this.config.LANGUAGE);
    if(!this.token) this.token = this.config.TOKEN;
    const clientID = this.config.SPOTIFY_ID
    const clientSecret = this.config.SPOTIFY_SECRET

    process.on('unhandledRejection', error => console.log(error));
    process.on('uncaughtException', error => console.log(error));

    const client = this;

    this.manager = new Manager({
      nodes: this.config.NODES,
      autoPlay: true,
      plugins: [
        new Spotify({
          clientID,
          clientSecret
        }),
        new Facebook(),
        new Deezer(),
        new AppleMusic()
      ],
      send(id, payload) {
        const guild = client.guilds.cache.get(id);
        if (guild) guild.shard.send(payload);
      },
    });

    ["slash", "premiums"].forEach(x => client[x] = new Collection());
    ["loadCommand", "loadEvent", "loadDatabase", "loadPlayer" ].forEach(x => require(`../handlers/${x}`)(client));

    }
        connect() {
      return super.login(this.token);
    };
};

module.exports = MainClient;
meister03 commented 2 years ago

Does it respond twice or hit the gateway/bot route?

RainyXeon commented 2 years ago

@meister03 it respond twice when I run the code and I have this log

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: PassThrough {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: null
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://discord.com/api/v10/gateway/bot',
    status: 429,
    statusText: 'Too Many Requests',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}
meister03 commented 2 years ago

Could you add an event listener on the client client.on('debug', console.log)

RainyXeon commented 2 years ago

@meister03 ok let me try

RainyXeon commented 2 years ago

@meister03 It gives me these 2 logos and I don't know what to do with it

Provided token: OTc4NTMwNjc0MzU1MTAxNzc2.GDrWRU.**************************************
Preparing to connect to the gateway...
Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: PassThrough {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: null
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://discord.com/api/v10/gateway/bot',
    status: 429,
    statusText: 'Too Many Requests',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}
Hit a 429 while executing a request.
    Global  : false
    Method  : get
    Path    : /gateway/bot
    Route   : /gateway/bot
    Limit   : 2
    Timeout : 2114ms
    Sublimit: None
[7/26/2022, 5:42:16 PM] - [ERROR  ] - [Rate Limited, Sleeping for 0 seconds]
[WS => Manager] Fetched Gateway Information
    URL: wss://gateway.discord.gg
    Recommended Shards: 1
[WS => Manager] Session Limit Information
    Total: 1000
    Remaining: 985
[WS => Manager] Spawning shards: 0
[WS => Shard 0] [CONNECT]
    Gateway    : wss://gateway.discord.gg/
    Version    : 9
    Encoding   : json
    Compression: none
[WS => Shard 0] Setting a HELLO timeout for 20s.
[WS => Shard 0] [CONNECTED] Took 337ms
[WS => Shard 0] Clearing the HELLO timeout.
[WS => Shard 0] Setting a heartbeat interval for 41250ms.
[WS => Shard 0] [IDENTIFY] Shard 0/1 with intents: 641
[WS => Shard 0] [READY] Session 50e33a6acc573170d64c64f64df9ece8.
[WS => Shard 0] [ReadyHeartbeat] Sending a heartbeat.
[WS => Shard 0] Shard received all its guilds. Marking as fully ready.
Provided token: OTc4NTMwNjc0MzU1MTAxNzc2.GDrWRU.**************************************
Preparing to connect to the gateway...
[WS => Manager] Fetched Gateway Information
    URL: wss://gateway.discord.gg
    Recommended Shards: 1
[WS => Manager] Session Limit Information
    Total: 1000
    Remaining: 986
[WS => Manager] Spawning shards: 0
[WS => Shard 0] [CONNECT]
    Gateway    : wss://gateway.discord.gg/
    Version    : 9
    Encoding   : json
    Compression: none
[WS => Shard 0] Setting a HELLO timeout for 20s.
[7/26/2022, 5:42:12 PM] - [INFO   ] - [Launched Cluster 0]
[WS => Shard 0] [CONNECTED] Took 436ms
[WS => Shard 0] Clearing the HELLO timeout.
[WS => Shard 0] Setting a heartbeat interval for 41250ms.
[WS => Shard 0] [IDENTIFY] Shard 0/1 with intents: 641
[WS => Shard 0] [READY] Session d08d219a108d59e33b1e9434d5d44df0.
[WS => Shard 0] [ReadyHeartbeat] Sending a heartbeat.
[WS => Shard 0] Shard received all its guilds. Marking as fully ready.
meister03 commented 2 years ago

Okay, could you log out Cluster.data. I have a guess...

RainyXeon commented 2 years ago

@meister03 ok let me try

RainyXeon commented 2 years ago

@meister03 but how?

meister03 commented 2 years ago
const Cluster = require('discord-hybrid-sharding');
console.log(Cluster.data)
class MainClient extends Client {
RainyXeon commented 2 years ago

@meister03 here is the Cluster.data log:

{
SHARD_LIST: [ 0 ],
TOTAL_SHARDS: 1,
CLUSTER_COUNT: 1,
CLUSTER: 0, 
CLUSTER_MANAGER_MODE: 'process'
MAINTENANCE: 'undefined', CLUSTER_QUEUE_MODE: 'auto',
FIRST_SHARD_ID: 0, 
LAST_SHARD_ID: 0
}
meister03 commented 2 years ago

On Both Clusters?

RainyXeon commented 2 years ago

@meister03 I dont know what you mean but may be is only this cluster.