Skip to content

Commit

Permalink
fix: assign gaws only to their respective shards and not 'all to all'
Browse files Browse the repository at this point in the history
refreshStorage() (with broadCastEval) never did smth XD
  • Loading branch information
Nico105 committed Feb 9, 2022
1 parent 39c1cd5 commit 9a1f12d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 104 deletions.
52 changes: 1 addition & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,54 +520,4 @@ You can use your custom database to save giveaways, instead of the json files (t
- [Apache CouchDB - Nano](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/nano.js)
- Replit Database ⚠️ Only usable if your bot is hosted on [Replit](https://replit.com/)
- [@replit/database](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/replit.js)
- [Quick.Replit](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/quick.replit.js)

## Support shards

To make `discord-giveaways` working with shards, you will need to extend the `GiveawaysManager` class and update the `refreshStorage()` method. This method should call the `getAllGiveaways()` method for **every** shard, so all `GiveawaysManager` synchronize their cache with the updated database.

⚠️ **Note**: If you are using a [custom database](https://github.com/Androz2091/discord-giveaways#custom-database) then you must call (= add to code) `this.refreshStorage()` at the end of your extended `saveGiveaway`, `editGiveaway` and `deleteGiveaway` methods.

```js
const Discord = require('discord.js'),
client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Discord.Intents.FLAGS.GUILD_MEMBERS // Not required, but recommended
]
}),
settings = {
prefix: 'g!',
token: 'Your Discord Bot Token'
};

// Extends the GiveawaysManager class and update the refreshStorage method
const { GiveawaysManager } = require('discord-giveaways');
const GiveawayManagerWithShardSupport = class extends GiveawaysManager {
// Refresh storage method is called when the database is updated on one of the shards
async refreshStorage() {
// This should make all shard refreshing their cache with the updated database
return client.shard.broadcastEval(() => this.giveawaysManager.getAllGiveaways());
}
};

// Create a new instance of your new class
const manager = new GiveawayManagerWithShardSupport(client, {
storage: './giveaways.json',
default: {
botsCanWin: false,
embedColor: '#FF0000',
embedColorEnd: '#000000',
reaction: '🎉'
}
});
// We now have a giveawaysManager property to access the manager everywhere!
client.giveawaysManager = manager;

client.on('ready', () => {
console.log('I\'m ready!');
});

client.login(settings.token);
```
- [Quick.Replit](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/quick.replit.js)
40 changes: 0 additions & 40 deletions examples/sharding.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/Giveaway.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class Giveaway extends EventEmitter {

// Fetch all guild members if the intent is available
if (new Discord.Intents(this.client.options.intents).has(Discord.Intents.FLAGS.GUILD_MEMBERS)) {
await guild.members.fetch();
await guild.members.fetch().catch(() => {});
}

// Fetch all reaction users
Expand Down
23 changes: 11 additions & 12 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,15 +407,6 @@ class GiveawaysManager extends EventEmitter {
),
'utf-8'
);
this.refreshStorage();
return true;
}

/**
* Refresh the cache to support shards.
* @ignore
*/
async refreshStorage() {
return true;
}

Expand Down Expand Up @@ -475,7 +466,6 @@ class GiveawaysManager extends EventEmitter {
),
'utf-8'
);
this.refreshStorage();
return;
}

Expand All @@ -494,7 +484,6 @@ class GiveawaysManager extends EventEmitter {
),
'utf-8'
);
this.refreshStorage();
return;
}

Expand Down Expand Up @@ -641,8 +630,18 @@ class GiveawaysManager extends EventEmitter {
* @ignore
*/
async _init() {
const rawGiveaways = await this.getAllGiveaways();
let rawGiveaways = await this.getAllGiveaways();

await (this.client.readyAt ? Promise.resolve() : new Promise((resolve) => this.client.once('ready', resolve)));

// Filter giveaways for each shard
if (this.client.shard && this.client.guilds.cache.size) {
const shardId = this.client.shard.shardIdForGuildId(this.client.guilds.cache.first().id, this.client.shard.count);
rawGiveaways = rawGiveaways.filter((g) => shardId === this.client.shard.shardIdForGuildId(g.guildId, this.client.shard.count));
}

rawGiveaways.forEach((giveaway) => this.giveaways.push(new Giveaway(this, giveaway)));

setInterval(() => {
if (this.client.readyAt) this._checkGiveaway.call(this);
}, this.options.forceUpdateEvery || DEFAULT_CHECK_INTERVAL);
Expand Down

0 comments on commit 9a1f12d

Please sign in to comment.