-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from TosoxDev/development
feat: german words for Hangman
- Loading branch information
Showing
11 changed files
with
195 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.tosoxdev.tosoxjr.commands.joke; | ||
|
||
import de.tosoxdev.tosoxjr.utils.APIRequest; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
public class Joke { | ||
private final static String PUN_TEMPLATE = "%s\n... %s"; | ||
|
||
public static String getGeneral() { | ||
JSONArray response = (JSONArray) APIRequest.getJson("https://official-joke-api.appspot.com/jokes/general/random"); | ||
if ((response == null) || (response.isEmpty())) { | ||
return null; | ||
} | ||
|
||
String setup = response.getJSONObject(0).getString("setup"); | ||
String punchline = response.getJSONObject(0).getString("punchline"); | ||
return String.format(PUN_TEMPLATE, setup, punchline); | ||
} | ||
|
||
public static String getProgramming() { | ||
JSONArray response = (JSONArray) APIRequest.getJson("https://official-joke-api.appspot.com/jokes/programming/random"); | ||
if ((response == null) || (response.isEmpty())) { | ||
return null; | ||
} | ||
|
||
String setup = response.getJSONObject(0).getString("setup"); | ||
String punchline = response.getJSONObject(0).getString("punchline"); | ||
return String.format(PUN_TEMPLATE, setup, punchline); | ||
} | ||
|
||
public static String getChuckNorris() { | ||
JSONObject response = (JSONObject) APIRequest.getJson("https://api.chucknorris.io/jokes/random"); | ||
if (response == null) { | ||
return null; | ||
} | ||
|
||
return response.getString("value"); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/de/tosoxdev/tosoxjr/commands/joke/JokeCmd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package de.tosoxdev.tosoxjr.commands.joke; | ||
|
||
import de.tosoxdev.tosoxjr.Main; | ||
import de.tosoxdev.tosoxjr.commands.CommandBase; | ||
import de.tosoxdev.tosoxjr.utils.ArgumentParser; | ||
import de.tosoxdev.tosoxjr.utils.Utils; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
import net.dv8tion.jda.api.interactions.commands.build.OptionData; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public class JokeCmd extends CommandBase { | ||
private final HashMap<String, Callable<String>> categories = new HashMap<>(Map.of( | ||
"general", Joke::getGeneral, | ||
"programming", Joke::getProgramming, | ||
"chuck-norris", Joke::getChuckNorris | ||
)); | ||
private final String categoriesList; | ||
|
||
public JokeCmd() { | ||
super("joke", "Show a random joke", List.of( | ||
new OptionData(OptionType.STRING, "category", "List all available categories with 'list'", false) | ||
)); | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
categories.forEach((key, value) -> sb.append(String.format("- %s\n", key))); | ||
categoriesList = sb.toString(); | ||
} | ||
|
||
@Override | ||
public void handle(SlashCommandInteractionEvent event) { | ||
String category = ArgumentParser.getString(event.getOption("category"), null); | ||
if (category == null) { | ||
// Get random joke | ||
int randomIdx = ThreadLocalRandom.current().nextInt(categories.size()); | ||
String randomCategory = (String) categories.keySet().toArray()[randomIdx]; | ||
Callable<String> callable = categories.get(randomCategory); | ||
|
||
String joke = Utils.getStringFromCallable(callable); | ||
if (joke == null) { | ||
Main.getLogger().error("The callable didn't return a value when trying to run 'joke'"); | ||
return; | ||
} | ||
|
||
event.reply(joke).queue(); | ||
return; | ||
} | ||
|
||
if (category.equalsIgnoreCase("list")) { | ||
String msg = String.format("Available categories\n%s", categoriesList); | ||
event.reply(msg).queue(); | ||
return; | ||
} | ||
|
||
Callable<String> callable = categories.get(category); | ||
if (callable == null) { | ||
String msg = String.format("There are no jokes for '%s'", category); | ||
event.reply(msg).queue(); | ||
return; | ||
} | ||
|
||
String joke = Utils.getStringFromCallable(callable); | ||
if (joke == null) { | ||
Main.getLogger().error("The callable didn't return a value when trying to run 'joke'"); | ||
return; | ||
} | ||
|
||
event.reply(joke).queue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.