maybe fix loading log channels

This commit is contained in:
Lee 2024-07-04 03:40:12 +01:00
parent 3b3ea2b3cc
commit ece36f7f27
6 changed files with 50 additions and 10 deletions

@ -6,7 +6,6 @@ import cc.fascinated.bat.service.EventService;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import io.mongock.runner.springboot.EnableMongock; import io.mongock.runner.springboot.EnableMongock;
import jakarta.annotation.PreDestroy;
import lombok.NonNull; import lombok.NonNull;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;

@ -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);
}
}

@ -1,9 +1,9 @@
package cc.fascinated.bat.features.logging; package cc.fascinated.bat.features.logging;
import cc.fascinated.bat.common.ChannelUtils;
import cc.fascinated.bat.common.Serializable; import cc.fascinated.bat.common.Serializable;
import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.DiscordService;
import com.google.gson.Gson; import com.google.gson.Gson;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.bson.Document; import org.bson.Document;
@ -69,10 +69,9 @@ public class LogProfile extends Serializable {
@Override @Override
public void load(Document document, Gson gson) { public void load(Document document, Gson gson) {
JDA jda = DiscordService.JDA;
for (LogType logType : LogType.values()) { for (LogType logType : LogType.values()) {
if (document.containsKey(logType.name())) { if (document.containsKey(logType.name())) {
TextChannel channel = jda.getTextChannelById(document.getString(logType.name())); TextChannel channel = ChannelUtils.getTextChannel(document.getString(logType.name()));
if (channel == null) { if (channel == null) {
return; return;
} }

@ -17,8 +17,6 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.awt.*;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */

@ -63,9 +63,9 @@ public class BatUser extends ProfileHolder {
// User was not passed through // User was not passed through
if (user == null) { if (user == null) {
user = UserUtils.getUser(id); user = UserUtils.getUser(id);
this.user = user;
this.globalName = user.getGlobalName();
} }
this.user = user;
this.globalName = user.getGlobalName();
} }
/** /**

@ -184,6 +184,7 @@ public class CommandService extends ListenerAdapter {
} }
} }
// Check if the executor is null
if (executor == null) { if (executor == null) {
event.replyEmbeds(EmbedUtils.errorEmbed() event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("Unable to find a command executor the command name ):") .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) { if (isSubCommand && commandInfo.guildOnly() && !ranInsideGuild) {
event.replyEmbeds(EmbedUtils.errorEmbed() event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("This command can only be executed in a guild") .setDescription("This command can only be executed in a guild")
@ -221,6 +223,7 @@ public class CommandService extends ListenerAdapter {
return; return;
} }
// Check if the feature is disabled in the guild
if (guild != null) { if (guild != null) {
FeatureProfile featureProfile = guild.getFeatureProfile(); FeatureProfile featureProfile = guild.getFeatureProfile();
if (featureProfile.isFeatureDisabled(command.getFeature())) { 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(), executor.execute(guild, user, ranInsideGuild ? event.getChannel().asTextChannel() : event.getChannel().asPrivateChannel(),
event.getMember(), event.getInteraction()); event.getMember(), event.getInteraction());
log.info("Executed command \"{}\" for user \"{}\" (took: {}ms)", commandName, user.getDiscordUser().getName(), log.info("Executed command \"{}\" for user \"{}\" (took: {}ms)", commandName, user.getName(), System.currentTimeMillis() - before);
System.currentTimeMillis() - before);
} catch (Exception ex) { } catch (Exception ex) {
log.error("An error occurred while executing command \"{}\"", commandName, ex); log.error("An error occurred while executing command \"{}\"", commandName, ex);
event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue(); event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue();