Skip to content

Commit

Permalink
remove channel getter
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico105 committed Aug 5, 2021
1 parent 9a65bcc commit 235f9d1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
36 changes: 15 additions & 21 deletions src/Giveaway.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,6 @@ class Giveaway extends EventEmitter {
return false;
}

/**
* The channel of the giveaway.
* @type {Discord.TextChannel|Discord.NewsChannel|Discord.ThreadChannel}
* @readonly
*/
get channel() {
return this.client.channels.cache.get(this.channelID);
}

/**
* Gets the content of the giveaway.
* @type {string}
Expand Down Expand Up @@ -339,10 +330,18 @@ class Giveaway extends EventEmitter {
async fetchMessage() {
return new Promise(async (resolve, reject) => {
if (!this.messageID) return;
const message = await this.channel.messages.fetch(this.messageID).catch(() => {});
let tryLater = false;
const channel = await this.client.channels.fetch(this.channelID).catch((err) => {
if ((err.code).toString().startsWith('5') || err.code === 429) tryLater = true;
});
const message = await channel.messages.fetch(this.messageID).catch((err) => {
if ((err.code).toString().startsWith('5') || err.code === 429) tryLater = true;
});
if (!message) {
this.manager.giveaways = this.manager.giveaways.filter((g) => g.messageID !== this.messageID);
await this.manager.deleteGiveaway(this.messageID);
if (!tryLater) {
this.manager.giveaways = this.manager.giveaways.filter((g) => g.messageID !== this.messageID);
await this.manager.deleteGiveaway(this.messageID);
}
return reject('Unable to fetch message with ID ' + this.messageID + '.');
}
this.message = message;
Expand All @@ -356,8 +355,8 @@ class Giveaway extends EventEmitter {
*/
async checkWinnerEntry(user) {
if (this.winnerIDs.includes(user.id)) return false;
const guild = this.channel.guild;
const member = guild.members.cache.get(user.id) || (await guild.members.fetch(user.id).catch(() => {}));
const guild = this.message.guild;
const member = await guild.members.fetch(user.id).catch(() => {});
if (!member) return false;
const exemptMember = await this.exemptMembers(member);
if (exemptMember) return false;
Expand All @@ -371,7 +370,7 @@ class Giveaway extends EventEmitter {
* @returns {Promise<number|boolean>} The highest bonus entries the user should get or false.
*/
async checkBonusEntries(user) {
const member = this.channel.guild.members.cache.get(user.id);
const member = this.message.guild.members.cache.get(user.id);
const entries = [];
const cumulativeEntries = [];

Expand Down Expand Up @@ -412,7 +411,7 @@ class Giveaway extends EventEmitter {
reactions.find((r) => r.emoji.name === Discord.Util.resolvePartialEmoji(this.reaction)?.name) ||
reactions.get(Discord.Util.resolvePartialEmoji(this.reaction)?.id);
if (!reaction) return [];
const guild = this.channel.guild;
const guild = this.message.guild;
// Fetch guild members
if (new Discord.Intents(this.client.options.intents).has('GUILD_MEMBERS')) await guild.members.fetch();

Expand Down Expand Up @@ -484,7 +483,6 @@ class Giveaway extends EventEmitter {
edit(options = {}) {
return new Promise(async (resolve, reject) => {
if (this.ended) return reject('Giveaway with message ID ' + this.messageID + ' is already ended.');
if (!this.channel) return reject('Unable to get the channel of the giveaway with message ID ' + this.messageID + '.');
await this.fetchMessage().catch(() => {});
if (!this.message) return reject('Unable to fetch message with ID ' + this.messageID + '.');

Expand Down Expand Up @@ -515,7 +513,6 @@ class Giveaway extends EventEmitter {
end() {
return new Promise(async (resolve, reject) => {
if (this.ended) return reject('Giveaway with message ID ' + this.messageID + ' is already ended');
if (!this.channel) return reject('Unable to get the channel of the giveaway with message ID ' + this.messageID + '.');
this.ended = true;
this.endAt = Date.now();
await this.fetchMessage().catch(() => {});
Expand Down Expand Up @@ -578,7 +575,6 @@ class Giveaway extends EventEmitter {
reroll(options) {
return new Promise(async (resolve, reject) => {
if (!this.ended) return reject('Giveaway with message ID ' + this.messageID + ' is not ended.');
if (!this.channel) return reject('Unable to get the channel of the giveaway with message ID ' + this.messageID + '.');
await this.fetchMessage().catch(() => {});
if (!this.message) return reject('Unable to fetch message with ID ' + this.messageID + '.');
if (options.winnerCount && (!Number.isInteger(options.winnerCount) || options.winnerCount < 1)) {
Expand Down Expand Up @@ -640,7 +636,6 @@ class Giveaway extends EventEmitter {
pause(options = {}) {
return new Promise(async (resolve, reject) => {
if (this.ended) return reject('Giveaway with message ID ' + this.messageID + ' is already ended.');
if (!this.channel) return reject('Unable to get the channel of the giveaway with message ID ' + this.messageID + '.');
await this.fetchMessage().catch(() => {});
if (!this.message) return reject('Unable to fetch message with ID ' + this.messageID + '.');
if (this.pauseOptions.isPaused) return reject('Giveaway with message ID ' + this.messageID + ' is already paused.');
Expand Down Expand Up @@ -684,7 +679,6 @@ class Giveaway extends EventEmitter {
unpause() {
return new Promise(async (resolve, reject) => {
if (this.ended) return reject('Giveaway with message ID ' + this.messageID + ' is already ended.');
if (!this.channel) return reject('Unable to get the channel of the giveaway with message ID ' + this.messageID + '.');
await this.fetchMessage().catch(() => {});
if (!this.message) return reject('Unable to fetch message with ID ' + this.messageID + '.');
if (!this.pauseOptions.isPaused) return reject('Giveaway with message ID ' + this.messageID + ' is not paused.');
Expand Down
19 changes: 3 additions & 16 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,6 @@ class GiveawaysManager extends EventEmitter {
return new Promise(async (resolve, reject) => {
const giveaway = this.giveaways.find((g) => g.messageID === messageID);
if (!giveaway) return reject('No giveaway found with ID ' + messageID + '.');
if (!giveaway.channel && !doNotDeleteMessage) {
return reject('Unable to get the channel of the giveaway with message ID ' + giveaway.messageID + '.');
}

if (!doNotDeleteMessage) {
await giveaway.fetchMessage().catch(() => {});
Expand Down Expand Up @@ -473,14 +470,9 @@ class GiveawaysManager extends EventEmitter {
}
return;
}
if (!giveaway.channel) return;
if (giveaway.remainingTime <= 0) return this.end(giveaway.messageID).catch(() => {});
await giveaway.fetchMessage().catch(() => {});
if (!giveaway.message) {
giveaway.ended = true;
await this.editGiveaway(giveaway.messageID, giveaway.data);
return;
}
if (!giveaway.message) return;
if (giveaway.pauseOptions.isPaused) {
if (
(isNaN(giveaway.pauseOptions.unPauseAfter) || typeof giveaway.pauseOptions.unPauseAfter !== 'number') &&
Expand Down Expand Up @@ -521,9 +513,9 @@ class GiveawaysManager extends EventEmitter {
const guild = this.client.guilds.cache.get(packet.d.guild_id) || (await this.client.guilds.fetch(packet.d.guild_id).catch(() => {}));
if (!guild || !guild.available) return;
if (packet.d.user_id === this.client.user.id) return;
const member = guild.members.cache.get(packet.d.user_id) || (await guild.members.fetch(packet.d.user_id).catch(() => {}));
const member = await guild.members.fetch(packet.d.user_id).catch(() => {});
if (!member) return;
const channel = guild.channels.cache.get(packet.d.channel_id) || (await this.client.channels.fetch(packet.d.channel_id).catch(() => {}));
const channel = await this.client.channels.fetch(packet.d.channel_id).catch(() => {});
if (!channel) return;
const message = await channel.messages.fetch(packet.d.message_id).catch(() => {});
if (!message) return;
Expand All @@ -548,11 +540,6 @@ class GiveawaysManager extends EventEmitter {
async _init() {
const rawGiveaways = await this.getAllGiveaways();
rawGiveaways.forEach((giveaway) => this.giveaways.push(new Giveaway(this, giveaway)));
const cacheAllGiveawayChannels = async () => {
if (!this.client.readyAt) return setTimeout(cacheAllGiveawayChannels, 100);
for (const giveaway of this.giveaways) await this.client.channels.fetch(giveaway.channelID).catch(() => {});
};
await cacheAllGiveawayChannels();
setInterval(() => {
if (this.client.readyAt) this._checkGiveaway.call(this);
}, this.options.updateCountdownEvery);
Expand Down
1 change: 0 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ declare module 'discord-giveaways' {
readonly duration: number;
readonly messageURL: string;
readonly remainingTimeText: string;
readonly channel: TextChannel | NewsChannel | ThreadChannel;
readonly exemptMembersFunction: Function | null;
readonly bonusEntries: BonusEntry[];
readonly data: GiveawayData;
Expand Down

0 comments on commit 235f9d1

Please sign in to comment.