From 3b7242990953a7a60117f19afa7b8ff1146b5f78 Mon Sep 17 00:00:00 2001 From: John Grosh Date: Sat, 10 Feb 2024 13:46:31 -0500 Subject: [PATCH] idk what this is --- .../jagrosh/giveawaybot/commands/ListCmd.java | 53 ++++++++++++------ .../jagrosh/giveawaybot/data/Database.java | 54 +++++++++++-------- .../entities/LocalizedMessage.java | 3 +- .../jagrosh/giveawaybot/entities/Uptimer.java | 2 +- .../localization/messages.properties | 1 + 5 files changed, 72 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/jagrosh/giveawaybot/commands/ListCmd.java b/src/main/java/com/jagrosh/giveawaybot/commands/ListCmd.java index 8b3a8567..7cebcce9 100644 --- a/src/main/java/com/jagrosh/giveawaybot/commands/ListCmd.java +++ b/src/main/java/com/jagrosh/giveawaybot/commands/ListCmd.java @@ -21,9 +21,7 @@ import com.jagrosh.giveawaybot.entities.LocalizedMessage; import com.jagrosh.giveawaybot.util.FormatUtil; import com.jagrosh.interactions.command.ApplicationCommand; -import com.jagrosh.interactions.entities.AllowedMentions; -import com.jagrosh.interactions.entities.Permission; -import com.jagrosh.interactions.entities.SentMessage; +import com.jagrosh.interactions.entities.*; import com.jagrosh.interactions.receive.Interaction; import com.jagrosh.interactions.responses.InteractionResponse; import com.jagrosh.interactions.responses.MessageCallback; @@ -37,6 +35,8 @@ */ public class ListCmd extends GBCommand { + private final static int MAX_LENGTH = 6000; + public ListCmd(GiveawayBot bot) { super(bot); @@ -52,26 +52,45 @@ public ListCmd(GiveawayBot bot) @Override protected InteractionResponse gbExecute(Interaction interaction) throws GiveawayException { - //bot.getGiveawayManager().checkPermission(interaction.getMember(), interaction.getGuildId()); - List list = bot.getDatabase().getGiveawaysByGuild(interaction.getGuildId()); if(list.isEmpty()) return respondError(LocalizedMessage.WARNING_NO_GIVEAWAYS.getLocalizedMessage(interaction.getEffectiveLocale())); - //Color c = bot.getDatabase().getSettings(interaction.getGuildId()).getColor(); - StringBuilder sb = new StringBuilder("**Active Giveaways**\n"); - list.forEach(giv -> + + WebLocale loc = interaction.getEffectiveLocale(); + String title = LocalizedMessage.GIVEAWAY_LIST.getLocalizedMessage(interaction.getEffectiveLocale()); + int total = title.length(); + + Embed.Builder eb = new Embed.Builder(); + eb.setColor(bot.getDatabase().getSettings(interaction.getGuildId()).getColor()); + eb.setTitle(title, null); + + for(Giveaway giv: list) { - sb.append("\n[`").append(giv.getMessageId()).append("`](").append(giv.getJumpLink()).append(") | <#").append(giv.getChannelId()).append("> | **").append(giv.getWinners()) - .append("** ").append(FormatUtil.pluralise(giv.getWinners(), "winner", "winners")).append(" | ") - .append(giv.getPrize() == null || giv.getPrize().isEmpty() ? "No prize specified" : "Prize: **" + giv.getPrize() + "**").append(" | ") - .append("Host: <@").append(giv.getUserId()).append("> | ") - .append("Ends in ").append(FormatUtil.secondsToTime(Instant.now().until(giv.getEndInstant(), ChronoUnit.SECONDS))); - - }); + String key = giv.getPrize().isEmpty() ? "Giveaway" : giv.getPrize(); + String val = renderGiveawayString(giv, loc); + if(total + key.length() + val.length() >= MAX_LENGTH - 4) + { + eb.setFooter("...", null); + break; + } + eb.addField(key, val, true); + total += key.length() + val.length(); + } return new MessageCallback(new SentMessage.Builder() - .setContent(sb.toString()) + //.setContent(sb.toString()) .setAllowedMentions(new AllowedMentions(true)) - //.addEmbed(new Embed.Builder().setTitle("Active Giveaways", null).setColor(c).setDescription(sb.toString()).build()) + .addEmbed(eb.build()) .build()); } + + private String renderGiveawayString(Giveaway giv, WebLocale loc) + { + return new StringBuilder() + .append("[`").append(giv.getMessageId()).append("`](").append(giv.getJumpLink()).append(") | <#").append(giv.getChannelId()).append("> | ") + //.append(giv.getPrize().isEmpty() ? "" : "**" + giv.getPrize() + "** | ") + .append(LocalizedMessage.GIVEAWAY_WINNERS.getLocalizedMessage(loc)).append(": **").append(giv.getWinners()).append("** | ") + .append(LocalizedMessage.GIVEAWAY_HOSTED.getLocalizedMessage(loc)).append(": <@").append(giv.getUserId()).append("> | ") + .append(LocalizedMessage.GIVEAWAY_ENDS.getLocalizedMessage(loc)).append(": ").append(FormatUtil.secondsToTime(Instant.now().until(giv.getEndInstant(), ChronoUnit.SECONDS))) + .toString(); + } } diff --git a/src/main/java/com/jagrosh/giveawaybot/data/Database.java b/src/main/java/com/jagrosh/giveawaybot/data/Database.java index c73518ca..fdb7beb7 100644 --- a/src/main/java/com/jagrosh/giveawaybot/data/Database.java +++ b/src/main/java/com/jagrosh/giveawaybot/data/Database.java @@ -40,6 +40,7 @@ public class Database private final EntityManagerFactory emf; private final EntityManager em; private final Map cachedEntries = new HashMap<>(); + private final Map cachedUsers = new HashMap<>(); private final Map cachedGiveawaysReadonly = new HashMap<>(); private final ScheduledExecutorService cacheCombiner = Executors.newSingleThreadScheduledExecutor(); @@ -54,13 +55,13 @@ public Database(String host, String user, String pass) em.getMetamodel().managedType(Giveaway.class); em.getMetamodel().managedType(GiveawayEntries.class); em.getMetamodel().managedType(GuildSettings.class); - cacheCombiner.scheduleWithFixedDelay(() -> syncEntries(), 60, 60, TimeUnit.SECONDS); + cacheCombiner.scheduleWithFixedDelay(() -> syncCaches(), 60, 60, TimeUnit.SECONDS); } public void shutdown() { cacheCombiner.shutdown(); - syncEntries(); + syncCaches(); em.close(); emf.close(); } @@ -204,32 +205,19 @@ public synchronized void removeGiveaway(long id) // entries public synchronized void updateUser(User user) { - // update cached user - CachedUser u = em.find(CachedUser.class, user.getIdLong()); - - // short circuit if data is up to date - if(u != null - && OtherUtil.strEquals(user.getUsername(), u.getUsername()) - && OtherUtil.strEquals(user.getDiscriminator(), u.getDiscriminator()) - && OtherUtil.strEquals(user.getAvatar(), u.getAvatar())) - return; - - em.getTransaction().begin(); - if(u == null) - { - u = new CachedUser(); - u.setId(user.getIdLong()); - em.persist(u); - } + // construct a cached user object and cache immediately + // this will reach the database when we sync + CachedUser u = new CachedUser(); + u.setId(user.getIdLong()); u.setUsername(user.getUsername()); u.setDiscriminator(user.getDiscriminator()); u.setAvatar(user.getAvatar()); - em.getTransaction().commit(); + cachedUsers.put(user.getIdLong(), u); } public CachedUser getUser(long userId) { - return em.find(CachedUser.class, userId); + return cachedUsers.containsKey(userId) ? cachedUsers.get(userId) : em.find(CachedUser.class, userId); } public synchronized int addEntry(long giveawayId, User user) @@ -276,12 +264,34 @@ public synchronized boolean removeEntry(long giveawayId, User user) return true; } - public synchronized void syncEntries() + public synchronized void syncCaches() { + // open transaction em.getTransaction().begin(); + + // update entries cachedEntries.values().forEach(e -> em.merge(e)); + + // update users + cachedUsers.values().forEach(user -> + { + CachedUser u = em.find(CachedUser.class, user.getId()); + if(u == null) + { + em.persist(user); + } + else + { + u.setUsername(user.getUsername()); + u.setDiscriminator(user.getDiscriminator()); + u.setAvatar(user.getAvatar()); + } + }); + + // commit the transaction em.getTransaction().commit(); cachedEntries.clear(); + cachedUsers.clear(); } public List getEntriesList(long giveawayId) diff --git a/src/main/java/com/jagrosh/giveawaybot/entities/LocalizedMessage.java b/src/main/java/com/jagrosh/giveawaybot/entities/LocalizedMessage.java index 0494049d..76e65b01 100644 --- a/src/main/java/com/jagrosh/giveawaybot/entities/LocalizedMessage.java +++ b/src/main/java/com/jagrosh/giveawaybot/entities/LocalizedMessage.java @@ -112,7 +112,8 @@ public enum LocalizedMessage GIVEAWAY_HOSTED("giveaway.hosted"), GIVEAWAY_ENTRIES("giveaway.entries"), GIVEAWAY_WINNERS("giveaway.winners"), - GIVEAWAY_SUMMARY("giveaway.summary"); + GIVEAWAY_SUMMARY("giveaway.summary"), + GIVEAWAY_LIST("giveaway.list"); private final static String FILENAME = "localization/messages"; private final String key; diff --git a/src/main/java/com/jagrosh/giveawaybot/entities/Uptimer.java b/src/main/java/com/jagrosh/giveawaybot/entities/Uptimer.java index 9e2565b0..baedcddd 100644 --- a/src/main/java/com/jagrosh/giveawaybot/entities/Uptimer.java +++ b/src/main/java/com/jagrosh/giveawaybot/entities/Uptimer.java @@ -52,7 +52,7 @@ public void shutdown() private void check() { double req = bot.getInteractionsClient().getMetrics().getOrDefault("TotalTime", 0L) / bot.getInteractionsClient().getMetrics().getOrDefault("TotalRequests", 1L) * 1e-9; - if(req > 1) + if(req > .5) bot.shutdown("AUTOMATIC: avg req = " + req); } } diff --git a/src/main/resources/localization/messages.properties b/src/main/resources/localization/messages.properties index 82e3ff19..352d68dd 100644 --- a/src/main/resources/localization/messages.properties +++ b/src/main/resources/localization/messages.properties @@ -80,4 +80,5 @@ giveaway.hosted = Hosted by giveaway.entries = Entries giveaway.winners = Winners giveaway.summary = Giveaway Summary +giveaway.list = Active Giveaways