diff --git a/README.md b/README.md index a8eebeb..d4f0a24 100644 --- a/README.md +++ b/README.md @@ -9,19 +9,19 @@ ___ My Discord bot is built using Java Discord API with Spring framework integration. It contains several features, including: ___ -![GitHub Issues](https://img.shields.io/github/issues/pitzzahh/spring-pitzzahh-bot) -![Forks](https://img.shields.io/github/forks/pitzzahh/spring-pitzzahh-bot) -![Stars](https://img.shields.io/github/stars/pitzzahh/spring-pitzzahh-bot) -![License](https://img.shields.io/github/license/pitzzahh/spring-pitzzahh-bot) -![Forks](https://img.shields.io/github/forks/pitzzahh/spring-pitzzahh-bot) -![Stars](https://img.shields.io/github/stars/pitzzahh/spring-pitzzahh-bot) -![Contributors](https://img.shields.io/github/contributors/pitzzahh/spring-pitzzahh-bot) -![Last Commit](https://img.shields.io/github/last-commit/pitzzahh/spring-pitzzahh-bot) -![Code size](https://img.shields.io/github/languages/code-size/pitzzahh/spring-pitzzahh-bot) -![Top language](https://img.shields.io/github/languages/top/pitzzahh/spring-pitzzahh-bot) -![Languages count](https://img.shields.io/github/languages/count/pitzzahh/spring-pitzzahh-bot) -![Repo size](https://img.shields.io/github/repo-size/pitzzahh/spring-pitzzahh-bot) -![Lines of code](https://img.shields.io/tokei/lines/github/pitzzahh/spring-pitzzahh-bot?label=lines%20of%20code) +![GitHub Issues](https://img.shields.io/github/issues/pitzzahh/pitzzahh-bot) +![Forks](https://img.shields.io/github/forks/pitzzahh/pitzzahh-bot) +![Stars](https://img.shields.io/github/stars/pitzzahh/pitzzahh-bot) +![License](https://img.shields.io/github/license/pitzzahh/pitzzahh-bot) +![Forks](https://img.shields.io/github/forks/pitzzahh/pitzzahh-bot) +![Stars](https://img.shields.io/github/stars/pitzzahh/pitzzahh-bot) +![Contributors](https://img.shields.io/github/contributors/pitzzahh/pitzzahh-bot) +![Last Commit](https://img.shields.io/github/last-commit/pitzzahh/pitzzahh-bot) +![Code size](https://img.shields.io/github/languages/code-size/pitzzahh/pitzzahh-bot) +![Top language](https://img.shields.io/github/languages/top/pitzzahh/pitzzahh-bot) +![Languages count](https://img.shields.io/github/languages/count/pitzzahh/pitzzahh-bot) +![Repo size](https://img.shields.io/github/repo-size/pitzzahh/pitzzahh-bot) +![Lines of code](https://img.shields.io/tokei/lines/github/pitzzahh/pitzzahh-bot?label=lines%20of%20code) ___ - User Verification: This feature allows users to verify their account before joining the server, ensuring that only authorized users can access the server. diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/SlashCommand.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/SlashCommand.java index c5ed91b..e34b0c3 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/SlashCommand.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/SlashCommand.java @@ -26,8 +26,6 @@ import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.internal.interactions.CommandDataImpl; - -import java.io.IOException; import java.util.function.Consumer; import java.util.function.Supplier; @@ -38,7 +36,7 @@ public interface SlashCommand { * @return nothing. * @see Consumer */ - Consumer execute() throws InterruptedException, IOException; + Consumer execute(); /** * Supplies the name of the slash command. diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/SlashCommandManager.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/SlashCommandManager.java index 9bccdcc..7dcb1c3 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/SlashCommandManager.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/SlashCommandManager.java @@ -25,7 +25,8 @@ package tech.araopj.springpitzzahhbot.commands.slash_command; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.confessions.service.ConfessionService; -import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.service.JokesService; +import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.submitJoke.SubmitJoke; +import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.service.JokesService; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.game.service.GameService; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.confessions.Confession; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.GetJoke; @@ -63,7 +64,8 @@ public SlashCommandManager( addCommands( new Confession(confessionService, commandsService, channelService, messageUtil), new Game(gameService, messageUtil), - new GetJoke(messageUtil, jokesService, httpConfig) + new GetJoke(messageUtil, jokesService, httpConfig), + new SubmitJoke(messageUtil, jokesService, httpConfig) ); } diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/entity/Joke.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/entity/Joke.java new file mode 100644 index 0000000..e272732 --- /dev/null +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/entity/Joke.java @@ -0,0 +1,30 @@ +/* + * MIT License + * + * Copyright (c) 2022 pitzzahh + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.entity; + +import lombok.Builder; + +@Builder +public record Joke(String joke, String category, String language) { } diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/getJoke/GetJoke.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/getJoke/GetJoke.java index 4f15201..1a4ea24 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/getJoke/GetJoke.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/getJoke/GetJoke.java @@ -24,7 +24,7 @@ package tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke; -import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.service.JokesService; +import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.service.JokesService; import tech.araopj.springpitzzahhbot.commands.slash_command.CommandContext; import tech.araopj.springpitzzahhbot.commands.slash_command.SlashCommand; import net.dv8tion.jda.api.interactions.commands.build.CommandData; @@ -36,18 +36,16 @@ import tech.araopj.springpitzzahhbot.config.HttpConfig; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Component; - -import static java.awt.Color.YELLOW; import static java.time.LocalDateTime.now; import static java.lang.String.format; - -import java.time.ZoneId; +import static java.awt.Color.YELLOW; import java.util.function.Consumer; import java.util.function.Supplier; import java.net.http.HttpResponse; import static java.awt.Color.CYAN; import lombok.extern.slf4j.Slf4j; import java.io.IOException; +import java.time.ZoneId; import java.net.URI; @Slf4j @@ -74,15 +72,12 @@ public Consumer execute() { * @param context the command context containing the information about the command. */ private void process(CommandContext context){ - - final var category = context.getEvent().getOption("category"); - log.info("Category: {}", category); - - final var language = context.getEvent().getOption("language"); - log.info("Language: {}", language); - String url = jokesService.createJokeRequestUrl(category, language); - log.info("Url: {}", url); - final var REQUEST = httpConfig.httpBuilder() // TODO: create a uri builder + var url = jokesService.createJokeRequestUrl( + context.getEvent().getOption("category"), + context.getEvent().getOption("language") + ); + log.info("Get Joke url: {}", url); + final var REQUEST = httpConfig.httpBuilder() .uri(URI.create(url)) .GET() .build(); diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/getJoke/service/JokesService.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/service/JokesService.java similarity index 88% rename from src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/getJoke/service/JokesService.java rename to src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/service/JokesService.java index e150d44..c5b1238 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/getJoke/service/JokesService.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/service/JokesService.java @@ -22,9 +22,10 @@ * SOFTWARE. */ -package tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.service; +package tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.service; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.entity.*; +import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.entity.Joke; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import tech.araopj.springpitzzahhbot.config.HttpConfig; import java.util.concurrent.ExecutionException; @@ -106,4 +107,15 @@ public String createJokeSubmitUrl() { return httpConfig.getJokeApiUrl().concat("submit"); } + public String createJokeSubmitBody(OptionMapping joke, OptionMapping category, OptionMapping language) { + var jokeObject = category != null ? Joke.builder() + .joke(joke.getAsString()) + .category(category.getAsString()) + .language(language.getAsString()) + .build() : Joke.builder() + .joke(joke.getAsString()) + .language(language.getAsString()) + .build(); + return new Gson().toJson(jokeObject); + } } diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/submitJoke/SubmitJoke.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/submitJoke/SubmitJoke.java new file mode 100644 index 0000000..600afea --- /dev/null +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/submitJoke/SubmitJoke.java @@ -0,0 +1,181 @@ +/* + * MIT License + * + * Copyright (c) 2022 pitzzahh + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.submitJoke; + +import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.service.JokesService; +import tech.araopj.springpitzzahhbot.commands.slash_command.CommandContext; +import tech.araopj.springpitzzahhbot.commands.slash_command.SlashCommand; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.internal.interactions.CommandDataImpl; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import tech.araopj.springpitzzahhbot.utilities.MessageUtil; +import tech.araopj.springpitzzahhbot.config.HttpConfig; +import org.springframework.stereotype.Component; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.ZoneId; +import java.util.function.Consumer; +import java.util.function.Supplier; +import lombok.extern.slf4j.Slf4j; +import static java.awt.Color.CYAN; +import static java.awt.Color.YELLOW; +import static java.lang.String.format; +import static java.time.LocalDateTime.now; + +@Slf4j +@Component +public record SubmitJoke( + MessageUtil messageUtil, + JokesService jokesService, + HttpConfig httpConfig +) implements SlashCommand { + + /** + * Executes a {@code SlashCommand} + * + * @return nothing. + * @see Consumer + */ + @Override + public Consumer execute() { + return this::process; + } + + /** + * Contains the process to be executed. + * + * @param context the command context containing the information about the command. + */ + private void process(CommandContext context) { + + var url = jokesService.createJokeSubmitUrl(); + + log.info("Submit Joke url: {}", url); + var jokeSubmitBody = jokesService.createJokeSubmitBody( + context.getEvent().getOption("joke"), + context.getEvent().getOption("category"), + context.getEvent().getOption("language") + ); + log.info("Submit Joke body: {}", jokeSubmitBody); + final var REQUEST = httpConfig.httpBuilder() + .uri(URI.create(url)) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(jokeSubmitBody)) + .build(); + + final HttpResponse RESPONSE; + + try { + RESPONSE = httpConfig.httpClient().send(REQUEST, HttpResponse.BodyHandlers.ofString()); + log.info("Response from joke api: {}", RESPONSE.body()); + } catch (IOException | InterruptedException e) { + log.error("Error while sending request to joke api", e); + throw new RuntimeException(e); + } + + if (RESPONSE.statusCode() == 200) { + messageUtil.getEmbedBuilder() + .clear() + .clearFields() + .setColor(CYAN) + .setTitle(RESPONSE.body()) + .setDescription("Your joke has been sent to the joke api. It will be reviewed and added to the joke api if it is good enough.") + .setTimestamp(now(ZoneId.systemDefault())) + .setFooter( + format("Created by %s", context.getGuild().getJDA().getSelfUser().getAsTag()), + context.getGuild().getJDA().getSelfUser().getAvatarUrl() + ); + context.getEvent() + .getInteraction() + .replyEmbeds(messageUtil.getEmbedBuilder().build()) + .queue(); + } else { + messageUtil.getEmbedBuilder() + .clear() + .clearFields() + .setColor(YELLOW) + .setTitle("Failed to send joke to joke api") + .setDescription("I couldn't send your request at the momentšŸ˜¢.") + .setTimestamp(now(ZoneId.systemDefault())) + .setFooter( + format("Created by %s", context.getGuild().getJDA().getSelfUser().getAsTag()), + context.getGuild().getJDA().getSelfUser().getAvatarUrl() + ); + context.getEvent() + .getInteraction() + .replyEmbeds(messageUtil.getEmbedBuilder().build()) + .queue(); + } + } + + /** + * Supplies the name of the slash command. + * + * @return a {@code Supplier}. + * @see Supplier + */ + @Override + public Supplier name() { + return () -> "submit-joke"; + } + + /** + * Supplies the command data of a slash command. + * + * @return a {@code Supplier}. + * @see Supplier + * @see CommandData + */ + @Override + public Supplier getCommandData() { + return () -> new CommandDataImpl( + name().get(), + description().get()) + .addOptions( + new OptionData(OptionType.STRING, "category", "Category of the joke", true) + .setDescription("Select the category of your joke") + .addChoices(jokesService.getCategories()), + new OptionData(OptionType.STRING, "language", "Language of the joke", true) + .setDescription("Select the language of your joke") + .addChoices(jokesService.getLanguages()), + new OptionData(OptionType.STRING, "joke", "The joke you to submit", true) + .setDescription("Enter your joke") + ); + } + + /** + * Supplies the description of a slash command. + * + * @return a {code Supplier} containing the description of the command. + * @see Supplier + */ + @Override + public Supplier description() { + return () -> "Submit a joke to the bot"; + } +} diff --git a/src/main/java/tech/araopj/springpitzzahhbot/service/BotService.java b/src/main/java/tech/araopj/springpitzzahhbot/service/BotService.java index 7cd3555..fbad143 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/service/BotService.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/service/BotService.java @@ -1,7 +1,7 @@ package tech.araopj.springpitzzahhbot.service; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.confessions.service.ConfessionService; -import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.service.JokesService; +import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.service.JokesService; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.game.service.GameService; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.confessions.Confession; import tech.araopj.springpitzzahhbot.config.moderation.service.MessageCheckerService;