Skip to content

Commit

Permalink
✨ better validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico105 committed May 8, 2021
1 parent 090add9 commit 6b88f0c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/Giveaway.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Giveaway extends EventEmitter {
this.winnerIDs = options.winnerIDs;
/**
* The mention of the user who hosts this giveaway
* @type {?string}
* @type {string?}
*/
this.hostedBy = options.hostedBy;
/**
Expand Down Expand Up @@ -149,7 +149,7 @@ class Giveaway extends EventEmitter {

/**
* The reaction on the giveaway message
* @type {string}
* @type {Discord.EmojiIdentifierResolvable}
*/
get reaction() {
return this.options.reaction || this.manager.options.default.reaction;
Expand Down Expand Up @@ -473,11 +473,10 @@ class Giveaway extends EventEmitter {
// Update data
if (Number.isInteger(options.newWinnerCount) && options.newWinnerCount > 0) this.winnerCount = options.newWinnerCount;
if (typeof options.newPrize === 'string') this.prize = options.newPrize;
if (options.addTime && !isNaN(options.addTime)) this.endAt = this.endAt + options.addTime;
if (options.setEndTimestamp && !isNaN(options.setEndTimestamp)) this.endAt = options.setEndTimestamp;
if (!isNaN(options.addTime) && typeof options.addTime === 'number') this.endAt = this.endAt + options.addTime;
if (!isNaN(options.setEndTimestamp) && typeof options.setEndTimestamp === 'number') this.endAt = options.setEndTimestamp;
if (options.newMessages && typeof options.newMessages === 'object') this.messages = merge(this.messages, options.newMessages);
if (Array.isArray(options.newBonusEntries) && options.newBonusEntries.every((elem) => typeof elem === 'object'))
this.options.bonusEntries = options.newBonusEntries;
if (Array.isArray(options.newBonusEntries)) this.options.bonusEntries = options.newBonusEntries.filter((elem) => typeof elem === 'object');
if (options.newExtraData) this.extraData = options.newExtraData;
// Call the db method
await this.manager.editGiveaway(this.messageID, this.data);
Expand Down Expand Up @@ -561,6 +560,9 @@ class Giveaway extends EventEmitter {
if (!this.message) {
return reject('Unable to fetch message with ID ' + this.messageID + '.');
}
if (options.winnerCount && (!Number.isInteger(options.winnerCount) || options.winnerCount < 1)) {
return reject(`options.winnerCount is not a positive integer. (val=${options.winnerCount})`);
}
const winners = await this.roll(options.winnerCount || undefined);
if (winners.length > 0) {
this.winnerIDs = winners.map((w) => w.id);
Expand Down
4 changes: 2 additions & 2 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ class GiveawaysManager extends EventEmitter {
if (!channel || !channel.id) {
return reject(`channel is not a valid guildchannel. (val=${channel})`);
}
if (!options.time || isNaN(options.time)) {
return reject(`options.time is not a number. (val=${options.time})`);
if (isNaN(options.time) || typeof options.time !== 'number' || options.time < 1) {
return reject(`options.time is not a positive number. (val=${options.time})`);
}
if (typeof options.prize !== 'string') {
return reject(`options.prize is not a string. (val=${options.prize})`);
Expand Down

0 comments on commit 6b88f0c

Please sign in to comment.