From 71d3cb8e0ec53d71a154b0fd1ff167588cafb134 Mon Sep 17 00:00:00 2001 From: beanbeanjuice Date: Thu, 20 Jun 2024 08:26:07 -0400 Subject: [PATCH] Refactored the Interaction Component --- .../section/interaction/Interaction.java | 68 ++++++------- .../interaction/InteractionHandler.java | 97 ++++++++----------- .../utility/section/moderation/poll/Poll.java | 1 + .../section/moderation/raffle/Raffle.java | 1 + .../utility/section/twitch/TwitchHandler.java | 1 - .../section/twitch/TwitchListener.java | 1 - .../twitch/TwitchMessageEventHandler.java | 1 - 7 files changed, 73 insertions(+), 97 deletions(-) diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/section/interaction/Interaction.java b/src/main/java/com/beanbeanjuice/cafebot/utility/section/interaction/Interaction.java index 25a24dd8..ea3703a7 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/section/interaction/Interaction.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/section/interaction/Interaction.java @@ -8,7 +8,6 @@ import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -17,8 +16,8 @@ * @author beanbeanjuice */ public class Interaction { - private final InteractionType type; + private final InteractionType type; private final String noUserAction;; private final String userAction; private String footer; @@ -39,8 +38,8 @@ public class Interaction { * @param selfMessage The {@link String message} to send to the user, if the receiver is the {@link Bot}. * @param event The {@link SlashCommandInteractionEvent event} that triggered the {@link Interaction}. */ - public Interaction(@NotNull InteractionType type, @NotNull String noUserAction, @NotNull String userAction, - @NotNull String footer, @NotNull String selfMessage, @NotNull SlashCommandInteractionEvent event) { + public Interaction(final InteractionType type, final String noUserAction, final String userAction, + final String footer, final String selfMessage, final SlashCommandInteractionEvent event) { this.type = type; this.noUserAction = noUserAction; this.userAction = userAction; @@ -56,22 +55,20 @@ public Interaction(@NotNull InteractionType type, @NotNull String noUserAction, this.receiver = null; try { this.receiver = event.getOption("receiver").getAsUser(); - } catch (NullPointerException ignored) {} - + } catch (NullPointerException ignored) { } this.description = null; try { this.description = event.getOption("message").getAsString(); - } catch (NullPointerException ignored) {} + } catch (NullPointerException ignored) { } - String url = InteractionHandler.getImage(this.type); + String url = InteractionHandler.getImage(this.type).orElse(null); getMessageAndFooter(); event.getHook().sendMessage(this.message).addEmbeds(actionEmbed(url, this.footer)).queue(); // Sends a message if CafeBot is the one who receives the interaction. - if (this.containsCafeBot()) - event.getChannel().sendMessage(selfMessage).queue(); + if (this.containsCafeBot()) event.getChannel().sendMessage(selfMessage).queue(); } /** @@ -82,39 +79,37 @@ private void getMessageAndFooter() { message = noUserAction.replace("{sender}", sender.getName()); footer = null; - } else { - message = userAction.replace("{sender}", sender.getName()).replace("{receiver}", receiver.getAsMention()); - - Integer sendAmount = null; - Integer receiveAmount = null; - try { - sendAmount = InteractionHandler.getUserInteractionsSent(sender.getId(), type) + 1; - receiveAmount = InteractionHandler.getUserInteractionsReceived(receiver.getId(), type) + 1; - InteractionHandler.updateSender(sender.getId(), type, sendAmount); - InteractionHandler.updateReceiver(receiver.getId(), type, receiveAmount); - } catch (NullPointerException e) { - Bot.getLogger().log(this.getClass(), LogLevel.WARN, "Error Getting Send/Receive Amounts: " + e.getMessage(), e); - } - - footer = footer.replace("{sender}", sender.getName()) - .replace("{amount_sent}", String.valueOf(sendAmount)) - .replace("{receiver}", receiver.getName()) - .replace("{amount_received}", String.valueOf(receiveAmount)); + return; } + + message = userAction.replace("{sender}", sender.getName()).replace("{receiver}", receiver.getAsMention()); + + Integer sendAmount = null; + Integer receiveAmount = null; + try { + sendAmount = InteractionHandler.getUserInteractionsSent(sender.getId(), type).orElseThrow() + 1; + receiveAmount = InteractionHandler.getUserInteractionsReceived(receiver.getId(), type).orElseThrow() + 1; + InteractionHandler.updateSender(sender.getId(), type, sendAmount); + InteractionHandler.updateReceiver(receiver.getId(), type, receiveAmount); + } catch (NullPointerException e) { + Bot.getLogger().log(this.getClass(), LogLevel.WARN, "Error Getting Send/Receive Amounts: " + e.getMessage(), e); + } + + footer = footer.replace("{sender}", sender.getName()) + .replace("{amount_sent}", String.valueOf(sendAmount)) + .replace("{receiver}", receiver.getName()) + .replace("{amount_received}", String.valueOf(receiveAmount)); } /** * @param link The image URL for the {@link MessageEmbed}. * @return The created {@link MessageEmbed}. */ - @NotNull private MessageEmbed actionEmbed(@Nullable String link, @Nullable String footer) { EmbedBuilder embedBuilder = new EmbedBuilder(); - if (link != null) - embedBuilder.setImage(link); - if (description != null) - embedBuilder.setDescription("\"" + description + "\""); + if (link != null) embedBuilder.setImage(link); + if (description != null) embedBuilder.setDescription("\"" + description + "\""); embedBuilder.setColor(Helper.getRandomColor()); embedBuilder.setFooter(footer); @@ -125,11 +120,8 @@ private MessageEmbed actionEmbed(@Nullable String link, @Nullable String footer) * Checks if the {@link Interaction} contains the bot. * @return True, if the {@link Interaction} contains the bot. */ - @NotNull - public Boolean containsCafeBot() { - if (receiver == null) - return false; - + public boolean containsCafeBot() { + if (receiver == null) return false; return receiver.getId().equals(Bot.getBot().getSelfUser().getId()); } diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/section/interaction/InteractionHandler.java b/src/main/java/com/beanbeanjuice/cafebot/utility/section/interaction/InteractionHandler.java index 4e6856fc..61ebd0d8 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/section/interaction/InteractionHandler.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/section/interaction/InteractionHandler.java @@ -7,8 +7,8 @@ import com.beanbeanjuice.cafeapi.wrapper.exception.api.CafeException; import com.beanbeanjuice.cafeapi.wrapper.exception.api.ConflictException; import com.beanbeanjuice.cafeapi.wrapper.exception.api.NotFoundException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; + +import java.util.Optional; /** * A class used for handling {@link Interaction} between users. @@ -19,71 +19,66 @@ public class InteractionHandler { /** * Retrieves a random image {@link String URL} from the {@link CafeAPI CafeAPI}. + * * @param type The {@link Interaction type} of image. * @return The {@link String URL} of the image. */ - @Nullable - public static String getImage(@NotNull InteractionType type) { + public static Optional getImage(final InteractionType type) { try { - return Bot.getCafeAPI().INTERACTION_PICTURE.getRandomInteractionPicture(type); + return Optional.of(Bot.getCafeAPI().INTERACTION_PICTURE.getRandomInteractionPicture(type)); } catch (CafeException e) { Bot.getLogger().log(InteractionHandler.class, LogLevel.WARN, "Error Getting Random Interaction Image: " + e.getMessage(), e); - return null; + return Optional.empty(); } } /** * Retrieve the {@link Integer amount} of {@link Interaction} sent by the specified {@link String userID} for the specified {@link InteractionType type}. + * * @param userID The specified {@link String userID}. - * @param type The {@link InteractionType type} of {@link Interaction}. - * @return The {@link Integer amount} of {@link Interaction} sent for that {@link InteractionType type}. Null, if there was an error. + * @param type The {@link InteractionType type} of {@link Interaction}. + * @return The {@link Optional amount} of {@link Interaction} sent for that {@link InteractionType type}. */ - @Nullable - public static Integer getUserInteractionsSent(@NotNull String userID, @NotNull InteractionType type) { - if (!createInteractionSender(userID)) { - try { - return Bot.getCafeAPI().INTERACTION.getSpecificUserInteractionSentAmount(userID, type); - } catch (CafeException e) { - return null; - } - } + public static Optional getUserInteractionsSent(final String userID, final InteractionType type) { + if (createInteractionSender(userID)) return Optional.of(0); - return 0; + try { + return Optional.of(Bot.getCafeAPI().INTERACTION.getSpecificUserInteractionSentAmount(userID, type)); + } catch (CafeException e) { + return Optional.empty(); + } } /** * Retrieve the {@link Integer amount} of {@link Interaction} received by the specified {@link String userID} for the specified {@link InteractionType type}. + * * @param userID The specified {@link String userID}. - * @param type The {@link InteractionType type} of {@link Interaction}. + * @param type The {@link InteractionType type} of {@link Interaction}. * @return The {@link Integer amount} of {@link Interaction} received for that {@link InteractionType type}. Null, if there was an error. */ - @Nullable - public static Integer getUserInteractionsReceived(@NotNull String userID, @NotNull InteractionType type) { - if (!createInteractionReceiver(userID)) { - try { - return Bot.getCafeAPI().INTERACTION.getSpecificUserInteractionReceivedAmount(userID, type); - } catch (CafeException e) { - return null; - } - } + public static Optional getUserInteractionsReceived(final String userID, final InteractionType type) { + if (createInteractionReceiver(userID)) return Optional.of(0); - return 0; + try { + return Optional.of(Bot.getCafeAPI().INTERACTION.getSpecificUserInteractionReceivedAmount(userID, type)); + } catch (CafeException e) { + return Optional.empty(); + } } /** * Creates an {@link Interaction} receiver user. + * * @param userID The specified {@link String userID}. * @return True, if the {@link Interaction} receiver was created successfully. */ - @NotNull - private static Boolean createInteractionReceiver(@NotNull String userID) { + private static boolean createInteractionReceiver(final String userID) { try { Bot.getCafeAPI().INTERACTION.createUserInteractionsReceived(userID); return true; } catch (ConflictException e) { return false; - } - catch (CafeException e) { + } catch (CafeException e) { Bot.getLogger().log(InteractionHandler.class, LogLevel.WARN, "Error Creating Interaction Receiver: " + e.getMessage(), e); return false; } @@ -91,18 +86,17 @@ private static Boolean createInteractionReceiver(@NotNull String userID) { /** * Creates an {@link Interaction} sender user. + * * @param userID The specified {@link String userID}. * @return True, if the {@link Interaction} sender was created successfully. */ - @NotNull - private static Boolean createInteractionSender(@NotNull String userID) { + private static boolean createInteractionSender(final String userID) { try { Bot.getCafeAPI().INTERACTION.createUserInteractionsSent(userID); return true; } catch (ConflictException ignored) { return false; - } - catch (CafeException e) { + } catch (CafeException e) { Bot.getLogger().log(InteractionHandler.class, LogLevel.WARN, "Error Creating Interaction Sender: " + e.getMessage(), e); return false; } @@ -110,22 +104,19 @@ private static Boolean createInteractionSender(@NotNull String userID) { /** * Updates the {@link Interaction} sender's {@link Integer amount} of {@link Interaction}. + * * @param userID The {@link String userID} of the {@link net.dv8tion.jda.api.entities.User sender}. - * @param type The {@link InteractionType type} of {@link Interaction}. + * @param type The {@link InteractionType type} of {@link Interaction}. * @param amount The {@link Integer amount} of {@link Interaction}. * @return True, if the {@link Interaction} sender was updated successfully. */ - @NotNull - public static Boolean updateSender(@NotNull String userID, @NotNull InteractionType type, @NotNull Integer amount) { + public static boolean updateSender(final String userID, final InteractionType type, final int amount) { try { Bot.getCafeAPI().INTERACTION.updateSpecificUserInteractionSentAmount(userID, type, amount); return true; } catch (NotFoundException e) { - if (createInteractionSender(userID)) { - return updateSender(userID, type, amount); - } else { - return false; - } + if (createInteractionSender(userID)) return updateSender(userID, type, amount); + else return false; } catch (CafeException e) { Bot.getLogger().log(InteractionHandler.class, LogLevel.WARN, "Error Updating Interaction Sender: " + e.getMessage(), e); return false; @@ -134,25 +125,19 @@ public static Boolean updateSender(@NotNull String userID, @NotNull InteractionT /** * Updates the {@link Interaction} receiver's {@link Integer amount} of {@link Interaction}. + * * @param userID The {@link String userID} of the {@link net.dv8tion.jda.api.entities.User receiver}. - * @param type The {@link InteractionType type} of {@link Interaction}. + * @param type The {@link InteractionType type} of {@link Interaction}. * @param amount The {@link Integer amount} of {@link Interaction}. * @return True, if the {@link Interaction} receiver was updated successfully. */ - @NotNull - public static Boolean updateReceiver(@NotNull String userID, @NotNull InteractionType type, @NotNull Integer amount) { - + public static boolean updateReceiver(final String userID, final InteractionType type, final int amount) { try { Bot.getCafeAPI().INTERACTION.updateSpecificUserInteractionReceivedAmount(userID, type, amount); return true; } catch (NotFoundException e) { - - if (createInteractionReceiver(userID)) { - return updateReceiver(userID, type, amount); - } else { - return false; - } - + if (createInteractionReceiver(userID)) return updateReceiver(userID, type, amount); + else return false; } catch (CafeException e) { Bot.getLogger().log(InteractionHandler.class, LogLevel.WARN, "Error Updating Interaction Receiver: " + e.getMessage(), e); return false; diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/section/moderation/poll/Poll.java b/src/main/java/com/beanbeanjuice/cafebot/utility/section/moderation/poll/Poll.java index c8576fe6..710e4dea 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/section/moderation/poll/Poll.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/section/moderation/poll/Poll.java @@ -11,6 +11,7 @@ * A class used for {@link Poll} objects. * * @author beanbeanjuice + * TODO: Refactor */ public class Poll extends com.beanbeanjuice.cafeapi.wrapper.cafebot.polls.Poll { diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/section/moderation/raffle/Raffle.java b/src/main/java/com/beanbeanjuice/cafebot/utility/section/moderation/raffle/Raffle.java index 7481c8da..638e0611 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/section/moderation/raffle/Raffle.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/section/moderation/raffle/Raffle.java @@ -11,6 +11,7 @@ * A custom {@link Raffle} class. * * @author beanbeanjuice + * TODO: Refactor */ public class Raffle extends com.beanbeanjuice.cafeapi.wrapper.cafebot.raffles.Raffle { diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchHandler.java b/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchHandler.java index dff9872c..24b7afaf 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchHandler.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchHandler.java @@ -6,7 +6,6 @@ import com.netflix.hystrix.exception.HystrixRuntimeException; import lombok.Getter; import org.apache.commons.lang3.exception.ContextedRuntimeException; -import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchListener.java b/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchListener.java index 1e7c0efc..17d68351 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchListener.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchListener.java @@ -8,7 +8,6 @@ import com.netflix.hystrix.exception.HystrixRuntimeException; import lombok.Getter; import org.apache.commons.lang3.exception.ContextedRuntimeException; -import org.jetbrains.annotations.NotNull; import java.util.List; diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchMessageEventHandler.java b/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchMessageEventHandler.java index 148c3a7b..b1b7ac4c 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchMessageEventHandler.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/section/twitch/TwitchMessageEventHandler.java @@ -10,7 +10,6 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; -import org.jetbrains.annotations.NotNull; import java.awt.*; import java.util.ArrayList;