From c9082e84ccaae678ec9faaaaaef2d044b506d407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Wed, 21 Dec 2022 04:09:27 +0100 Subject: [PATCH] Make trivia loop through all questions --- .../commands/message/TriviaCommand.java | 59 +++-------- .../runnables/ExpiredMessageTask.java | 87 +-------------- .../hidekobot/runnables/TriviaTask.java | 97 +++++++++++++++++ .../beatrice/hidekobot/util/CommandUtil.java | 100 ++++++++++++++++++ 4 files changed, 212 insertions(+), 131 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java index eea50ae..336cdc0 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java @@ -1,25 +1,22 @@ 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.entities.channel.Channel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import net.dv8tion.jda.api.interactions.components.buttons.Button; 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.runnables.TriviaTask; import wtf.beatrice.hidekobot.util.TriviaUtil; -import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; public class TriviaCommand implements MessageCommand @@ -62,11 +59,11 @@ public class TriviaCommand implements MessageCommand @Override public void runCommand(MessageReceivedEvent event, String label, String[] args) { - Channel channel = event.getChannel(); + MessageChannel channel = event.getChannel(); if(!(channel instanceof TextChannel)) { - event.getMessage().reply("\uD83D\uDE22 Sorry! Trivia doesn't work in DMs.").queue(); + channel.sendMessage("\uD83D\uDE22 Sorry! Trivia doesn't work in DMs.").queue(); return; } @@ -84,45 +81,15 @@ public class TriviaCommand implements MessageCommand } - JSONObject triviaJson = TriviaUtil.fetchTrivia(); - List questions = TriviaUtil.getQuestions(triviaJson); //todo null check, rate limiting... - TriviaQuestion first = questions.get(0); + TriviaTask triviaTask = new TriviaTask(event.getAuthor(), channel); + ScheduledFuture future = + Cache.getTaskScheduler().scheduleAtFixedRate(triviaTask, + 0, + 10, + TimeUnit.SECONDS); + triviaTask.setScheduledFuture(future); - List