Make trivia functional for a single question

This commit is contained in:
Bea 2022-12-21 03:40:13 +01:00
parent 3a8a44adf0
commit 78f62b5f8d
2 changed files with 43 additions and 12 deletions

View File

@ -4,6 +4,7 @@ import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.json.JSONObject; import org.json.JSONObject;
@ -14,6 +15,7 @@ import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
import wtf.beatrice.hidekobot.util.TriviaUtil; import wtf.beatrice.hidekobot.util.TriviaUtil;
import java.nio.channels.ScatteringByteChannel; import java.nio.channels.ScatteringByteChannel;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -63,25 +65,56 @@ public class TriviaCommand implements MessageCommand
if(TriviaUtil.channelsRunningTrivia.contains(event.getChannel().getId())) if(TriviaUtil.channelsRunningTrivia.contains(event.getChannel().getId()))
{ {
// todo nicer looking // todo nicer looking
// todo: also what if the bot stops (database...?)
// todo: also what if the message is already deleted
Message err = event.getMessage().reply("Trivia is already running here!").complete(); Message err = event.getMessage().reply("Trivia is already running here!").complete();
Cache.getTaskScheduler().schedule(() -> err.delete().queue(), 15, TimeUnit.SECONDS); Cache.getTaskScheduler().schedule(() -> err.delete().queue(), 10, TimeUnit.SECONDS);
return; return;
} else {
// todo nicer looking
event.getMessage().reply("Starting new Trivia session!").queue();
} }
JSONObject triviaJson = TriviaUtil.fetchTrivia();
List<TriviaQuestion> questions = TriviaUtil.getQuestions(triviaJson); //todo null check, rate limiting...
TriviaQuestion first = questions.get(0);
List<Button> answerButtons = new ArrayList<>();
Button correctAnswerButton = Button.primary("trivia_correct", first.correctAnswer());
answerButtons.add(correctAnswerButton);
int i = 0; // we need to add a number because buttons can't have the same id
for(String wrongAnswer : first.wrongAnswers())
{
i++;
Button wrongAnswerButton = Button.primary("trivia_wrong_" + i, wrongAnswer);
answerButtons.add(wrongAnswerButton);
}
Collections.shuffle(answerButtons);
EmbedBuilder embedBuilder = new EmbedBuilder(); EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Cache.getBotColor()); embedBuilder.setColor(Cache.getBotColor());
embedBuilder.setTitle("Trivia"); 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); embedBuilder.addField("Question", first.question(), false);
event.getChannel().sendMessageEmbeds(embedBuilder.build()).queue(); Message botMessage = event.getChannel()
.sendMessageEmbeds(embedBuilder.build())
.setActionRow(answerButtons)
.complete();
Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getAuthor());
// todo: ^ we should get rid of this tracking, since we don't need to know who started the trivia.
// todo: however, for now, that's the only way to avoid a thread-locking scenario as some data is
// todo: only stored in that table. this should be solved when we merge / fix the two main tables.
// todo: then, we can remove this instruction.
Cache.getDatabaseSource().queueDisabling(botMessage);
TriviaUtil.channelsRunningTrivia.add(event.getChannel().getId());
} }
} }

View File

@ -67,19 +67,18 @@ public class ExpiredMessageTask implements Runnable {
} }
} }
} }
private void disableExpired(String messageId) private void disableExpired(String messageId)
{ {
String channelId = databaseSource.getQueuedExpiringMessageChannel(messageId); String channelId = databaseSource.getQueuedExpiringMessageChannel(messageId);
// todo: warning, the following method + related if check are thread-locking.
// todo: we should probably merge the two tables somehow, since they have redundant information.
ChannelType msgChannelType = databaseSource.getTrackedMessageChannelType(messageId); ChannelType msgChannelType = databaseSource.getTrackedMessageChannelType(messageId);
MessageChannel textChannel = null; MessageChannel textChannel = null;
// this should never happen, but only message channels are supported. // this should never happen, but only message channels are supported.
if(!msgChannelType.isMessage()) if(!msgChannelType.isMessage())
{ {
@ -91,7 +90,7 @@ public class ExpiredMessageTask implements Runnable {
if(!(msgChannelType.isGuild())) if(!(msgChannelType.isGuild()))
{ {
String userId = databaseSource.getTrackedReplyUserId(messageId); String userId = databaseSource.getTrackedReplyUserId(messageId);
User user = HidekoBot.getAPI().retrieveUserById(userId).complete(); User user = userId == null ? null : HidekoBot.getAPI().retrieveUserById(userId).complete();
if(user == null) if(user == null)
{ {
// if user is not found, consider it expired // if user is not found, consider it expired
@ -105,7 +104,7 @@ public class ExpiredMessageTask implements Runnable {
else else
{ {
String guildId = databaseSource.getQueuedExpiringMessageGuild(messageId); String guildId = databaseSource.getQueuedExpiringMessageGuild(messageId);
Guild guild = HidekoBot.getAPI().getGuildById(guildId); Guild guild = guildId == null ? null : HidekoBot.getAPI().getGuildById(guildId);
if(guild == null) if(guild == null)
{ {
// if guild is not found, consider it expired // if guild is not found, consider it expired
@ -130,7 +129,6 @@ public class ExpiredMessageTask implements Runnable {
if(Cache.isVerbose()) logger.log("cleaning up: " + messageId); if(Cache.isVerbose()) logger.log("cleaning up: " + messageId);
retrieveAction.queue( retrieveAction.queue(
message -> { message -> {
if(message == null) if(message == null)
{ {