reaction optimizations and trivia approval

This commit is contained in:
nora 2021-01-22 20:15:05 +01:00
parent d1f07f86b5
commit 865b638aca
10 changed files with 137 additions and 150 deletions

View file

@ -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();

View file

@ -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();
}
}
}
}
}

View file

@ -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());

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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);
}

View file

@ -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
}
}

View file

@ -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);

View file

@ -1,110 +1 @@
[
{
"question": "What\u0027s the name of Gons aunt?",
"answers": [
"Kite",
"Mito",
"Ging",
"Kurapika"
],
"correctAnswer": 1,
"arc": 0
},
{
"question": "Why does Leorio want money?",
"answers": [
"To become a doctor",
"To send it to e-girls",
"So that others don\u0027t get it",
"To buy large quantities of drugs"
],
"correctAnswer": 0,
"arc": 0
},
{
"question": "What was Gon\u0027s aim after Killua left him alone playing with the Chairman Netero?",
"answers": [
"Get the ball",
"Make Netero use his right hand",
"Make Netero use his left leg",
"Let Netero drop the ball"
],
"correctAnswer": 1,
"arc": 0
},
{
"question": "What did Tonpa mix into the drink?",
"answers": [
"Sleeping pills",
"Headache tabletsschla",
"Laxative",
"Rat poison"
],
"correctAnswer": 2,
"arc": 0
},
{
"question": "How many doors are there at the Testing Gate of the Zoldyck Family?",
"answers": [
"7",
"5",
"12",
"8"
],
"correctAnswer": 0,
"arc": 1
},
{
"question": "What is Bisky\u0027s true form?",
"answers": [
"Boy",
"Big Woman",
"Little Girl"
],
"correctAnswer": 1,
"arc": 4
},
{
"question": "How did Gon die on Greed Island?",
"answers": [
"He didn\u0027t die on Greed Island",
"Hisoka killed him with a card to the neck",
"He lost too muich blood when Genthru blew his arm of",
"He killed himself after Killuas death"
],
"correctAnswer": 0,
"arc": 4
},
{
"question": "Where did Meruem die?",
"answers": [
"Volcano",
"In the desert with Netero",
"In the basement of the palace",
"In the womb of the queen"
],
"correctAnswer": 2,
"arc": 5
},
{
"question": "Who is Alluka?",
"answers": [
"Gon\u0027s sister",
"A Zodiac",
"The \u0027dark\u0027 side of Nanika",
"Killua\u0027s sister"
],
"correctAnswer": 3,
"arc": 6
},
{
"question": "What is Genthrus ability called?",
"answers": [
"Nuke",
"Massive Explosion",
"Little Flower"
],
"correctAnswer": 2,
"arc": 4
}
]
[{"question":"What\u0027s the name of Gons aunt?","answers":["Kite","Mito","Ging","Kurapika"],"correctAnswer":1,"arc":0},{"question":"Why does Leorio want money?","answers":["To become a doctor","To send it to e-girls","So that others don\u0027t get it","To buy large quantities of drugs"],"correctAnswer":0,"arc":0},{"question":"What was Gon\u0027s aim after Killua left him alone playing with the Chairman Netero?","answers":["Get the ball","Make Netero use his right hand","Make Netero use his left leg","Let Netero drop the ball"],"correctAnswer":1,"arc":0},{"question":"What did Tonpa mix into the drink?","answers":["Sleeping pills","Headache tabletsschla","Laxative","Rat poison"],"correctAnswer":2,"arc":0},{"question":"How many doors are there at the Testing Gate of the Zoldyck Family?","answers":["7","5","12","8"],"correctAnswer":0,"arc":1},{"question":"How many tons did Killua manage to move when he entered Kukooro Mountain","answers":["14","16","32","64"],"correctAnswer":1,"arc":1},{"question":"What did Kurapika bring to his fight with Uvo?","answers":["Shovel","Bomb","Gon"],"correctAnswer":0,"arc":3},{"question":"What is Bisky\u0027s true form?","answers":["Boy","Big Woman","Little Girl"],"correctAnswer":1,"arc":4},{"question":"How did Gon die on Greed Island?","answers":["He didn\u0027t die on Greed Island","Hisoka killed him with a card to the neck","He lost too muich blood when Genthru blew his arm of","He killed himself after Killuas death"],"correctAnswer":0,"arc":4},{"question":"What is Genthrus ability called?","answers":["Nuke","Massive Explosion","Little Flower"],"correctAnswer":2,"arc":4},{"question":"Where did Meruem die?","answers":["Volcano","In the desert with Netero","In the basement of the palace","In the womb of the queen"],"correctAnswer":2,"arc":5},{"question":"What is Pitou\u0027s full name?","answers":["Pitoue","Youpitou","Neferpitou","Pitorius"],"correctAnswer":2,"arc":5},{"question":"Who is Alluka?","answers":["Gon\u0027s sister","A Zodiac","The \u0027dark\u0027 side of Nanika","Killua\u0027s sister"],"correctAnswer":3,"arc":6},{"question":"Who won the election?","answers":["Gon","Pariston","Morel","Leorio"],"correctAnswer":1,"arc":6},{"question":"haha","answers":["never","immer"],"correctAnswer":1,"arc":6}]