mirror of
https://github.com/Noratrieb/killua-bot.git
synced 2026-01-14 15:15:01 +01:00
reaction optimizations and trivia approval
This commit is contained in:
parent
d1f07f86b5
commit
865b638aca
10 changed files with 137 additions and 150 deletions
|
|
@ -12,6 +12,8 @@ public class Config {
|
|||
|
||||
public static final long THIS_ID = 801015254023798825L;
|
||||
public static final long NILS_ID = 414755070161453076L;
|
||||
public static final long KUKUROO_MOUNTAIN_ID = 799696420386504795L;
|
||||
public static final long TRIVIA_APPROVAL_CHANNEL_ID = 802244298774413312L;
|
||||
|
||||
public static EmbedBuilder getDefaultEmbed() {
|
||||
EmbedBuilder builder = new EmbedBuilder();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,63 @@
|
|||
package com.github.nilstrieb.commands.fun.trivia;
|
||||
|
||||
import com.github.nilstrieb.cofig.Config;
|
||||
import com.github.nilstrieb.reactions.ReactionAdapter;
|
||||
import com.github.nilstrieb.reactions.ReactionEventManager;
|
||||
import com.github.nilstrieb.util.ConsoleColors;
|
||||
import com.github.nilstrieb.util.trivia.TriviaQuestion;
|
||||
import com.github.nilstrieb.util.trivia.TriviaQuestionData;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class TriviaApproval extends ReactionAdapter {
|
||||
|
||||
public static final String APPROVE_EMOTE = "\u2705"; //✅
|
||||
public static final String DENY_EMOTE = "\u274C"; //❌
|
||||
|
||||
private final TriviaQuestion question;
|
||||
|
||||
public TriviaApproval(MessageReceivedEvent event, TriviaQuestion question) {
|
||||
this.question = question;
|
||||
|
||||
EmbedBuilder builder = Config.getDefaultEmbed(event)
|
||||
.setTitle("TRIVIA QUESTION APPROVAL")
|
||||
.addField("Question:", question.getQuestion(), false);
|
||||
for (int i = 0; i < question.getAnswers().length; i++) {
|
||||
builder.addField("Answer " + i, question.getAnswers()[i], false);
|
||||
}
|
||||
builder.addField("Correct:", String.valueOf(question.getCorrectAnswer()), false)
|
||||
.addField("Arc: ", String.valueOf(question.getArc()), false);
|
||||
|
||||
//send message
|
||||
event.getJDA().getGuildById(Config.KUKUROO_MOUNTAIN_ID).getTextChannelById(Config.TRIVIA_APPROVAL_CHANNEL_ID).sendMessage(builder.build()).queue(message -> {
|
||||
message.addReaction(APPROVE_EMOTE).queue();
|
||||
message.addReaction(DENY_EMOTE).queue();
|
||||
create(message.getIdLong());
|
||||
ReactionEventManager.addMessage(message.getIdLong(), this);
|
||||
System.out.println(ConsoleColors.BLUE + "[TriviaApproval 32] Trivia Question registered for approval" + ConsoleColors.RESET);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReactionAdded(MessageReactionAddEvent event) {
|
||||
if (event.getUser() != null) {
|
||||
if (event.getUser().getIdLong() == Config.NILS_ID) {
|
||||
String emote = event.getReaction().getReactionEmote().getName();
|
||||
System.out.println(ConsoleColors.BLUE + "[TriviaApproval 46] Received Emote " + emote + ConsoleColors.RESET);
|
||||
if (emote.equals(APPROVE_EMOTE)) {
|
||||
event.getTextChannel().sendMessage("Question approved.").queue();
|
||||
System.out.println(ConsoleColors.BLUE + "[TriviaApproval 55] Question Approved: " + question.getQuestion() + ConsoleColors.RESET);
|
||||
TriviaQuestionData.add(question);
|
||||
dispose();
|
||||
} else if (emote.equals(DENY_EMOTE)) {
|
||||
event.getTextChannel().sendMessage("Question denied.").queue();
|
||||
System.out.println(ConsoleColors.BLUE + "[TriviaApproval 55] Question Denied: " + question.getQuestion() + ConsoleColors.RESET);
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
package com.github.nilstrieb.commands.fun;
|
||||
package com.github.nilstrieb.commands.fun.trivia;
|
||||
|
||||
import com.github.nilstrieb.cofig.Config;
|
||||
import com.github.nilstrieb.commands.handler.Command;
|
||||
import com.github.nilstrieb.sections.ChannelMessageEventManager;
|
||||
import com.github.nilstrieb.sections.Section;
|
||||
import com.github.nilstrieb.util.ConsoleColors;
|
||||
import com.github.nilstrieb.util.trivia.TriviaQuestion;
|
||||
|
|
@ -13,7 +12,7 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
|||
public class TriviaCommand extends Command {
|
||||
|
||||
public TriviaCommand() {
|
||||
super("trivia", "Answer random Trivia questions!", "trivia 0", "(maximal arc (inclusive) as a number)", """
|
||||
super("trivia", "Answer random Trivia questions!", "trivia 0", "(maximal arc (inclusive) as a number) or (add)", """
|
||||
Answer random trivia questions by the community!
|
||||
You can choose the last arc the questions will be from to avoid spoilers
|
||||
Arcs:
|
||||
|
|
@ -24,7 +23,8 @@ public class TriviaCommand extends Command {
|
|||
4 Greed Island arc
|
||||
5 Chimera Ant arc
|
||||
6 Election arc
|
||||
""");
|
||||
|
||||
Add questions using `""" + Config.PREFIX + "trivia add`");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -110,8 +110,17 @@ public class TriviaCommand extends Command {
|
|||
private int status = 0;
|
||||
private static final String[] messages = {"Enter all answers seperated by a ; (Example: \"Ging;Mito;Gon\")",
|
||||
"Enter the correct answer index starting at 0 (Example: \"0\")",
|
||||
"Enter the arc this question belongs to as a number (see " + Config.PREFIX + "help trivia for more info) (Example: \"0\")"};
|
||||
private String[] answers = new String[4];
|
||||
"""
|
||||
Enter the arc this question belongs to as a number (Example: "0")
|
||||
EXAM = 0
|
||||
ZOLDYCK_FAMILY = 1
|
||||
HEAVENS_ARENA = 2
|
||||
YORKNEW_CITY = 3
|
||||
GREED_ISLAND = 4
|
||||
CHIMERA_ANT = 5
|
||||
ELECTION = 6
|
||||
"""};
|
||||
private final String[] answers = new String[4];
|
||||
|
||||
private AddSection(long textChannelID, long userID) {
|
||||
super(textChannelID, userID);
|
||||
|
|
@ -119,13 +128,13 @@ public class TriviaCommand extends Command {
|
|||
|
||||
@Override
|
||||
public void messageReceived(MessageReceivedEvent event) {
|
||||
if(!event.getMessage().getContentRaw().startsWith(Config.PREFIX + "help")){
|
||||
if (!event.getMessage().getContentRaw().startsWith(Config.PREFIX + "help")) {
|
||||
System.out.println(ConsoleColors.BLUE_BOLD + "[TriviaCommand.AddSection 121] Received Next Message: "
|
||||
+ event.getMessage().getContentRaw() + " status: " + status + ConsoleColors.RESET);
|
||||
answers[status] = event.getMessage().getContentRaw();
|
||||
if (status >= 3) {
|
||||
try {
|
||||
TriviaQuestionData.addNew(new TriviaQuestion(answers));
|
||||
new TriviaApproval(event, new TriviaQuestion(answers));
|
||||
reply(event, "Question successfully added for approval");
|
||||
} catch (NumberFormatException e) {
|
||||
reply(event, "Error: " + e.getMessage());
|
||||
|
|
@ -23,13 +23,13 @@ public abstract class MessageSender {
|
|||
|
||||
protected void reply(MessageReceivedEvent event, MessageEmbed... embeds) {
|
||||
if (!embeds[0].isEmpty()) {
|
||||
event.getTextChannel().sendMessage(embeds[0]).queue(message -> new MultiPageEmbed(message, embeds));
|
||||
new MultiPageEmbed(event, embeds);
|
||||
}
|
||||
}
|
||||
|
||||
protected void reply(MessageReceivedEvent event, String emote1, String emote2, MessageEmbed... embeds) {
|
||||
if (!embeds[0].isEmpty()) {
|
||||
event.getTextChannel().sendMessage(embeds[0]).queue(message -> new MultiPageEmbed(message, emote1, emote2, embeds));
|
||||
new MultiPageEmbed(event, emote1, emote2, embeds);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import com.github.nilstrieb.cofig.Secrets;
|
|||
import com.github.nilstrieb.commands.fun.DepartureCommand;
|
||||
import com.github.nilstrieb.commands.fun.QuoteCommand;
|
||||
import com.github.nilstrieb.commands.fun.SayCommand;
|
||||
import com.github.nilstrieb.commands.fun.TriviaCommand;
|
||||
import com.github.nilstrieb.commands.fun.trivia.TriviaCommand;
|
||||
import com.github.nilstrieb.commands.info.InviteCommand;
|
||||
import com.github.nilstrieb.commands.info.EvalCommand;
|
||||
import com.github.nilstrieb.commands.info.HelpCommand;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
package com.github.nilstrieb.reactions;
|
||||
|
||||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
|
||||
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;
|
||||
|
||||
public abstract class ReactionAdapter implements ReactionListener {
|
||||
|
||||
private long message;
|
||||
|
||||
protected void create(long message) {
|
||||
this.message = message;
|
||||
ReactionEventManager.addMessage(message, this);
|
||||
}
|
||||
|
||||
protected void dispose() {
|
||||
ReactionEventManager.removeMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReactionAdded(MessageReactionAddEvent event) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReactionRemoved(MessageReactionRemoveEvent event) {
|
||||
}
|
||||
|
||||
public long getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(long message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ public class ReactionEventManager {
|
|||
currentReactions.put(message, listener);
|
||||
}
|
||||
|
||||
public static void removeMessage(String message){
|
||||
public static void removeMessage(Long message){
|
||||
currentReactions.remove(message);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,40 +1,38 @@
|
|||
package com.github.nilstrieb.util;
|
||||
|
||||
import com.github.nilstrieb.reactions.ReactionEventManager;
|
||||
import com.github.nilstrieb.reactions.ReactionListener;
|
||||
import com.github.nilstrieb.reactions.ReactionAdapter;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
|
||||
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class MultiPageEmbed implements ReactionListener {
|
||||
public class MultiPageEmbed extends ReactionAdapter {
|
||||
private static final String NEXT_PAGE_DEFAULT_REACTION = "\u25b6\ufe0f";
|
||||
private static final String PREVIOUS_PAGE_DEFAULT_REACTION = "\u25c0\ufe0f";
|
||||
|
||||
private final Message message;
|
||||
private Message message;
|
||||
private final MessageEmbed[] pages;
|
||||
private int currentState;
|
||||
private final String prevReaction;
|
||||
private final String nextReaction;
|
||||
|
||||
public MultiPageEmbed(Message message, MessageEmbed... pages) {
|
||||
this(message, PREVIOUS_PAGE_DEFAULT_REACTION, NEXT_PAGE_DEFAULT_REACTION, pages);
|
||||
|
||||
public MultiPageEmbed(MessageReceivedEvent event, MessageEmbed... pages) {
|
||||
this(event, PREVIOUS_PAGE_DEFAULT_REACTION, NEXT_PAGE_DEFAULT_REACTION, pages);
|
||||
}
|
||||
|
||||
public MultiPageEmbed(Message message, String prevReaction, String nextReaction, MessageEmbed... pages) {
|
||||
this.message = message;
|
||||
public MultiPageEmbed(MessageReceivedEvent event, String prevReaction, String nextReaction, MessageEmbed[] pages) {
|
||||
this.prevReaction = prevReaction;
|
||||
this.nextReaction = nextReaction;
|
||||
|
||||
this.pages = pages;
|
||||
currentState = 0;
|
||||
|
||||
message.addReaction(prevReaction).queue();
|
||||
message.addReaction(nextReaction).queue();
|
||||
|
||||
ReactionEventManager.addMessage(message.getIdLong(), this);
|
||||
event.getTextChannel().sendMessage(pages[0]).queue(message1 -> {
|
||||
message = message1;
|
||||
message.addReaction(prevReaction).queue();
|
||||
message.addReaction(nextReaction).queue();
|
||||
create(message1.getIdLong());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -55,12 +53,7 @@ public class MultiPageEmbed implements ReactionListener {
|
|||
}
|
||||
}
|
||||
}
|
||||
Objects.requireNonNull(event.getUser(), "[MultiPageEmbed] reaction user was null");
|
||||
Objects.requireNonNull(event.getUser(), "Reaction user was null");
|
||||
event.getReaction().removeReaction(event.getUser()).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReactionRemoved(MessageReactionRemoveEvent event) {
|
||||
//leave empty
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.github.nilstrieb.util.trivia;
|
|||
|
||||
import com.github.nilstrieb.util.ConsoleColors;
|
||||
import com.google.gson.Gson;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -79,12 +80,6 @@ public class TriviaQuestionData {
|
|||
saveJSONFromAll("trivia_questions.json");
|
||||
}
|
||||
|
||||
public static void addNew(TriviaQuestion triviaQuestion) {
|
||||
newQuestions.add(triviaQuestion);
|
||||
TriviaQuestion[] array = new TriviaQuestion[newQuestions.size()];
|
||||
saveJSON("new_trivia_questions.json", newQuestions.toArray(array));
|
||||
}
|
||||
|
||||
public static TriviaQuestion getQuestion(int toArc) {
|
||||
int totalQuestions = getTotalQuestions(toArc);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue