Skip to content

Commit

Permalink
✨ end function, noWinnerMessage option (#303)
Browse files Browse the repository at this point in the history
Co-authored-by: Androz <androz2091@gmail.com>
  • Loading branch information
Nico105 and Androz2091 authored Aug 29, 2021
1 parent 9e89ea3 commit 299a32e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ client.on('messageCreate', (message) => {
});
```

- **noWinnerMessage**: Sent in the channel if there is no valid winner for the giveaway.

### Fetch giveaways

```js
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Discord = require('discord.js');
* @property {string} [timeRemaining='Time remaining: **{duration}**'] Displayed below "inviteToParticipate" in the giveaway embed. "{duration}" will be replaced automatically with the time remaining.
* @property {string} [winMessage='Congratulations, {winners}! You won **{prize}**!\n{messageURL}'] Sent in the channel when the giveaway is ended.
* @property {string|EmbedFooterObject} [embedFooter='Powered by the discord-giveaways package'] The footer of the giveaway embed.
* @property {string} [noWinner='Giveaway cancelled, no valid participations.'] Sent in the channel if there is no valid winner for the giveaway.
* @property {string} [noWinner='Giveaway cancelled, no valid participations.'] Displayed in the giveaway embed when there is no valid winner for the giveaway.
* @property {string} [winners='winner(s)'] Displayed next to the embed footer, used to display the number of winners of the giveaways.
* @property {string} [endedAt='Ended at'] Displayed next to the embed footer, used to display the giveaway end date.
* @property {string} [hostedBy='Hosted by: {user}'] Below the "inviteToParticipate" message, in the description of the embed.
Expand Down
4 changes: 3 additions & 1 deletion src/Giveaway.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,10 @@ class Giveaway extends EventEmitter {

/**
* Ends the giveaway.
* @param {string} [noWinnerMessage=null] Sent in the channel if there is no valid winner for the giveaway.
* @returns {Promise<Discord.GuildMember[]>} The winner(s).
*/
end() {
end(noWinnerMessage = null) {
return new Promise(async (resolve, reject) => {
if (this.ended) return reject('Giveaway with message Id ' + this.messageId + ' is already ended');
this.ended = true;
Expand Down Expand Up @@ -560,6 +561,7 @@ class Giveaway extends EventEmitter {
}
resolve(winners);
} else {
if (typeof noWinnerMessage === 'string') this.channel.send(noWinnerMessage);
const embed = this.manager.generateNoValidParticipantsEndEmbed(this);
this.message.edit({ content: this.messages.giveawayEnded, embeds: [embed] }).catch(() => {});
resolve([]);
Expand Down
7 changes: 4 additions & 3 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,20 @@ class GiveawaysManager extends EventEmitter {

/**
* Ends a giveaway. This method is automatically called when a giveaway ends.
* @param {Discord.Snowflake} messageId The message Id of the giveaway
* @param {Discord.Snowflake} messageID The message ID of the giveaway
* @param {string} [noWinnerMessage=null] Sent in the channel if there is no valid winner for the giveaway.
* @returns {Promise<Discord.GuildMember[]>} The winners
*
* @example
* manager.end('664900661003157510');
*/
end(messageId) {
end(messageID, noWinnerMessage = null) {
return new Promise(async (resolve, reject) => {
const giveaway = this.giveaways.find((g) => g.messageId === messageId);
if (!giveaway) return reject('No giveaway found with message Id ' + messageId + '.');

giveaway
.end()
.end(noWinnerMessage)
.then((winners) => {
this.emit('giveawayEnded', giveaway, winners);
resolve(winners);
Expand Down
4 changes: 2 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare module 'discord-giveaways' {
public delete(messageId: Snowflake, doNotDeleteMessage?: boolean): Promise<Giveaway>;
public deleteGiveaway(messageId: Snowflake): Promise<boolean>;
public edit(messageId: Snowflake, options: GiveawayEditOptions): Promise<Giveaway>;
public end(messageId: Snowflake): Promise<GuildMember[]>;
public end(messageID: Snowflake, noWinnerMessage?: string): Promise<GuildMember[]>;
public reroll(messageId: Snowflake, options?: GiveawayRerollOptions): Promise<GuildMember[]>;
public start(channel: TextChannel | NewsChannel | ThreadChannel, options: GiveawayStartOptions): Promise<Giveaway>;
public pause(messageId: Snowflake, options: PauseOptions): Promise<Giveaway>;
Expand Down Expand Up @@ -160,7 +160,7 @@ declare module 'discord-giveaways' {

public exemptMembers(member: GuildMember): Promise<boolean>;
public edit(options: GiveawayEditOptions): Promise<Giveaway>;
public end(): Promise<GuildMember[]>;
public end(noWinnerMessage?: string): Promise<GuildMember[]>;
public fetchMessage(): Promise<Message>;
public reroll(options?: GiveawayRerollOptions): Promise<GuildMember[]>;
public roll(winnerCount?: number): Promise<GuildMember[]>;
Expand Down

0 comments on commit 299a32e

Please sign in to comment.