Start implementing trivia command
This commit is contained in:
parent
e451f59199
commit
b0622f36aa
@ -145,6 +145,7 @@ public class HidekoBot
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.LoveCalculatorCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.MagicBallCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.SayCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.TriviaCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.UrbanDictionaryCommand());
|
||||
Cache.setMessageCommandListener(messageCommandListener);
|
||||
|
||||
|
@ -0,0 +1,87 @@
|
||||
package wtf.beatrice.hidekobot.commands.message;
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.JSONObject;
|
||||
import wtf.beatrice.hidekobot.Cache;
|
||||
import wtf.beatrice.hidekobot.objects.TriviaQuestion;
|
||||
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
||||
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||
import wtf.beatrice.hidekobot.util.TriviaUtil;
|
||||
|
||||
import java.nio.channels.ScatteringByteChannel;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TriviaCommand implements MessageCommand
|
||||
{
|
||||
|
||||
@Override
|
||||
public LinkedList<String> getCommandLabels() {
|
||||
return new LinkedList<>(Collections.singletonList("trivia"));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public List<Permission> getPermissions() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean passRawArgs() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
return CommandCategory.FUN;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Start a Trivia session and play with others!";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runCommand(MessageReceivedEvent event, String label, String[] args)
|
||||
{
|
||||
|
||||
if(TriviaUtil.channelsRunningTrivia.contains(event.getChannel().getId()))
|
||||
{
|
||||
// todo nicer looking
|
||||
Message err = event.getMessage().reply("Trivia is already running here!").complete();
|
||||
Cache.getTaskScheduler().schedule(() -> err.delete().queue(), 15, TimeUnit.SECONDS);
|
||||
return;
|
||||
}
|
||||
|
||||
EmbedBuilder embedBuilder = new EmbedBuilder();
|
||||
|
||||
embedBuilder.setColor(Cache.getBotColor());
|
||||
embedBuilder.setTitle("Trivia");
|
||||
|
||||
JSONObject triviaJson = TriviaUtil.fetchTrivia();
|
||||
List<TriviaQuestion> questions = TriviaUtil.getQuestions(triviaJson); //todo null check, rate limiting...
|
||||
TriviaQuestion first = questions.get(0);
|
||||
|
||||
embedBuilder.addField("Question", first.question(), false);
|
||||
|
||||
event.getChannel().sendMessageEmbeds(embedBuilder.build()).queue();
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package wtf.beatrice.hidekobot.objects;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record TriviaQuestion(String question, String correctAnswer,
|
||||
List<String> wrongAnswers) {
|
||||
|
||||
}
|
74
src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java
Normal file
74
src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java
Normal file
@ -0,0 +1,74 @@
|
||||
package wtf.beatrice.hidekobot.util;
|
||||
|
||||
import org.apache.commons.text.StringEscapeUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONString;
|
||||
import org.jsoup.Jsoup;
|
||||
import wtf.beatrice.hidekobot.objects.TriviaQuestion;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TriviaUtil
|
||||
{
|
||||
private final static String link = "https://opentdb.com/api.php?amount=10";
|
||||
|
||||
public static List<String> channelsRunningTrivia = new ArrayList<>();
|
||||
|
||||
public static JSONObject fetchTrivia()
|
||||
{
|
||||
try {
|
||||
URL url = new URL(link);
|
||||
URLConnection connection = url.openConnection();
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
|
||||
String currentChar;
|
||||
StringBuilder jsonStrBuilder = new StringBuilder();
|
||||
while((currentChar = bufferedReader.readLine()) != null)
|
||||
{
|
||||
jsonStrBuilder.append(currentChar);
|
||||
}
|
||||
bufferedReader.close();
|
||||
return new JSONObject(jsonStrBuilder.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<TriviaQuestion> getQuestions(JSONObject jsonObject)
|
||||
{
|
||||
List<TriviaQuestion> questions = new ArrayList<>();
|
||||
|
||||
JSONArray results = jsonObject.getJSONArray("results");
|
||||
|
||||
for(Object currentQuestionGeneric : results)
|
||||
{
|
||||
JSONObject questionJson = (JSONObject) currentQuestionGeneric;
|
||||
String question = StringEscapeUtils.unescapeHtml4(questionJson.getString("question"));
|
||||
String correctAnswer = StringEscapeUtils.unescapeHtml4(questionJson.getString("correct_answer"));
|
||||
|
||||
List<String> incorrectAnswersList = new ArrayList<>();
|
||||
|
||||
JSONArray incorrectAnswers = questionJson.getJSONArray("incorrect_answers");
|
||||
for(Object incorrectAnswerGeneric : incorrectAnswers)
|
||||
{
|
||||
String incorrectAnswer = (String) incorrectAnswerGeneric;
|
||||
incorrectAnswersList.add(StringEscapeUtils.unescapeHtml4(incorrectAnswer));
|
||||
}
|
||||
|
||||
TriviaQuestion triviaQuestion = new TriviaQuestion(question, correctAnswer, incorrectAnswersList);
|
||||
questions.add(triviaQuestion);
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user