Skip to content

Commit

Permalink
💥 Replace updateCountdownEvery option
Browse files Browse the repository at this point in the history
  • Loading branch information
Androz2091 committed Aug 29, 2021
1 parent 43fde0f commit 5844ba5
Show file tree
Hide file tree
Showing 13 changed files with 9 additions and 17 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ const GiveawayManagerWithShardSupport = class extends GiveawaysManager {
// Create a new instance of your new class
const manager = new GiveawayManagerWithShardSupport(client, {
storage: './storage.json',
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/custom-databases/enmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/custom-databases/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/custom-databases/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/custom-databases/nano.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/custom-databases/quick.db.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/custom-databases/quick.replit.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/custom-databases/quickmongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/custom-databases/replit.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
1 change: 0 additions & 1 deletion examples/sharding.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const GiveawayManagerWithShardSupport = class extends GiveawaysManager {
// Create a new instance of your new class
const manager = new GiveawayManagerWithShardSupport(client, {
storage: './storage.json',
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: '#FF0000',
Expand Down
4 changes: 2 additions & 2 deletions src/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ exports.PauseOptions = {
* @typedef GiveawaysManagerOptions
*
* @property {string} [storage='./giveaways.json'] The storage path for the giveaways.
* @property {number} [updateCountdownEvery=5000] The giveaway update interval in milliseconds.
* @property {number} [forceUpdateEvery=5000] Force the giveaway messages to be updated at a specific interval.
* @property {number} [endedGiveawaysLifetime=null] The number of milliseconds after which ended giveaways should get deleted from the DB. ⚠ Giveaways deleted from the DB cannot get rerolled anymore!
* @property {Object} [default] The default options for new giveaways.
* @property {Boolean} [default.botsCanWin=false] If bots can win giveaways.
Expand All @@ -131,7 +131,7 @@ exports.PauseOptions = {
*/
exports.GiveawaysManagerOptions = {
storage: './giveaways.json',
updateCountdownEvery: 5000,
forceUpdateEvery: null,
endedGiveawaysLifetime: null,
default: {
botsCanWin: false,
Expand Down
10 changes: 6 additions & 4 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const {
const Giveaway = require('./Giveaway.js');
const { validateEmbedColor, embedEqual } = require('./utils.js');

const DEFAULT_CHECK_INTERVAL = 15_000;

/**
* Giveaways Manager
*/
Expand Down Expand Up @@ -507,13 +509,13 @@ class GiveawaysManager extends EventEmitter {

// fifth case: the giveaway will be ended soon, we add a timeout so it ends at the right time
// and it does not need to wait for _checkGiveaway to be called again
if (giveaway.remainingTime < this.options.updateCountdownEvery) {
if (giveaway.remainingTime < (this.options.forceUpdateEvery || DEFAULT_CHECK_INTERVAL)) {
setTimeout(() => this.end.call(this, giveaway.messageId).catch(() => {}), giveaway.remainingTime);
}

// sixth case: the giveaway will be in the last chance state soon, we add a timeout so it's updated at the right time
// and it does not need to wait for _checkGiveaway to be called again
if (giveaway.lastChance.enabled && (giveaway.remainingTime - giveaway.lastChance.threshold) < this.options.updateCountdownEvery) {
if (giveaway.lastChance.enabled && (giveaway.remainingTime - giveaway.lastChance.threshold) < (this.options.forceUpdateEvery || DEFAULT_CHECK_INTERVAL)) {
setTimeout(async () => {
giveaway.message ??= await giveaway.fetchMessage().catch(() => {});
const embed = this.generateMainEmbed(giveaway, true);
Expand All @@ -526,7 +528,7 @@ class GiveawaysManager extends EventEmitter {
const updatedEmbed = this.generateMainEmbed(giveaway, lastChanceEnabled);
const needUpdate = !embedEqual(giveaway.message.embeds[0], updatedEmbed) || giveaway.message.content !== giveaway.messages.giveaway;

if (needUpdate || this.options.updateCountdownEvery) {
if (needUpdate || this.options.forceUpdateEvery) {
giveaway.message ??= await giveaway.fetchMessage().catch(() => {});
const embed = this.generateMainEmbed(giveaway, );
giveaway.message?.edit({ content: giveaway.messages.giveaway, embeds: [embed], allowedMentions: giveaway.allowedMentions }).catch(() => {});
Expand Down Expand Up @@ -576,7 +578,7 @@ class GiveawaysManager extends EventEmitter {
rawGiveaways.forEach((giveaway) => this.giveaways.push(new Giveaway(this, giveaway)));
setInterval(() => {
if (this.client.readyAt) this._checkGiveaway.call(this);
}, this.options.updateCountdownEvery || 15_000);
}, this.options.forceUpdateEvery || DEFAULT_CHECK_INTERVAL);
this.ready = true;

if (!isNaN(this.options.endedGiveawaysLifetime) && typeof this.options.endedGiveawaysLifetime === 'number') {
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ declare module 'discord-giveaways' {
}
interface GiveawaysManagerOptions {
storage?: string;
updateCountdownEvery?: number;
forceUpdateEvery?: number;
endedGiveawaysLifetime?: number;
default?: {
botsCanWin?: boolean,
Expand Down

0 comments on commit 5844ba5

Please sign in to comment.