Androz2091 / discord-giveaways

🎉 Complete framework to facilitate the creation of giveaways using discord.js
https://discord-giveaways.js.org
MIT License
335 stars 127 forks source link

Adding support for Discord-Hybrid-Sharding #454

Closed typhoon11 closed 1 year ago

typhoon11 commented 2 years ago

Is your feature request related to a problem? Please describe.

We all know how big Discord.Js's Sharding Manager is and to minimize that Discord-Hybrid-Sharding was made which is light in weight, consumes less ram, and gives the access to make Clusters. So I wish the support for hybrid-sharding should be added in this package.

The package link is: https://www.npmjs.com/package/discord-hybrid-sharding

Nico105 commented 2 years ago

Not really wanna throw with numbers, but it doesn't seem that popular as it would give reason to support it specifically. Also the question is, if the implementation would be undisturbing, cause it does seem to change some stuff. And also relying on another package is risky, cause if it gets abandoned or left behind then it's worthless. So for now it doesn't seem that likely that it will get supported by us. And I already tried smth similar with eris back then, which didn't work for the reasons above

but if someone wants to prove the simplicity of the implementation, then a draft pr can be made of course.

meister03 commented 2 years ago

Should support every Sharding Solution

const client  = new Discord.Client(...)
const {getAllGiveaways} = manager;
manager.getAllGiveaways = () => {
  const rawGiveaways = await getAllGiveaways();
  const shardIds = [...client.ws.shards.keys()];
  if(shardIds.length === 1) return rawGiveaways;

  return rawGiveaways.filter((g) => 
    shardIds.includes(Discord.ShardClientUtil.shardIdForGuildId(g.guildId, client.shard.count))
  );
}
Tomato6966 commented 2 years ago

Should support every Sharding Solution

const client  = new Discord.Client(...)
const {getAllGiveaways} = manager;
manager.getAllGiveaways = () => {
  const rawGiveaways = await getAllGiveaways();
  const shardIds = [...client.ws.shards.keys()];
  if(shardIds.length === 1) return rawGiveaways;

  return rawGiveaways.filter((g) => 
    shardIds.includes(Discord.ShardClientUtil.shardIdForGuildId(g.guildId, client.shard.count))
  );
}

You need to do multiple things. How I do it (and finally made it work:)

const { ShardClientUtil } = require("discord.js");
// when extending the giveaway manager, add this for getAllGiveaways
async getAllGiveaways() {
    // this function is just a boiler template.
    const rawGiveaways = await getDataBaseDataSomehow(); // find from example what should be the method for your DB
    const shardIds = client.cluster.info.SHARD_LIST || [ 0 ]; // for hybrid sharding
    return rawGiveaways.filter((g) => shardIds.includes(ShardClientUtil.shardIdForGuildId(g.guildId, client.cluster.info.TOTAL_SHARDS)));
}

And before creating the manager you have to do this:

client.shard = {
    count: client.cluster.info.TOTAL_SHARDS,
};

because the package needs the client to have a count property which should be the ENTIRE SHARDS amount.