From 381081fc81df60ce40dc6b6cd7a0c70da199e440 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Wed, 20 Jan 2021 22:05:39 +0100 Subject: [PATCH] channel message thing getter listener thing --- .../nilstrieb/commands/fun/TriviaCommand.java | 44 +++++++++++++ .../nilstrieb/commands/handler/Command.java | 64 ++++++++++++++++--- .../commands/handler/CommandHandler.java | 3 +- .../nilstrieb/commands/info/EvalCommand.java | 2 +- .../java/com/github/nilstrieb/core/Main.java | 5 +- .../listener/ChannelMessageListener.java | 15 +++++ .../nilstrieb/listener/CommandListener.java | 2 +- .../nilstrieb/listener/StartUpListener.java | 3 +- .../nilstrieb/sections/ChannelListener.java | 9 +++ .../sections/ChannelMessageEventManager.java | 55 ++++++++++++++++ .../github/nilstrieb/sections/Section.java | 34 ++++++++++ .../com/github/nilstrieb/util/trivia/Arc.java | 15 +++++ .../nilstrieb/util/trivia/TriviaQuestion.java | 31 +++++++++ .../util/trivia/TriviaQuestionData.java | 64 +++++++++++++++++++ 14 files changed, 332 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/github/nilstrieb/commands/fun/TriviaCommand.java create mode 100644 src/main/java/com/github/nilstrieb/listener/ChannelMessageListener.java create mode 100644 src/main/java/com/github/nilstrieb/sections/ChannelListener.java create mode 100644 src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java create mode 100644 src/main/java/com/github/nilstrieb/sections/Section.java create mode 100644 src/main/java/com/github/nilstrieb/util/trivia/Arc.java create mode 100644 src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestion.java create mode 100644 src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestionData.java diff --git a/src/main/java/com/github/nilstrieb/commands/fun/TriviaCommand.java b/src/main/java/com/github/nilstrieb/commands/fun/TriviaCommand.java new file mode 100644 index 0000000..e5ba9dc --- /dev/null +++ b/src/main/java/com/github/nilstrieb/commands/fun/TriviaCommand.java @@ -0,0 +1,44 @@ +package com.github.nilstrieb.commands.fun; + +import com.github.nilstrieb.commands.handler.Command; +import com.github.nilstrieb.sections.ChannelMessageEventManager; +import com.github.nilstrieb.sections.Section; +import com.github.nilstrieb.util.trivia.TriviaQuestionData; +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)", """ + Answer random trivia questions by the community! + You can choose the last arc the questions will be from to avoid spoilers + Arcs: + 0 Hunter Exam arc + 1 Zoldyck Family arc + 2 Heavens Arena arc + 3 Yorknew City arc + 4 Greed Island arc + 5 Chimera Ant arc + 6 Election arc"" + """); + } + + @Override + public void called(MessageReceivedEvent event, String args) { + int arc = 6; + try { + arc = Integer.parseInt(args); + } catch (NumberFormatException ignored) { + } + + reply(event, TriviaQuestionData.getQuestion(arc).getQuestion()); + + new Section(event.getTextChannel().getIdLong(), 414755070161453076L) { + @Override + public void messageReceived(MessageReceivedEvent event) { + event.getTextChannel().sendMessage("hallo ich bin killua").queue(); + ChannelMessageEventManager.removeListener(this); + } + }; + } +} diff --git a/src/main/java/com/github/nilstrieb/commands/handler/Command.java b/src/main/java/com/github/nilstrieb/commands/handler/Command.java index 490d18e..0278049 100644 --- a/src/main/java/com/github/nilstrieb/commands/handler/Command.java +++ b/src/main/java/com/github/nilstrieb/commands/handler/Command.java @@ -13,24 +13,66 @@ public abstract class Command { private final String description; private final String exampleUsage; private final String arguments; + private final String detailDescription; private final CommandParser parser = CommandParser.getInstance(); - public Command(String name, String description, String exampleUsage, String arguments, boolean hidden) { + + /** + * New command + * @param name command name + * @param description quick description + * @param exampleUsage example usage without the prefix + * @param arguments all arguments (() -> optional. <> -> required + * @param detailDescription a detailed description + */ + public Command(String name, String description, String exampleUsage, String arguments, String detailDescription) { this.name = name; this.description = description; this.exampleUsage = exampleUsage; this.arguments = arguments; + this.detailDescription = detailDescription; + CommandHandler.addCommand(name, this, false); + } + + /** + * New command + * @param name command name + * @param description quick description + * @param exampleUsage example usage without the prefix + * @param arguments all arguments (() -> optional. <> -> required + */ + public Command(String name, String description, String exampleUsage, String arguments){ + this(name, description, exampleUsage, arguments, ""); + } + + /** + * New command + * @param name command name + * @param description quick description + */ + public Command(String name, String description) { + this(name, description, name, "", ""); + } + + /** + * Hidden command + * @param name name + * @param hidden should always be true + */ + public Command(String name, boolean hidden) { + this.name = name; + this.description = ""; + this.exampleUsage = ""; + this.arguments = ""; + this.detailDescription = ""; CommandHandler.addCommand(name, this, hidden); } - public Command(String name, String description, String exampleUsage, String arguments) { - this(name, description, exampleUsage, arguments, false); - } - - public Command(String name, String description) { - this(name, description, name, "", false); - } - + /** + * The method called by the CommandHandler + * @param event The event + * @param args The arguments (after the command and an optional whitespace) + */ public abstract void called(MessageReceivedEvent event, String args); protected void reply(MessageReceivedEvent event, String message) { @@ -89,4 +131,8 @@ public abstract class Command { public String getArguments() { return arguments; } + + public String getDetailDescription() { + return detailDescription; + } } diff --git a/src/main/java/com/github/nilstrieb/commands/handler/CommandHandler.java b/src/main/java/com/github/nilstrieb/commands/handler/CommandHandler.java index 392648c..77bfb42 100644 --- a/src/main/java/com/github/nilstrieb/commands/handler/CommandHandler.java +++ b/src/main/java/com/github/nilstrieb/commands/handler/CommandHandler.java @@ -90,7 +90,8 @@ public class CommandHandler { .setTitle("Killua help: " + cmd.getName()) .addField("Name", cmd.getName(), true) .addField("Description", cmd.getDescription(), true) - .addField("Example usage", "`" + cmd.getExampleUsage() + "`", true); + .addField("Example usage", "`" + cmd.getExampleUsage() + "`", true) + .addField("Detail:", cmd.getDetailDescription(), true); if (!cmd.getArguments().equals("")) { builder.addField("Arguments", "`" + cmd.getArguments() + "`", true); diff --git a/src/main/java/com/github/nilstrieb/commands/info/EvalCommand.java b/src/main/java/com/github/nilstrieb/commands/info/EvalCommand.java index b43096f..e17fd96 100644 --- a/src/main/java/com/github/nilstrieb/commands/info/EvalCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/info/EvalCommand.java @@ -5,7 +5,7 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent; public class EvalCommand extends Command { public EvalCommand() { - super("eval", "no", "", "", true); + super("eval", true); } @Override diff --git a/src/main/java/com/github/nilstrieb/core/Main.java b/src/main/java/com/github/nilstrieb/core/Main.java index 27d12e5..bc4c94b 100644 --- a/src/main/java/com/github/nilstrieb/core/Main.java +++ b/src/main/java/com/github/nilstrieb/core/Main.java @@ -4,10 +4,12 @@ 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.info.InviteCommand; import com.github.nilstrieb.commands.info.EvalCommand; import com.github.nilstrieb.commands.info.HelpCommand; import com.github.nilstrieb.commands.info.ToukaCommand; +import com.github.nilstrieb.listener.ChannelMessageListener; import com.github.nilstrieb.listener.CommandListener; import com.github.nilstrieb.listener.ReactionEventListener; import com.github.nilstrieb.listener.StartUpListener; @@ -25,7 +27,7 @@ public class Main { builder.setCompression(Compression.ZLIB); builder.setActivity(Activity.watching("over Gon")); - builder.addEventListeners(new StartUpListener(), new CommandListener(), new ReactionEventListener()); + builder.addEventListeners(new StartUpListener(), new ChannelMessageListener(), new CommandListener(), new ReactionEventListener()); JDA jda = builder.build(); setupCommands(); @@ -39,5 +41,6 @@ public class Main { new InviteCommand(); new QuoteCommand(); new DepartureCommand(); + new TriviaCommand(); } } diff --git a/src/main/java/com/github/nilstrieb/listener/ChannelMessageListener.java b/src/main/java/com/github/nilstrieb/listener/ChannelMessageListener.java new file mode 100644 index 0000000..f689458 --- /dev/null +++ b/src/main/java/com/github/nilstrieb/listener/ChannelMessageListener.java @@ -0,0 +1,15 @@ +package com.github.nilstrieb.listener; + +import com.github.nilstrieb.sections.ChannelMessageEventManager; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; + +public class ChannelMessageListener extends ListenerAdapter { + @Override + public void onMessageReceived(@NotNull MessageReceivedEvent event) { + if (!event.getAuthor().isBot()) { + ChannelMessageEventManager.onMessageReceived(event); + } + } +} diff --git a/src/main/java/com/github/nilstrieb/listener/CommandListener.java b/src/main/java/com/github/nilstrieb/listener/CommandListener.java index 2010e71..0f0ccb8 100644 --- a/src/main/java/com/github/nilstrieb/listener/CommandListener.java +++ b/src/main/java/com/github/nilstrieb/listener/CommandListener.java @@ -9,8 +9,8 @@ import org.jetbrains.annotations.NotNull; public class CommandListener extends ListenerAdapter { @Override public void onMessageReceived(@NotNull MessageReceivedEvent event) { - System.out.println(ConsoleColors.CYAN + "[CListener] Received message: '" + event.getMessage().getContentRaw() + "'" + ConsoleColors.RESET); if (!event.getAuthor().isBot()) { + System.out.println(ConsoleColors.CYAN + "[CListener] Received message: '" + event.getMessage().getContentRaw() + "'" + ConsoleColors.RESET); CommandHandler.call(event); } } diff --git a/src/main/java/com/github/nilstrieb/listener/StartUpListener.java b/src/main/java/com/github/nilstrieb/listener/StartUpListener.java index 873bd37..fbb8937 100644 --- a/src/main/java/com/github/nilstrieb/listener/StartUpListener.java +++ b/src/main/java/com/github/nilstrieb/listener/StartUpListener.java @@ -1,5 +1,6 @@ package com.github.nilstrieb.listener; +import com.github.nilstrieb.util.ConsoleColors; import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; @@ -8,6 +9,6 @@ public class StartUpListener extends ListenerAdapter { @Override public void onReady(@NotNull ReadyEvent event) { - System.out.println("[Startup] Killua started"); + System.out.println(ConsoleColors.BLACK_BACKGROUND + ConsoleColors.YELLOW_BOLD_BRIGHT + "[Startup] Killua started" + ConsoleColors.RESET); } } diff --git a/src/main/java/com/github/nilstrieb/sections/ChannelListener.java b/src/main/java/com/github/nilstrieb/sections/ChannelListener.java new file mode 100644 index 0000000..fbf3b30 --- /dev/null +++ b/src/main/java/com/github/nilstrieb/sections/ChannelListener.java @@ -0,0 +1,9 @@ +package com.github.nilstrieb.sections; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public interface ChannelListener { + void messageReceived(MessageReceivedEvent event); + long getUserID(); + long getChannelID(); +} diff --git a/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java b/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java new file mode 100644 index 0000000..235b859 --- /dev/null +++ b/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java @@ -0,0 +1,55 @@ +package com.github.nilstrieb.sections; + + +import com.github.nilstrieb.util.ConsoleColors; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +import java.util.*; + +public class ChannelMessageEventManager { + private static HashMap> listeners = new HashMap<>(); + private static List removeBuffer = new ArrayList<>(); + private static Set removedChannels = new HashSet<>(); + + public static void addListener(ChannelListener listener, long channel) { + if (!listeners.containsKey(channel)) { + listeners.put(channel, new ArrayList<>()); + } + listeners.get(channel).add(listener); + } + + public static void removeListener(ChannelListener listener) { + if (listeners.containsKey(listener.getChannelID())) { + removeBuffer.add(listener); + } + } + + public static void onMessageReceived(MessageReceivedEvent event) { + long id = event.getTextChannel().getIdLong(); + if (listeners.containsKey(id)) { + System.out.println(ConsoleColors.YELLOW + "[ChannelMsgEvtMgr] Message in listened channel " + + event.getTextChannel().getName() + " by " + event.getAuthor().getAsTag() + ": " + event.getMessage().getContentRaw() + ConsoleColors.RESET); + List list = listeners.get(id); + for (ChannelListener channelListener : list) { + if (channelListener.getUserID() == 0) { + channelListener.messageReceived(event); + } else if (channelListener.getUserID() == event.getAuthor().getIdLong()) { + channelListener.messageReceived(event); + } + } + + for (ChannelListener channelListener : removeBuffer) { + listeners.get(channelListener.getChannelID()).remove(channelListener); + removedChannels.add(channelListener.getChannelID()); + } + for (Long removedChannel : removedChannels) { + list = listeners.get(removedChannel); + if (list.isEmpty()) { + listeners.remove(removedChannel); + } + } + removedChannels.clear(); + removeBuffer.clear(); + } + } +} diff --git a/src/main/java/com/github/nilstrieb/sections/Section.java b/src/main/java/com/github/nilstrieb/sections/Section.java new file mode 100644 index 0000000..3287c55 --- /dev/null +++ b/src/main/java/com/github/nilstrieb/sections/Section.java @@ -0,0 +1,34 @@ +package com.github.nilstrieb.sections; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +import java.util.HashMap; + +public abstract class Section implements ChannelListener{ + private long textChannelID; + private long userID; + private boolean multiUser; + + public Section(long textChannelID, long userID) { + this.textChannelID = textChannelID; + this.userID = userID; + this.multiUser = false; + + ChannelMessageEventManager.addListener(this, textChannelID); + } + + public Section(long textChannelID) { + this.textChannelID = textChannelID; + this.multiUser = true; + } + + @Override + public long getUserID() { + return userID; + } + + @Override + public long getChannelID() { + return textChannelID; + } +} diff --git a/src/main/java/com/github/nilstrieb/util/trivia/Arc.java b/src/main/java/com/github/nilstrieb/util/trivia/Arc.java new file mode 100644 index 0000000..a89ff8d --- /dev/null +++ b/src/main/java/com/github/nilstrieb/util/trivia/Arc.java @@ -0,0 +1,15 @@ +package com.github.nilstrieb.util.trivia; + +public enum Arc { + EXAM(0), ZOLDYCK_FAMILY(1), HEAVENS_ARENA(2), YORKNEW_CITY(3), + GREED_ISLAND(4), CHIMERA_ANT(5), ELECTION(6); + + private final int number; + Arc(int number) { + this.number = number; + } + + public int getNumber() { + return number; + } +} diff --git a/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestion.java b/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestion.java new file mode 100644 index 0000000..7687f79 --- /dev/null +++ b/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestion.java @@ -0,0 +1,31 @@ +package com.github.nilstrieb.util.trivia; + +public class TriviaQuestion { + private String question; + private String[] answers; + private int correctAnswer; + private Arc arc; + + public TriviaQuestion(String question, int correctAnswer, Arc arc, String ... answers) { + this.question = question; + this.answers = answers; + this.arc = arc; + this.correctAnswer = correctAnswer; + } + + public String getQuestion() { + return question; + } + + public String[] getAnswers() { + return answers; + } + + public int getCorrectAnswer() { + return correctAnswer; + } + + public Arc getArc() { + return arc; + } +} diff --git a/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestionData.java b/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestionData.java new file mode 100644 index 0000000..b8d783b --- /dev/null +++ b/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestionData.java @@ -0,0 +1,64 @@ +package com.github.nilstrieb.util.trivia; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class TriviaQuestionData { + static List> questions = new ArrayList<>(); + private static final Random random = new Random(); + + static { + questions.add(new ArrayList<>()); + questions.add(new ArrayList<>()); + questions.add(new ArrayList<>()); + questions.add(new ArrayList<>()); + questions.add(new ArrayList<>()); + questions.add(new ArrayList<>()); + questions.add(new ArrayList<>()); + + + add(new TriviaQuestion("What's the name of Gons aunt?", 1, Arc.EXAM, + "Kite", "Mito", "Ging", "Kurapika")); + add(new TriviaQuestion("What is Bisky's true form?", 1, Arc.GREED_ISLAND, + "Boy", "Big Woman", "Little Girl")); + add(new TriviaQuestion("Why does Leorio want money?", 0, Arc.EXAM, + "To become a doctor", "To send it to e-girls", "So that others don't get it", "To buy large quantities of drugs")); + add(new TriviaQuestion("Where did Meruem die?", 2, Arc.CHIMERA_ANT, + "Volcano", "In the desert with Netero", "In the basement of the palace", "In the womb of the queen")); + add(new TriviaQuestion("Who is Alluka?", 3, Arc.ELECTION, + "Gon's sister", "A Zodiac", "The 'dark' side of Nanika", "Killua's sister")); + add(new TriviaQuestion("How did Gon die on Greed Island?", 0, Arc.GREED_ISLAND, + "He didn't 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")); + add(new TriviaQuestion("How many doors are there at the Testing Gate of the Zoldyck Family?", 0, Arc.ZOLDYCK_FAMILY, + "7", "5", "12", "8")); + add(new TriviaQuestion("What was Gon's aim after Killua left him alone playing with the Chairman Netero", 1, Arc.EXAM, + "Get the ball", "Make Netero use his right hand", "Make Netero use his left leg", "Let Netero drop the ball")); + add(new TriviaQuestion("What did Tonpa mix into the drink?", 2, Arc.EXAM, + "Sleeping pills", "Headache tabletsschla", "Laxative", "Rat poison")); + + } + + private static void add(TriviaQuestion triviaQuestion) { + questions.get(triviaQuestion.getArc().getNumber()).add(triviaQuestion); + } + + public static TriviaQuestion getQuestion(int toArc) { + int totalQuestions = 0; + for (int i = 0; i <= toArc; i++) { + totalQuestions += questions.get(i).size(); + } + + int randomQuestion = random.nextInt(totalQuestions); + + for (int i = 0; i <= toArc; i++) { + if (randomQuestion >= questions.get(i).size()) { + randomQuestion -= questions.get(i).size(); + } else { + return questions.get(i).get(randomQuestion); + } + } + + throw new IndexOutOfBoundsException("No Question available for arc " + toArc); + } +}