Skip to content

Commit

Permalink
Refactored the Interaction Component
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jun 20, 2024
1 parent 21d0e84 commit 71d3cb8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
}

/**
Expand All @@ -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);
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,113 +19,104 @@ 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<String> 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<Integer> 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<Integer> 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<Integer> 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;
}
}

/**
* 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;
}
}

/**
* 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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* A custom {@link Raffle} class.
*
* @author beanbeanjuice
* TODO: Refactor
*/
public class Raffle extends com.beanbeanjuice.cafeapi.wrapper.cafebot.raffles.Raffle {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 71d3cb8

Please sign in to comment.