Skip to content

Commit

Permalink
Refactored the API Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jun 23, 2024
1 parent 8b92a69 commit 37dee73
Show file tree
Hide file tree
Showing 32 changed files with 564 additions and 622 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/beanbeanjuice/cafebot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public class Bot {

public Bot() throws LoginException, InterruptedException {
logger = new LogManager("cafeBot Logging System", HOME_GUILD_ID, HOME_GUILD_LOG_CHANNEL_ID, "logs/");
Helper.startCafeAPIRefreshTimer(location);
cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), location);
cafeAPI.setKawaiiAPI(System.getenv("KAWAII_API_TOKEN"));

logger.addWebhookURL(HOME_GUILD_WEBHOOK_URL);
logger.log(Bot.class, LogLevel.OKAY, "Starting bot!", true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

// Updating the Donator
try {
Bot.getCafeAPI().CAFE_USER.updateCafeUser(donator.getUserID(), CafeType.BEAN_COINS, donator.getBeanCoins() - amountToDonate);
Bot.getCafeAPI().getCafeUsersEndpoint().updateCafeUser(donator.getUserID(), CafeType.BEAN_COINS, donator.getBeanCoins() - amountToDonate);
} catch (CafeException e) {
event.getHook().sendMessageEmbeds(Helper.sqlServerError(
"Error updating donation sender. There has been an error contacting the Cafe API."
Expand All @@ -103,7 +103,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

// Updating the Donatee
try {
Bot.getCafeAPI().CAFE_USER.updateCafeUser(donatee.getUserID(), CafeType.BEAN_COINS, donatee.getBeanCoins() + amountToDonate);
Bot.getCafeAPI().getCafeUsersEndpoint().updateCafeUser(donatee.getUserID(), CafeType.BEAN_COINS, donatee.getBeanCoins() + amountToDonate);
} catch (CafeException e) {
event.getHook().sendMessageEmbeds(Helper.sqlServerError(
"Error updating donation receiver. There has been an error contacting the Cafe API."
Expand All @@ -116,7 +116,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {
try {
CafeGeneric.parseTimestamp(new Timestamp(System.currentTimeMillis() + BeanCoinDonationHandler.getCooldown()).toString())
.ifPresent((endingTime) -> {
Bot.getCafeAPI().DONATION_USER.addDonationUser(donatee.getUserID(), endingTime);
Bot.getCafeAPI().getDonationUsersEndpoint().addDonationUser(donatee.getUserID(), endingTime);
BeanCoinDonationHandler.addUser(donatee.getUserID(), endingTime);
});
} catch (CafeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

// Trying to update the orderer.
try {
Bot.getCafeAPI().CAFE_USER.updateCafeUser(orderer.getUserID(), CafeType.BEAN_COINS, orderer.getBeanCoins() - totalPrice);
Bot.getCafeAPI().CAFE_USER.updateCafeUser(orderer.getUserID(), CafeType.ORDERS_BOUGHT, orderer.getOrdersBought() + 1);
Bot.getCafeAPI().getCafeUsersEndpoint().updateCafeUser(orderer.getUserID(), CafeType.BEAN_COINS, orderer.getBeanCoins() - totalPrice);
Bot.getCafeAPI().getCafeUsersEndpoint().updateCafeUser(orderer.getUserID(), CafeType.ORDERS_BOUGHT, orderer.getOrdersBought() + 1);
} catch (CafeException e) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Updating User",
Expand All @@ -94,7 +94,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

// Trying to update the receiver.
try {
Bot.getCafeAPI().CAFE_USER.updateCafeUser(receiver.getUserID(), CafeType.ORDERS_RECEIVED, receiver.getOrdersReceived() + 1);
Bot.getCafeAPI().getCafeUsersEndpoint().updateCafeUser(receiver.getUserID(), CafeType.ORDERS_RECEIVED, receiver.getOrdersReceived() + 1);
} catch (CafeException e) {
Bot.getLogger().log(this.getClass(), LogLevel.ERROR, "Error Updating Receiver: " + e.getMessage(), e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {
Word serveWord;
try {
// Checking if the word entered is a word.
serveWord = Bot.getCafeAPI().WORD.getWord(word);
serveWord = Bot.getCafeAPI().getWordsEndpoint().getWord(word);
} catch (CafeException e) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Not A Word",
Expand Down Expand Up @@ -79,8 +79,8 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

// Updates the Balance and Last Serving Time for the user.
try {
Bot.getCafeAPI().CAFE_USER.updateCafeUser(cafeUser.getUserID(), CafeType.LAST_SERVING_TIME, currentTimestamp);
Bot.getCafeAPI().CAFE_USER.updateCafeUser(cafeUser.getUserID(), CafeType.BEAN_COINS, cafeUser.getBeanCoins() + calculatedTip);
Bot.getCafeAPI().getCafeUsersEndpoint().updateCafeUser(cafeUser.getUserID(), CafeType.LAST_SERVING_TIME, currentTimestamp);
Bot.getCafeAPI().getCafeUsersEndpoint().updateCafeUser(cafeUser.getUserID(), CafeType.BEAN_COINS, cafeUser.getBeanCoins() + calculatedTip);
} catch (CafeException e) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Updating Cafe User",
Expand All @@ -92,7 +92,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

// Attempts to update the word
try {
Bot.getCafeAPI().WORD.updateWord(serveWord.getWord(), serveWord.getUses() + 1);
Bot.getCafeAPI().getWordsEndpoint().updateWord(serveWord.getWord(), serveWord.getUses() + 1);
} catch (CafeException e) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Updating Word",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

// Tries to create the code for the user in the database.
try {
Bot.getCafeAPI().GENERATED_CODE.createUserGeneratedCode(event.getUser().getId(), generatedCode);
Bot.getCafeAPI().getGeneratedCodesEndpoint().createUserGeneratedCode(event.getUser().getId(), generatedCode);
} catch (ConflictException e) {

// If the code exists, then update it.
try {
Bot.getCafeAPI().GENERATED_CODE.updateUserGeneratedCode(event.getUser().getId(), generatedCode);
Bot.getCafeAPI().getGeneratedCodesEndpoint().updateUserGeneratedCode(event.getUser().getId(), generatedCode);

event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Generated Code",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class PollCommand implements ICommand {
public void handle(@NotNull SlashCommandInteractionEvent event) {

// Check if the poll channel exists.
if (GuildHandler.getCustomGuild(event.getGuild()).getPollChannel() == null) {
if (GuildHandler.getCustomGuild(event.getGuild()).getPollChannel().isEmpty()) {
event.replyEmbeds(Helper.errorEmbed(
"No Poll Channel",
"It seems you do not have a poll channel set! You must have a dedicated poll channel. " +
Expand All @@ -53,7 +53,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {
}

// Makes sure that guilds only have 3 polls.
if (Bot.getCafeAPI().POLL.getGuildPolls(event.getGuild().getId()).size() >= MAX_POLLS) {
if (Bot.getCafeAPI().getPollsEndpoint().getGuildPolls(event.getGuild().getId()).size() >= MAX_POLLS) {
event.replyEmbeds(Helper.errorEmbed(
"Too Many Polls",
"You can currently only have a total of " +
Expand Down Expand Up @@ -86,34 +86,34 @@ public void handleModal(@NotNull ModalInteractionEvent event) {
}

// Making sure the poll channel exists.
TextChannel pollChannel = GuildHandler.getCustomGuild(event.getGuild()).getPollChannel();

// Making sure there are less than 20 arguments.
if (arguments.size() > 20) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Too Many Items",
"You can only have 20 items. Please try again. Discord only supports 20 message reactions."
GuildHandler.getCustomGuild(event.getGuild()).getPollChannel().ifPresent((pollChannel) -> {
// Making sure there are less than 20 arguments.
if (arguments.size() > 20) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Too Many Items",
"You can only have 20 items. Please try again. Discord only supports 20 message reactions."
)).queue();
return;
}

Timestamp timestamp = CafeGeneric.parseTimestamp(new Timestamp(System.currentTimeMillis() + (minutes*60000)).toString()).orElseThrow();

event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Creating Poll",
"I'm creating your poll right now!"
)).queue();
return;
}

Timestamp timestamp = CafeGeneric.parseTimestamp(new Timestamp(System.currentTimeMillis() + (minutes*60000)).toString()).orElseThrow();

event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Creating Poll",
"I'm creating your poll right now!"
)).queue();

// Sending a message in the poll channel.
if (event.getValue("message") != null) {
pollChannel.sendMessage(event.getValue("poll-message").getAsString()).setEmbeds(startingPollEmbed()).queue(message -> {
editMessage(message, event, timestamp, title, minutes, arguments);
});
} else {
pollChannel.sendMessageEmbeds(startingPollEmbed()).queue(message -> {
editMessage(message, event, timestamp, title, minutes, arguments);
});
}
// Sending a message in the poll channel.
if (event.getValue("message") != null) {
pollChannel.sendMessage(event.getValue("poll-message").getAsString()).setEmbeds(startingPollEmbed()).queue(message -> {
editMessage(message, event, timestamp, title, minutes, arguments);
});
} else {
pollChannel.sendMessageEmbeds(startingPollEmbed()).queue(message -> {
editMessage(message, event, timestamp, title, minutes, arguments);
});
}
});
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class RaffleCommand implements ICommand {
@Override
public void handle(@NotNull SlashCommandInteractionEvent event) {
// Check if the amount of raffles the server has is more than 3
if (Bot.getCafeAPI().RAFFLE.getGuildRaffles(event.getGuild().getId()).size() >= MAX_RAFFLES) {
if (Bot.getCafeAPI().getRafflesEndpoint().getGuildRaffles(event.getGuild().getId()).size() >= MAX_RAFFLES) {
event.replyEmbeds(Helper.errorEmbed(
"Too Many Raffles",
"Your guild currently has 3 raffles. This is a limitation due to server costs."
Expand All @@ -49,7 +49,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {
}

// Checking if the raffle channel still exists.
if (GuildHandler.getCustomGuild(event.getGuild()).getRaffleChannel() == null) {
if (GuildHandler.getCustomGuild(event.getGuild()).getRaffleChannel().isEmpty()) {
event.replyEmbeds(Helper.errorEmbed(
"Raffle Channel Not Set",
"You currently do not have a raffle channel set. You must have a dedicated raffle channel. " +
Expand All @@ -65,46 +65,46 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

@Override
public void handleModal(@NotNull ModalInteractionEvent event) {
TextChannel raffleChannel = GuildHandler.getCustomGuild(event.getGuild()).getRaffleChannel();

// Required items.
String title = event.getValue("raffle-title").getAsString();
String description = event.getValue("raffle-description").getAsString();
int winnerAmount = Helper.stringToPositiveInteger(event.getValue("raffle-winners").getAsString());
int minutes = Helper.stringToPositiveInteger(event.getValue("raffle-time").getAsString());

// Checking integer values.
if (winnerAmount < 1) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Must Have At Least 1 Winner",
"Raffles must have at least one winner."
GuildHandler.getCustomGuild(event.getGuild()).getRaffleChannel().ifPresent((raffleChannel) -> {
// Required items.
String title = event.getValue("raffle-title").getAsString();
String description = event.getValue("raffle-description").getAsString();
int winnerAmount = Helper.stringToPositiveInteger(event.getValue("raffle-winners").getAsString());
int minutes = Helper.stringToPositiveInteger(event.getValue("raffle-time").getAsString());

// Checking integer values.
if (winnerAmount < 1) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Must Have At Least 1 Winner",
"Raffles must have at least one winner."
)).queue();
return;
}

if (minutes < 1) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Must Be At Least 1 Minute",
"Raffles must last at least 1 minute."
)).queue();
return;
}

event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Creating Raffle",
"Currently creating the raffle..."
)).queue();
return;
}

if (minutes < 1) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Must Be At Least 1 Minute",
"Raffles must last at least 1 minute."
)).queue();
return;
}

event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Creating Raffle",
"Currently creating the raffle..."
)).queue();

// Finally send the message.
if (event.getValue("raffle-message") != null) {
raffleChannel.sendMessage(event.getValue("raffle-message").getAsString()).setEmbeds(creatingRaffle()).queue(message -> {
editMessage(message, event, title, description, minutes, winnerAmount);
});
} else {
raffleChannel.sendMessageEmbeds(creatingRaffle()).queue(message -> {
editMessage(message, event, title, description, minutes, winnerAmount);
});
}
// Finally send the message.
if (event.getValue("raffle-message") != null) {
raffleChannel.sendMessage(event.getValue("raffle-message").getAsString()).setEmbeds(creatingRaffle()).queue(message -> {
editMessage(message, event, title, description, minutes, winnerAmount);
});
} else {
raffleChannel.sendMessageEmbeds(creatingRaffle()).queue(message -> {
editMessage(message, event, title, description, minutes, winnerAmount);
});
}
});
}

private void editMessage(Message message, ModalInteractionEvent event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {

// Now, try to create "Counting Information" in the database.
try {
Bot.getCafeAPI().COUNTING_INFORMATION.createGuildCountingInformation(event.getGuild().getId());
Bot.getCafeAPI().getCountingEndpoint().createGuildCountingInformation(event.getGuild().getId());
}
catch (ConflictException ignored) {} // Ignore this.
catch (AuthorizationException | ResponseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {
@NotNull
public Boolean setGuildGoodbye(@NotNull GuildGoodbye guildGoodbye) {
try {
return Bot.getCafeAPI().GOODBYE.createGuildGoodbye(guildGoodbye);
return Bot.getCafeAPI().getGoodbyesEndpoint().createGuildGoodbye(guildGoodbye);
} catch (ConflictException e) {
return Bot.getCafeAPI().GOODBYE.updateGuildGoodbye(guildGoodbye);
return Bot.getCafeAPI().getGoodbyesEndpoint().updateGuildGoodbye(guildGoodbye);
} catch (CafeException e) {
Bot.getLogger().log(this.getClass(), LogLevel.ERROR, "Error Setting Guild Goodbye: " + e.getMessage(), e);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void handle(@NotNull SlashCommandInteractionEvent event) {
@NotNull
public Boolean setGuildWelcome(@NotNull GuildWelcome guildWelcome) {
try {
return Bot.getCafeAPI().WELCOME.createGuildWelcome(guildWelcome);
return Bot.getCafeAPI().getWelcomesEndpoint().createGuildWelcome(guildWelcome);
} catch (ConflictException e) {
return Bot.getCafeAPI().WELCOME.updateGuildWelcome(guildWelcome);
return Bot.getCafeAPI().getWelcomesEndpoint().updateGuildWelcome(guildWelcome);
} catch (CafeException e) {
Bot.getLogger().log(this.getClass(), LogLevel.ERROR, "Error Setting Guild Welcome: " + e.getMessage(), e);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@ public class VentCommand implements ICommand {
@Override
public void handle(@NotNull SlashCommandInteractionEvent event) {
CustomGuild guild = GuildHandler.getCustomGuild(event.getGuild());
TextChannel ventChannel = guild.getVentingChannel();

if (ventChannel == null) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Cannot Send Vent",
"There is no anonymous venting channel enabled on this server. Let them know " +
"they can enable it by doing `/venting-channel set`!"
)).queue();
return;
}

ventChannel.sendMessageEmbeds(ventEmbed(event.getOption("vent_message").getAsString())).queue((e) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Vent Sent",
"Your anonymous vent has been successfully sent!"
)).queue();
});
guild.getVentingChannel().ifPresentOrElse(
(ventingChannel) -> {
ventingChannel.sendMessageEmbeds(ventEmbed(event.getOption("vent_message").getAsString())).queue((e) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Vent Sent",
"Your anonymous vent has been successfully sent!"
)).queue();
});
},
() -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Cannot Send Vent",
"There is no anonymous venting channel enabled on this server. Let them know " +
"they can enable it by doing `/venting-channel set`!"
)).queue();
}
);
}

@NotNull
Expand Down
Loading

0 comments on commit 37dee73

Please sign in to comment.