Skip to content

Commit

Permalink
Merge pull request #64 from pitzzahh/patches
Browse files Browse the repository at this point in the history
/submit-joke slash command
  • Loading branch information
pitzzahh authored Feb 25, 2023
2 parents 61f40ea + f683a1a commit 62b1b94
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 34 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,7 +36,7 @@ public interface SlashCommand {
* @return nothing.
* @see Consumer
*/
Consumer<CommandContext> execute() throws InterruptedException, IOException;
Consumer<CommandContext> execute();

/**
* Supplies the name of the slash command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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) { }
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -74,15 +72,12 @@ public Consumer<CommandContext> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<CommandContext> 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<String> 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<String>}.
* @see Supplier
*/
@Override
public Supplier<String> name() {
return () -> "submit-joke";
}

/**
* Supplies the command data of a slash command.
*
* @return a {@code Supplier<CommandData>}.
* @see Supplier
* @see CommandData
*/
@Override
public Supplier<CommandData> 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<String>} containing the description of the command.
* @see Supplier
*/
@Override
public Supplier<String> description() {
return () -> "Submit a joke to the bot";
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 62b1b94

Please sign in to comment.