Skip to content

Commit

Permalink
clean up emoji logic, add more caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jagrosh committed Oct 14, 2022
1 parent feedf8a commit 2b6a007
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
15 changes: 10 additions & 5 deletions src/main/java/com/jagrosh/giveawaybot/GiveawayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class GiveawayManager
MAX_PRIZE_LENGTH = 250,
MAX_DESCR_LENGTH = 1000,
FAILURE_COOLDOWN_TIME = 30;
private final static Color ENDED_COLOR = new Color(0x2F3136);
private final static Permission[] REQUIRED_PERMS = { Permission.SEND_MESSAGES, Permission.VIEW_CHANNEL,
Permission.READ_MESSAGE_HISTORY, Permission.EMBED_LINKS };

Expand Down Expand Up @@ -260,14 +261,11 @@ public SentMessage renderGiveaway(Giveaway giveaway, int numEntries, List<Cached
SentMessage.Builder sb = new SentMessage.Builder()
.addEmbed(new Embed.Builder()
.setTitle(giveaway.getPrize(), null)
.setColor(winners == null ? gs.getColor() : new Color(0x2F3136))
.setColor(winners == null ? gs.getColor() : ENDED_COLOR)
.setTimestamp(giveaway.getEndInstant())
.setDescription(message).build());
if(winners == null)
{
EmojiParser.ParsedEntryButton pe = emojis.parse(gs.getEmoji());
sb.addComponent(new ActionRowComponent(new ButtonComponent(ButtonComponent.Style.PRIMARY, pe.text, new PartialEmoji(pe.name, pe.id, pe.animated), ENTER_BUTTON_ID, null, false)));
}
sb.addComponent(new ActionRowComponent(createEntryButton(emojis.parse(gs.getEmoji()))));
else if(summaryKey != null)
sb.addComponent(new ActionRowComponent(new ButtonComponent(LocalizedMessage.GIVEAWAY_SUMMARY.getLocalizedMessage(gs.getLocale()), Constants.SUMMARY + "#giveaway=" + summaryKey)));
else
Expand Down Expand Up @@ -308,4 +306,11 @@ private JSONObject createGiveawaySummary(Giveaway giveaway, CachedUser host, Lis
.put("winners", JsonUtil.buildArray(winners))
.put("entries", JsonUtil.buildArray(entries));
}

private ButtonComponent createEntryButton(EmojiParser.ParsedEntryButton pe)
{
return new ButtonComponent(ButtonComponent.Style.PRIMARY, pe.text,
pe.hasEmoji() ? new PartialEmoji(pe.name, pe.id, pe.animated) : null,
ENTER_BUTTON_ID, null, false);
}
}
23 changes: 8 additions & 15 deletions src/main/java/com/jagrosh/giveawaybot/data/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Database
private final EntityManagerFactory emf;
private final EntityManager em;
private final Map<Long, GiveawayEntries> cachedEntries = new HashMap<>();
//private final Map<Long, Giveaway> cachedGiveaways = new HashMap<>();
private final Map<Long, Giveaway> cachedGiveawaysReadonly = new HashMap<>();
private final ScheduledExecutorService cacheCombiner = Executors.newSingleThreadScheduledExecutor();

public Database(String host, String user, String pass)
Expand Down Expand Up @@ -136,12 +136,12 @@ public synchronized void setGuildEmoji(long guildId, String emoji)
// giveaways
public Giveaway getGiveaway(long id)
{
//if(cachedGiveaways.containsKey(id))
// return cachedGiveaways.get(id);
//Giveaway g = em.find(Giveaway.class, id);
//cachedGiveaways.put(id, g);
//return g;
return em.find(Giveaway.class, id);
if(cachedGiveawaysReadonly.containsKey(id))
return cachedGiveawaysReadonly.get(id);
Giveaway g = em.find(Giveaway.class, id);
cachedGiveawaysReadonly.put(id, g);
return g;
//return em.find(Giveaway.class, id);
}

public List<Giveaway> getGiveawaysByGuild(long guildId)
Expand Down Expand Up @@ -183,6 +183,7 @@ public synchronized void createGiveaway(Giveaway giveaway)

public synchronized void removeGiveaway(long id)
{
cachedGiveawaysReadonly.remove(id);
Giveaway g = em.find(Giveaway.class, id);
if(g != null)
{
Expand Down Expand Up @@ -298,14 +299,6 @@ private GiveawayEntries getEntries(long giveawayId)
return cachedEntries.containsKey(giveawayId) ? cachedEntries.get(giveawayId) : em.find(GiveawayEntries.class, giveawayId);
}

/*public int getEntryCount(long giveawayId)
{
GiveawayEntries ge = em.find(GiveawayEntries.class, giveawayId);
if(ge == null)
return 0;
return ge.getUsers().size();
}*/


// premium
public PremiumLevel getPremiumLevel(long guildId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public boolean isFree()
return text == null && id == 0L && !animated && freeEmoji.contains(name);
}

public boolean hasEmoji()
{
return id != 0L || name != null;
}

public String render()
{
return ((id == 0L
Expand Down

0 comments on commit 2b6a007

Please sign in to comment.