From ece36f7f27758a06f5c378d0e45414d781d3bc84 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 03:40:12 +0100 Subject: [PATCH] maybe fix loading log channels --- .../cc/fascinated/bat/BatApplication.java | 1 - .../fascinated/bat/common/ChannelUtils.java | 41 +++++++++++++++++++ .../bat/features/logging/LogProfile.java | 5 +-- .../welcomer/command/EmbedSubCommand.java | 2 - .../java/cc/fascinated/bat/model/BatUser.java | 4 +- .../bat/service/CommandService.java | 7 +++- 6 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/ChannelUtils.java diff --git a/src/main/java/cc/fascinated/bat/BatApplication.java b/src/main/java/cc/fascinated/bat/BatApplication.java index d55bf03..90c77cb 100644 --- a/src/main/java/cc/fascinated/bat/BatApplication.java +++ b/src/main/java/cc/fascinated/bat/BatApplication.java @@ -6,7 +6,6 @@ import cc.fascinated.bat.service.EventService; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.mongock.runner.springboot.EnableMongock; -import jakarta.annotation.PreDestroy; import lombok.NonNull; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; diff --git a/src/main/java/cc/fascinated/bat/common/ChannelUtils.java b/src/main/java/cc/fascinated/bat/common/ChannelUtils.java new file mode 100644 index 0000000..0b3ee77 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/ChannelUtils.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.channel.concrete.TextChannel; + +/** + * @author Fascinated (fascinated7) + */ +@Log4j2 +public class ChannelUtils { + /** + * 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 TextChannel getTextChannel(String id, int retries) { + if (retries >= 25) { + log.error("Failed to find user \"{}\" after {} retries.", id, retries); + return null; + } + TextChannel channel = DiscordService.JDA.getTextChannelById(id); + if (channel == null) { + return getTextChannel(id, retries + 1); + } + log.info("Found text channel \"{}\" after {} retries.", channel.getName(), retries); + return channel; + } + + /** + * Gets the user with the given id + * + * @param id the id of the user + * @return the user with the given id + */ + public static TextChannel getTextChannel(String id) { + return getTextChannel(id, 0); + } +} diff --git a/src/main/java/cc/fascinated/bat/features/logging/LogProfile.java b/src/main/java/cc/fascinated/bat/features/logging/LogProfile.java index e40e657..a3d7db4 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogProfile.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogProfile.java @@ -1,9 +1,9 @@ package cc.fascinated.bat.features.logging; +import cc.fascinated.bat.common.ChannelUtils; import cc.fascinated.bat.common.Serializable; import cc.fascinated.bat.service.DiscordService; import com.google.gson.Gson; -import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import org.bson.Document; @@ -69,10 +69,9 @@ public class LogProfile extends Serializable { @Override public void load(Document document, Gson gson) { - JDA jda = DiscordService.JDA; for (LogType logType : LogType.values()) { if (document.containsKey(logType.name())) { - TextChannel channel = jda.getTextChannelById(document.getString(logType.name())); + TextChannel channel = ChannelUtils.getTextChannel(document.getString(logType.name())); if (channel == null) { return; } diff --git a/src/main/java/cc/fascinated/bat/features/welcomer/command/EmbedSubCommand.java b/src/main/java/cc/fascinated/bat/features/welcomer/command/EmbedSubCommand.java index 61eea35..3390d71 100644 --- a/src/main/java/cc/fascinated/bat/features/welcomer/command/EmbedSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/welcomer/command/EmbedSubCommand.java @@ -17,8 +17,6 @@ import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import org.springframework.stereotype.Component; -import java.awt.*; - /** * @author Fascinated (fascinated7) */ diff --git a/src/main/java/cc/fascinated/bat/model/BatUser.java b/src/main/java/cc/fascinated/bat/model/BatUser.java index 19d519d..83e10a2 100644 --- a/src/main/java/cc/fascinated/bat/model/BatUser.java +++ b/src/main/java/cc/fascinated/bat/model/BatUser.java @@ -63,9 +63,9 @@ public class BatUser extends ProfileHolder { // User was not passed through if (user == null) { user = UserUtils.getUser(id); - this.user = user; - this.globalName = user.getGlobalName(); } + this.user = user; + this.globalName = user.getGlobalName(); } /** diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index 111f43f..ad660c7 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -184,6 +184,7 @@ public class CommandService extends ListenerAdapter { } } + // Check if the executor is null if (executor == null) { event.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("Unable to find a command executor the command name ):") @@ -214,6 +215,7 @@ public class CommandService extends ListenerAdapter { } } + // Check if the command is guild only and if it was not ran inside a guild if (isSubCommand && commandInfo.guildOnly() && !ranInsideGuild) { event.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("This command can only be executed in a guild") @@ -221,6 +223,7 @@ public class CommandService extends ListenerAdapter { return; } + // Check if the feature is disabled in the guild if (guild != null) { FeatureProfile featureProfile = guild.getFeatureProfile(); if (featureProfile.isFeatureDisabled(command.getFeature())) { @@ -231,10 +234,10 @@ public class CommandService extends ListenerAdapter { } } + // Execute the command executor.execute(guild, user, ranInsideGuild ? event.getChannel().asTextChannel() : event.getChannel().asPrivateChannel(), event.getMember(), event.getInteraction()); - log.info("Executed command \"{}\" for user \"{}\" (took: {}ms)", commandName, user.getDiscordUser().getName(), - System.currentTimeMillis() - before); + log.info("Executed command \"{}\" for user \"{}\" (took: {}ms)", commandName, user.getName(), System.currentTimeMillis() - before); } catch (Exception ex) { log.error("An error occurred while executing command \"{}\"", commandName, ex); event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue();