From 39d073c5f96c7d6aea085e809327cbde1ddcf78d Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 26 Feb 2023 21:14:12 +0800 Subject: [PATCH 1/7] refactor: removed verifyChannel name config --- .../springpitzzahhbot/config/channels/ChannelsConfig.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/tech/araopj/springpitzzahhbot/config/channels/ChannelsConfig.java b/src/main/java/tech/araopj/springpitzzahhbot/config/channels/ChannelsConfig.java index 0c1c180..5bb39ff 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/config/channels/ChannelsConfig.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/config/channels/ChannelsConfig.java @@ -32,9 +32,6 @@ @Configuration public class ChannelsConfig { - @Value("${bot.channel.verification.name}") - private String verifyChannelName; - @Value("${bot.channel.member-updates-channel.name}") private String memberUpdatesChannel; From 004508397c9ddbfb857111a2e979413a34d94994 Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 26 Feb 2023 21:14:47 +0800 Subject: [PATCH 2/7] refactor: removed verifyChannelName method --- .../config/channels/service/ChannelService.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/tech/araopj/springpitzzahhbot/config/channels/service/ChannelService.java b/src/main/java/tech/araopj/springpitzzahhbot/config/channels/service/ChannelService.java index d7c967c..295849c 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/config/channels/service/ChannelService.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/config/channels/service/ChannelService.java @@ -37,10 +37,6 @@ @Service public record ChannelService(ChannelsConfig channelsConfig) { - public String verifyChannelName() { - return channelsConfig.getVerifyChannelName(); - } - public String getMemberUpdatesChannel() { return channelsConfig.getMemberUpdatesChannel(); } From e653adfc10c6dab469d462faae60f3825a647bff Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 26 Feb 2023 22:34:28 +0800 Subject: [PATCH 3/7] add: JokeBody for approving jokes --- .../joke/approveJoke/entity/JokeBody.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/approveJoke/entity/JokeBody.java diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/approveJoke/entity/JokeBody.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/approveJoke/entity/JokeBody.java new file mode 100644 index 0000000..d09135a --- /dev/null +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/approveJoke/entity/JokeBody.java @@ -0,0 +1,31 @@ +/* + * 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.approveJoke.entity; + +import lombok.Builder; + +@Builder +public record JokeBody(String key, String id) { +} From d44c8e1fd7b26be5ed7415ae382a578948188f92 Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 26 Feb 2023 22:53:10 +0800 Subject: [PATCH 4/7] add: config class for getting jokes secrets --- .../config/secret/SecretConfig.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/tech/araopj/springpitzzahhbot/config/secret/SecretConfig.java diff --git a/src/main/java/tech/araopj/springpitzzahhbot/config/secret/SecretConfig.java b/src/main/java/tech/araopj/springpitzzahhbot/config/secret/SecretConfig.java new file mode 100644 index 0000000..23b95df --- /dev/null +++ b/src/main/java/tech/araopj/springpitzzahhbot/config/secret/SecretConfig.java @@ -0,0 +1,36 @@ +/* + * 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.config.secret; + +import lombok.Getter; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +@Getter +@Configuration +public class SecretConfig { + @Value("${joke-api.secret.key}") + private String key; +} From 4a251f5ecec66aa577f851f61c608b0553e6f6a8 Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 26 Feb 2023 23:06:55 +0800 Subject: [PATCH 5/7] add: service for getting jokes api secrets --- .../config/secret/service/SecretService.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/tech/araopj/springpitzzahhbot/config/secret/service/SecretService.java diff --git a/src/main/java/tech/araopj/springpitzzahhbot/config/secret/service/SecretService.java b/src/main/java/tech/araopj/springpitzzahhbot/config/secret/service/SecretService.java new file mode 100644 index 0000000..79cba90 --- /dev/null +++ b/src/main/java/tech/araopj/springpitzzahhbot/config/secret/service/SecretService.java @@ -0,0 +1,35 @@ +/* + * 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.config.secret.service; + +import tech.araopj.springpitzzahhbot.config.secret.SecretConfig; +import org.springframework.stereotype.Service; + +@Service +public record SecretService(SecretConfig secretConfig) { + public String getKey() { + return secretConfig().getKey(); + } +} From 46e9bd39502e7799fba289bb6e2cd6fdb3c3fc23 Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 26 Feb 2023 23:09:04 +0800 Subject: [PATCH 6/7] add: dependency injection and approving joke service method. #66 --- .../commands/joke/service/JokesService.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/service/JokesService.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/service/JokesService.java index ac5659a..556817a 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/service/JokesService.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/service/JokesService.java @@ -24,8 +24,9 @@ package tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.service; +import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.approveJoke.entity.*; import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.entity.*; -import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.approveJoke.entity.Joke; +import tech.araopj.springpitzzahhbot.config.secret.service.SecretService; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import com.fasterxml.jackson.core.JsonProcessingException; import tech.araopj.springpitzzahhbot.config.HttpConfig; @@ -46,7 +47,10 @@ @Slf4j @Service -public record JokesService(HttpConfig httpConfig) { +public record JokesService( + SecretService secretService, + HttpConfig httpConfig +) { public Collection getCategories() { var httpResponseCompletableFuture = httpConfig.httpClient() @@ -137,7 +141,7 @@ public Collection getSubmittedJokes() { log.error("Error while getting categories", e); throw new RuntimeException(e); } - ObjectMapper objectMapper = new ObjectMapper(); + var objectMapper = new ObjectMapper(); Collection jokes; try { jokes = objectMapper.readValue(stringHttpResponse.body(), new TypeReference<>() {}); @@ -151,4 +155,25 @@ public Collection getSubmittedJokes() { .filter(j -> !j.approved()) .collect(Collectors.toCollection(ArrayList::new)); } + + public boolean approveJoke(Joke joke) { + var httpResponseCompletableFuture = httpConfig.httpClient() + .sendAsync(HttpRequest.newBuilder() + .PUT(HttpRequest.BodyPublishers.ofString(new Gson().toJson(new JokeBody(secretService().getKey(), joke.joke())))) + .uri(URI.create("https://jokes.araopj.tech/v1/submit/approve")) + .build(), + HttpResponse.BodyHandlers.ofString() + ); + HttpResponse stringHttpResponse; + + try { + stringHttpResponse = httpResponseCompletableFuture.get(); + } catch (InterruptedException | ExecutionException e) { + log.error("Error while approving joke", e); + throw new RuntimeException(e); + } + + log.info("Approve Joke Response: {}", stringHttpResponse.body()); + return stringHttpResponse.body().equals("Joke approved successfully"); + } } From 59c70292df3e22d175df84d90698ee374f851675 Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 26 Feb 2023 23:10:28 +0800 Subject: [PATCH 7/7] add: business logic for approving joke request #66 --- .../joke/approveJoke/ApproveJoke.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/approveJoke/ApproveJoke.java b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/approveJoke/ApproveJoke.java index e59c5d7..7f50961 100644 --- a/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/approveJoke/ApproveJoke.java +++ b/src/main/java/tech/araopj/springpitzzahhbot/commands/slash_command/commands/joke/approveJoke/ApproveJoke.java @@ -30,6 +30,7 @@ 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.api.interactions.commands.OptionMapping; import net.dv8tion.jda.internal.interactions.CommandDataImpl; import net.dv8tion.jda.api.interactions.commands.OptionType; import org.springframework.stereotype.Component; @@ -72,6 +73,52 @@ private void process(CommandContext context) { context.getMember().isOwner() ); log.info("Is user {} an admin? and can manage this server?: {}", context.getMember().getAsMention(), isAdmin); + if (isAdmin) { + + OptionMapping idOption = context.getEvent().getOption("joke-id"); + if (idOption != null) { + log.info("Joke id: {}", idOption.getAsString()); + boolean noJokeWithId = jokesService.getSubmittedJokes() + .stream() + .noneMatch(j -> j.id() == Integer.parseInt(idOption.getAsString())); + if (noJokeWithId) { + log.info("No joke with id: {}", idOption.getAsString()); + messageUtilService.generateAutoDeleteMessage( + context.event(), + YELLOW, + "Testing", + String.format("No joke with id %s", idOption.getAsString()) + ); + } else { + log.info("Joke with id: {} found", idOption.getAsString()); + jokesService + .getSubmittedJokes() + .forEach(j -> { + if (j.id() == Integer.parseInt(idOption.getAsString())) { + log.info(String.format("Joke with id %s has been approved", idOption.getAsString())); + boolean isApproved = jokesService.approveJoke(j); + if (isApproved) { + messageUtilService.generateAutoDeleteMessage( + context.event(), + YELLOW, + "Testing", + String.format("Joke with id %s has been approved", idOption.getAsString()) + ); + } else { + log.info(String.format("Joke with id %s has not been approved", idOption.getAsString())); + messageUtilService.generateAutoDeleteMessage( + context.event(), + YELLOW, + "Testing", + String.format("Joke with id %s has not been approved", idOption.getAsString()) + ); + } + } + }); + } + } + + } messageUtilService.generateAutoDeleteMessage( context.event(), YELLOW,