Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished /approve-joke #70

Merged
merged 7 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,7 +47,10 @@

@Slf4j
pitzzahh marked this conversation as resolved.
Show resolved Hide resolved
@Service
public record JokesService(HttpConfig httpConfig) {
public record JokesService(
SecretService secretService,
HttpConfig httpConfig
) {

public Collection<Category> getCategories() {
var httpResponseCompletableFuture = httpConfig.httpClient()
Expand Down Expand Up @@ -137,7 +141,7 @@ public Collection<Joke> getSubmittedJokes() {
log.error("Error while getting categories", e);
throw new RuntimeException(e);
}
ObjectMapper objectMapper = new ObjectMapper();
var objectMapper = new ObjectMapper();
Collection<Joke> jokes;
try {
jokes = objectMapper.readValue(stringHttpResponse.body(), new TypeReference<>() {});
Expand All @@ -151,4 +155,25 @@ public Collection<Joke> 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<String> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
@Service
public record ChannelService(ChannelsConfig channelsConfig) {

public String verifyChannelName() {
return channelsConfig.getVerifyChannelName();
}

public String getMemberUpdatesChannel() {
return channelsConfig.getMemberUpdatesChannel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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();
}
}