From 5ad1d9e891702d7733ee59329950086e75dcf1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sat, 6 Sep 2025 21:44:55 +0200 Subject: [PATCH] start moving to @Components --- .../wtf/beatrice/hidekobot/HidekoBot.java | 15 ++-- .../hidekobot/commands/base/Alias.java | 8 +-- .../hidekobot/commands/base/BotInfo.java | 8 +-- ...sCommand.java => MessageAliasCommand.java} | 14 +++- ...ommand.java => MessageBotInfoCommand.java} | 14 +++- ...oCommand.java => MessageHelloCommand.java} | 4 +- ...lpCommand.java => MessageHelpCommand.java} | 13 +++- ...oCommand.java => SlashBotInfoCommand.java} | 15 +++- .../wtf/beatrice/hidekobot/util/Logger.java | 71 ------------------- 9 files changed, 66 insertions(+), 96 deletions(-) rename src/main/java/wtf/beatrice/hidekobot/commands/message/{AliasCommand.java => MessageAliasCommand.java} (84%) rename src/main/java/wtf/beatrice/hidekobot/commands/message/{BotInfoCommand.java => MessageBotInfoCommand.java} (82%) rename src/main/java/wtf/beatrice/hidekobot/commands/message/{HelloCommand.java => MessageHelloCommand.java} (91%) rename src/main/java/wtf/beatrice/hidekobot/commands/message/{HelpCommand.java => MessageHelpCommand.java} (94%) rename src/main/java/wtf/beatrice/hidekobot/commands/slash/{BotInfoCommand.java => SlashBotInfoCommand.java} (79%) delete mode 100644 src/main/java/wtf/beatrice/hidekobot/util/Logger.java diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index f0eab9b..f4307fe 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -11,7 +11,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.system.ApplicationHome; import org.springframework.context.ConfigurableApplicationContext; import wtf.beatrice.hidekobot.commands.completer.ProfileImageCommandCompleter; -import wtf.beatrice.hidekobot.commands.message.HelloCommand; +import wtf.beatrice.hidekobot.commands.message.MessageAliasCommand; +import wtf.beatrice.hidekobot.commands.message.MessageBotInfoCommand; +import wtf.beatrice.hidekobot.commands.message.MessageHelloCommand; +import wtf.beatrice.hidekobot.commands.message.MessageHelpCommand; import wtf.beatrice.hidekobot.commands.slash.*; import wtf.beatrice.hidekobot.datasources.ConfigurationSource; import wtf.beatrice.hidekobot.datasources.PropertiesSource; @@ -159,7 +162,7 @@ public class HidekoBot ProfileImageCommandCompleter bannerCommandCompleter = new ProfileImageCommandCompleter(bannerCommand); slashCommandListener.registerCommand(bannerCommand); slashCommandCompletionListener.registerCommandCompleter(bannerCommandCompleter); - slashCommandListener.registerCommand(new BotInfoCommand()); + slashCommandListener.registerCommand(context.getBean(SlashBotInfoCommand.class)); slashCommandListener.registerCommand(new ClearCommand()); slashCommandListener.registerCommand(new CoinFlipCommand()); slashCommandListener.registerCommand(new DiceRollCommand()); @@ -176,16 +179,16 @@ public class HidekoBot slashCommandListener.registerCommand(new UrbanDictionaryCommand()); // register message commands - messageCommandListener.registerCommand(new HelloCommand()); - messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.AliasCommand()); + messageCommandListener.registerCommand(new MessageHelloCommand()); + messageCommandListener.registerCommand(context.getBean(MessageAliasCommand.class)); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.AvatarCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BanCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BannerCommand()); - messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BotInfoCommand()); + messageCommandListener.registerCommand(context.getBean(MessageBotInfoCommand.class)); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.ClearCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.DiceRollCommand()); - messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.HelpCommand()); + messageCommandListener.registerCommand(context.getBean(MessageHelpCommand.class)); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.InviteCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.KickCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.LoveCalculatorCommand()); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/Alias.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/Alias.java index 55ec155..449a5d4 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/Alias.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/Alias.java @@ -1,17 +1,15 @@ package wtf.beatrice.hidekobot.commands.base; +import org.springframework.stereotype.Component; import wtf.beatrice.hidekobot.objects.commands.MessageCommand; import java.util.LinkedList; +@Component public class Alias { - private Alias() - { - throw new IllegalStateException("Utility class"); - } - public static String generateNiceAliases(MessageCommand command) + public String generateNiceAliases(MessageCommand command) { LinkedList aliases = command.getCommandLabels(); StringBuilder aliasesStringBuilder = new StringBuilder(); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/BotInfo.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/BotInfo.java index 977421b..6167ffd 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/BotInfo.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/BotInfo.java @@ -2,6 +2,7 @@ package wtf.beatrice.hidekobot.commands.base; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; +import org.springframework.stereotype.Component; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; import wtf.beatrice.hidekobot.util.FormatUtil; @@ -11,14 +12,11 @@ import java.lang.management.ManagementFactory; import java.text.DecimalFormat; import java.util.List; +@Component public class BotInfo { - private BotInfo() - { - throw new IllegalStateException("Utility class"); - } - public static MessageEmbed generateEmbed(List commandLabels) + public MessageEmbed generateEmbed(List commandLabels) { EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setColor(Cache.getBotColor()); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/AliasCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/MessageAliasCommand.java similarity index 84% rename from src/main/java/wtf/beatrice/hidekobot/commands/message/AliasCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/message/MessageAliasCommand.java index c697f97..7a9b04e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/AliasCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/MessageAliasCommand.java @@ -4,6 +4,8 @@ import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.commands.base.Alias; import wtf.beatrice.hidekobot.objects.commands.CommandCategory; @@ -13,9 +15,17 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; -public class AliasCommand implements MessageCommand +@Component +public class MessageAliasCommand implements MessageCommand { + private final Alias alias; + + public MessageAliasCommand(@Autowired Alias alias) + { + this.alias = alias; + } + @Override public LinkedList getCommandLabels() { @@ -73,7 +83,7 @@ public class AliasCommand implements MessageCommand return; } - String aliases = Alias.generateNiceAliases(command); + String aliases = alias.generateNiceAliases(command); aliases = "Aliases for **" + command.getCommandLabels().get(0) + "**: " + aliases; event.getMessage() diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/MessageBotInfoCommand.java similarity index 82% rename from src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/message/MessageBotInfoCommand.java index a56b1ee..4ae1076 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/MessageBotInfoCommand.java @@ -5,6 +5,8 @@ import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.commands.base.BotInfo; import wtf.beatrice.hidekobot.objects.commands.CommandCategory; @@ -14,9 +16,17 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; -public class BotInfoCommand implements MessageCommand +@Component +public class MessageBotInfoCommand implements MessageCommand { + private final BotInfo botInfo; + + public MessageBotInfoCommand(@Autowired BotInfo botInfo) + { + this.botInfo = botInfo; + } + @Override public LinkedList getCommandLabels() { @@ -70,7 +80,7 @@ public class BotInfoCommand implements MessageCommand } // send the list - MessageEmbed embed = BotInfo.generateEmbed(commandNames); + MessageEmbed embed = botInfo.generateEmbed(commandNames); event.getMessage().replyEmbeds(embed).queue(); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/HelloCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/MessageHelloCommand.java similarity index 91% rename from src/main/java/wtf/beatrice/hidekobot/commands/message/HelloCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/message/MessageHelloCommand.java index fdd84bf..80e4ec6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/HelloCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/MessageHelloCommand.java @@ -4,6 +4,7 @@ import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.springframework.stereotype.Component; import wtf.beatrice.hidekobot.objects.commands.CommandCategory; import wtf.beatrice.hidekobot.objects.commands.MessageCommand; @@ -11,7 +12,8 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; -public class HelloCommand implements MessageCommand +@Component +public class MessageHelloCommand implements MessageCommand { @Override diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/HelpCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/MessageHelpCommand.java similarity index 94% rename from src/main/java/wtf/beatrice/hidekobot/commands/message/HelpCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/message/MessageHelpCommand.java index 70a80ff..37cae87 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/HelpCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/MessageHelpCommand.java @@ -6,6 +6,8 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.apache.commons.text.WordUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.commands.base.Alias; import wtf.beatrice.hidekobot.objects.commands.CommandCategory; @@ -13,9 +15,16 @@ import wtf.beatrice.hidekobot.objects.commands.MessageCommand; import java.util.*; -public class HelpCommand implements MessageCommand +@Component +public class MessageHelpCommand implements MessageCommand { + private final Alias alias; + + public MessageHelpCommand(@Autowired Alias alias) + { + this.alias = alias; + } @Override public LinkedList getCommandLabels() @@ -127,7 +136,7 @@ public class HelpCommand implements MessageCommand if (internalUsage != null) usage += " " + internalUsage; usage += "`"; - String aliases = Alias.generateNiceAliases(command); + String aliases = alias.generateNiceAliases(command); List permissions = command.getPermissions(); StringBuilder permissionsStringBuilder = new StringBuilder(); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/SlashBotInfoCommand.java similarity index 79% rename from src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/slash/SlashBotInfoCommand.java index 1a65ef2..c16cd1b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/SlashBotInfoCommand.java @@ -5,6 +5,8 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; import org.jetbrains.annotations.NotNull; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.commands.base.BotInfo; import wtf.beatrice.hidekobot.objects.commands.SlashCommand; @@ -13,8 +15,17 @@ import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl; import java.util.LinkedList; import java.util.List; -public class BotInfoCommand extends SlashCommandImpl +@Component +public class SlashBotInfoCommand extends SlashCommandImpl { + private final BotInfo botInfo; + + public SlashBotInfoCommand(@Autowired BotInfo botInfo) + { + this.botInfo = botInfo; + } + + @Override public CommandData getSlashCommandData() { @@ -37,7 +48,7 @@ public class BotInfoCommand extends SlashCommandImpl } // send the list - MessageEmbed embed = BotInfo.generateEmbed(registeredCommandNames); + MessageEmbed embed = botInfo.generateEmbed(registeredCommandNames); event.getHook().editOriginalEmbeds(embed).queue(); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/util/Logger.java b/src/main/java/wtf/beatrice/hidekobot/util/Logger.java deleted file mode 100644 index 0afeb36..0000000 --- a/src/main/java/wtf/beatrice/hidekobot/util/Logger.java +++ /dev/null @@ -1,71 +0,0 @@ -package wtf.beatrice.hidekobot.util; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -@Deprecated(since = "0.5.16", forRemoval = true) -public class Logger -{ - - // objects that we need to have for a properly formatted message - private final String className; - private final String format = "[%date% %time%] [%class%] %message%"; - private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - private final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); - - - // when initializing a new logger, save variables in that instance - public Logger(Class logClass) - { - className = logClass.getSimpleName(); - } - - /** - * Logs a message to console, following a specific format. - * - * @param message the message to log - */ - public void log(String message) - { - LocalDateTime now = LocalDateTime.now(); - String currentDate = dateFormatter.format(now); - String currentTime = timeFormatter.format(now); - logRaw(format - .replace("%date%", currentDate) - .replace("%time%", currentTime) - .replace("%class%", className) - .replace("%message%", message)); - } - - /** - * Logs a message to console, after delaying it. - * - * @param message the message to log - * @param delay the time to wait before logging, in seconds - */ - public void log(String message, int delay) - { - // create a new scheduled executor with an anonymous runnable... - //... after waiting seconds. - try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor()) - { - executor.schedule(() -> log(message), delay, TimeUnit.SECONDS); - } - - } - - /** - * Prints a message to console without any formatting. - * - * @param message the message to log - */ - public void logRaw(String message) - { - System.out.println(message); - } - - -}