Skip to content

Commit

Permalink
🐛 fill strings before embed limit checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico105 committed Sep 3, 2021
1 parent 0e8b48c commit 6a6b8bc
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ class GiveawaysManager extends EventEmitter {
* @returns {Discord.MessageEmbed} The generated embed
*/
generateMainEmbed(giveaway, lastChanceEnabled = false) {
const embed = new Discord.MessageEmbed();
embed
const embed = new Discord.MessageEmbed()
.setTitle(giveaway.prize)
.setColor(
giveaway.pauseOptions.isPaused && giveaway.pauseOptions.embedColor
Expand Down Expand Up @@ -118,26 +117,29 @@ class GiveawaysManager extends EventEmitter {
generateEndEmbed(giveaway, winners) {
let formattedWinners = winners.map((w) => `<@${w.id}>`).join(', ');

const descriptionString = (formattedWinners) => {
return giveaway.messages.winners + ' ' + formattedWinners + (giveaway.hostedBy ? '\n' + giveaway.messages.hostedBy : '');
const strings = {
winners: giveaway.fillInString(giveaway.messages.giveaway),
hostedBy: giveaway.fillInString(giveaway.messages.hostedBy),
prize: giveaway.fillInString(giveaway.prize),
endedAt: giveaway.fillInString(giveaway.messages.endedAt)
};

const descriptionString = (formattedWinners) => strings.winners + ' ' + formattedWinners + (giveaway.hostedBy ? '\n' + strings.hostedBy : '');

for (
let i = 1;
descriptionString(formattedWinners).length > 4096 ||
giveaway.prize.length + giveaway.messages.endedAt.length + descriptionString(formattedWinners).length > 6000;
strings.prize.length + strings.endedAt.length + descriptionString(formattedWinners).length > 6000;
i++
) formattedWinners = formattedWinners.substr(0, formattedWinners.lastIndexOf(', <@')) + `, ${i} more`;

const embed = new Discord.MessageEmbed();
embed
.setTitle(giveaway.prize)
return new Discord.MessageEmbed()
.setTitle(strings.prize)
.setColor(giveaway.embedColorEnd)
.setFooter(giveaway.messages.endedAt, giveaway.messages.embedFooter.iconURL)
.setFooter(strings.endedAt, giveaway.messages.embedFooter.iconURL)
.setDescription(descriptionString(formattedWinners))
.setTimestamp(new Date(giveaway.endAt).toISOString())
.setThumbnail(giveaway.thumbnail);
return giveaway.fillInEmbed(embed);
}

/**
Expand All @@ -146,8 +148,7 @@ class GiveawaysManager extends EventEmitter {
* @returns {Discord.MessageEmbed} The generated embed
*/
generateNoValidParticipantsEndEmbed(giveaway) {
const embed = new Discord.MessageEmbed();
embed
const embed = new Discord.MessageEmbed()
.setTitle(giveaway.prize)
.setColor(giveaway.embedColorEnd)
.setFooter(giveaway.messages.endedAt, giveaway.messages.embedFooter.iconURL)
Expand Down

0 comments on commit 6a6b8bc

Please sign in to comment.