From dc7e44239f6a7955d9f352e904d6d739afb183cf Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 01:33:24 +0100 Subject: [PATCH] fix npe and maybe fix null users???????? --- .../cc/fascinated/bat/common/UserUtils.java | 41 +++++++++++++++++++ .../bat/features/drag/DragRequest.java | 1 - .../features/drag/command/DragCommand.java | 1 - .../bat/features/tmdb/TMDBFeature.java | 4 +- .../java/cc/fascinated/bat/model/BatUser.java | 3 +- 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/UserUtils.java diff --git a/src/main/java/cc/fascinated/bat/common/UserUtils.java b/src/main/java/cc/fascinated/bat/common/UserUtils.java new file mode 100644 index 0000000..d7e11b2 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/UserUtils.java @@ -0,0 +1,41 @@ +package cc.fascinated.bat.common; + +import cc.fascinated.bat.service.DiscordService; +import lombok.extern.log4j.Log4j2; +import net.dv8tion.jda.api.entities.User; + +/** + * @author Fascinated (fascinated7) + */ +@Log4j2 +public class UserUtils { + /** + * Gets the user with the given id + * + * @param id the id of the user + * @param retries the amount of retries + * @return the user with the given id + */ + private static User getUser(String id, int retries) { + if (retries >= 25) { + log.error("Failed to find user \"{}\" after {} retries.", id, retries); + return null; + } + User user = DiscordService.JDA.getUserById(id); + if (user == null) { + return getUser(id, retries + 1); + } + log.info("Found user \"{}\" after {} retries.", id, retries); + return user; + } + + /** + * Gets the user with the given id + * + * @param id the id of the user + * @return the user with the given id + */ + public static User getUser(String id) { + return getUser(id, 0); + } +} diff --git a/src/main/java/cc/fascinated/bat/features/drag/DragRequest.java b/src/main/java/cc/fascinated/bat/features/drag/DragRequest.java index 4372f41..26bf4ba 100644 --- a/src/main/java/cc/fascinated/bat/features/drag/DragRequest.java +++ b/src/main/java/cc/fascinated/bat/features/drag/DragRequest.java @@ -1,6 +1,5 @@ package cc.fascinated.bat.features.drag; -import lombok.AllArgsConstructor; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; diff --git a/src/main/java/cc/fascinated/bat/features/drag/command/DragCommand.java b/src/main/java/cc/fascinated/bat/features/drag/command/DragCommand.java index f4a7fe9..a832c18 100644 --- a/src/main/java/cc/fascinated/bat/features/drag/command/DragCommand.java +++ b/src/main/java/cc/fascinated/bat/features/drag/command/DragCommand.java @@ -2,7 +2,6 @@ package cc.fascinated.bat.features.drag.command; import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.CommandInfo; -import io.sentry.protocol.App; import lombok.NonNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; diff --git a/src/main/java/cc/fascinated/bat/features/tmdb/TMDBFeature.java b/src/main/java/cc/fascinated/bat/features/tmdb/TMDBFeature.java index 3e26136..73013e5 100644 --- a/src/main/java/cc/fascinated/bat/features/tmdb/TMDBFeature.java +++ b/src/main/java/cc/fascinated/bat/features/tmdb/TMDBFeature.java @@ -45,7 +45,7 @@ public class TMDBFeature extends Feature { public static EmbedBuilder pageMovie(@NonNull TMDBService tmdbService, @NonNull String query, String language, String primaryReleaseYear, String region, String year, int movie, boolean adult) { List movieList = tmdbService.lookupMovies(query, adult, language, primaryReleaseYear, region, year); - if (movieList.isEmpty()) { + if (movieList == null || movieList.isEmpty()) { return EmbedUtils.errorEmbed() .setDescription("No movieList found with the provided query + options!"); } @@ -80,7 +80,7 @@ public class TMDBFeature extends Feature { public static EmbedBuilder pageSeries(@NonNull TMDBService tmdbService, @NonNull String query, String language, int firstAirDateYear, int year, int series, boolean adult) { List seriesList = tmdbService.lookupSeries(query, adult, language, firstAirDateYear, year); - if (seriesList.isEmpty()) { + if (seriesList == null || seriesList.isEmpty()) { return EmbedUtils.errorEmbed() .setDescription("No series found with the provided query + options!"); } diff --git a/src/main/java/cc/fascinated/bat/model/BatUser.java b/src/main/java/cc/fascinated/bat/model/BatUser.java index 9e9808f..3e552b2 100644 --- a/src/main/java/cc/fascinated/bat/model/BatUser.java +++ b/src/main/java/cc/fascinated/bat/model/BatUser.java @@ -3,6 +3,7 @@ package cc.fascinated.bat.model; import cc.fascinated.bat.BatApplication; import cc.fascinated.bat.common.ProfileHolder; import cc.fascinated.bat.common.Serializable; +import cc.fascinated.bat.common.UserUtils; import cc.fascinated.bat.features.namehistory.profile.user.NameHistoryProfile; import cc.fascinated.bat.features.scoresaber.profile.user.ScoreSaberProfile; import cc.fascinated.bat.service.DiscordService; @@ -59,7 +60,7 @@ public class BatUser extends ProfileHolder { boolean newAccount = this.document.isEmpty(); this.createdAt = newAccount ? new Date() : document.getDate("createdAt"); - User user = DiscordService.JDA.getUserById(id); + User user = UserUtils.getUser(id); if (user != null) { this.user = user; this.globalName = user.getGlobalName();