From 8dd6c760dc02c0723d79628e3e66b58dc7689ba8 Mon Sep 17 00:00:00 2001 From: SebClem Date: Mon, 23 Nov 2020 14:29:44 +0100 Subject: [PATCH] :ambulance: Fix cache for stats --- .../java/net/Broken/Tools/CacheTools.java | 19 +++++++++++++++++-- .../UserManager/Stats/UserStatsUtils.java | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/Broken/Tools/CacheTools.java b/src/main/java/net/Broken/Tools/CacheTools.java index 24a5d23..6af1952 100644 --- a/src/main/java/net/Broken/Tools/CacheTools.java +++ b/src/main/java/net/Broken/Tools/CacheTools.java @@ -1,15 +1,30 @@ package net.Broken.Tools; +import net.Broken.DB.Entity.UserEntity; import net.Broken.MainBot; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.User; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.List; public class CacheTools { - public static void loadAllGuildMembers(){ + private static Logger logger = LogManager.getLogger(); + + public static void loadAllGuildMembers() { List guilds = MainBot.jda.getGuilds(); - for(Guild guild : guilds){ + for (Guild guild : guilds) { guild.loadMembers().get(); } } + + public static User getJdaUser(UserEntity userEntity) { + User user = MainBot.jda.getUserById(userEntity.getJdaId()); + if (user == null) { + logger.debug("User cache not found for " + userEntity.getName() + ", fetching user."); + user = MainBot.jda.retrieveUserById(userEntity.getJdaId()).complete(); + } + return user; + } } diff --git a/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java b/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java index deb1ae4..c59a98f 100644 --- a/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java +++ b/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java @@ -6,6 +6,7 @@ import net.Broken.DB.Repository.UserRepository; import net.Broken.DB.Repository.UserStatsRepository; import net.Broken.MainBot; import net.Broken.SpringContext; +import net.Broken.Tools.CacheTools; import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; import net.Broken.Tools.UserManager.UserUtils; @@ -58,7 +59,7 @@ public class UserStatsUtils { } public List getUserStats(UserEntity userEntity) { - User jdaUser = MainBot.jda.getUserById(userEntity.getJdaId()); + User jdaUser = CacheTools.getJdaUser(userEntity); //TODO clean database for deleted users if (jdaUser == null){