Make trivia functional for a single question
This commit is contained in:
parent
3a8a44adf0
commit
78f62b5f8d
@ -4,6 +4,7 @@ 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 net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.JSONObject;
|
||||
@ -14,6 +15,7 @@ import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||
import wtf.beatrice.hidekobot.util.TriviaUtil;
|
||||
|
||||
import java.nio.channels.ScatteringByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -63,25 +65,56 @@ public class TriviaCommand implements MessageCommand
|
||||
if(TriviaUtil.channelsRunningTrivia.contains(event.getChannel().getId()))
|
||||
{
|
||||
// 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();
|
||||
Cache.getTaskScheduler().schedule(() -> err.delete().queue(), 15, TimeUnit.SECONDS);
|
||||
Cache.getTaskScheduler().schedule(() -> err.delete().queue(), 10, TimeUnit.SECONDS);
|
||||
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.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();
|
||||
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());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -67,19 +67,18 @@ public class ExpiredMessageTask implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void disableExpired(String 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);
|
||||
|
||||
MessageChannel textChannel = null;
|
||||
|
||||
|
||||
// this should never happen, but only message channels are supported.
|
||||
if(!msgChannelType.isMessage())
|
||||
{
|
||||
@ -91,7 +90,7 @@ public class ExpiredMessageTask implements Runnable {
|
||||
if(!(msgChannelType.isGuild()))
|
||||
{
|
||||
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 is not found, consider it expired
|
||||
@ -105,7 +104,7 @@ public class ExpiredMessageTask implements Runnable {
|
||||
else
|
||||
{
|
||||
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 is not found, consider it expired
|
||||
@ -130,7 +129,6 @@ public class ExpiredMessageTask implements Runnable {
|
||||
if(Cache.isVerbose()) logger.log("cleaning up: " + messageId);
|
||||
|
||||
retrieveAction.queue(
|
||||
|
||||
message -> {
|
||||
if(message == null)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user