From 5c60b60c1b75bb5cceb8d355f52120987c30e113 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Sun, 31 Jan 2021 13:07:03 +0100 Subject: [PATCH] refactoring and docs --- .../commands/fun/DepartureCommand.java | 2 +- .../nilstrieb/commands/fun/FightCommand.java | 2 +- .../nilstrieb/commands/fun/QuoteCommand.java | 2 +- .../nilstrieb/commands/fun/SayCommand.java | 3 +- .../commands/fun/trivia/TriviaApproval.java | 4 +- .../commands/fun/trivia/TriviaCommand.java | 4 +- .../commands/handler/MessageSender.java | 65 ----------- .../nilstrieb/commands/info/EvalCommand.java | 2 +- .../nilstrieb/commands/info/HelpCommand.java | 13 +-- .../commands/info/InviteCommand.java | 2 +- .../nilstrieb/commands/info/ToukaCommand.java | 2 +- .../commands/util/EmoteAddCommand.java | 3 +- .../java/com/github/nilstrieb/core/Main.java | 6 +- .../handler => core/command}/Command.java | 7 +- .../command}/CommandHandler.java | 55 ++++++--- .../command}/CommandListener.java | 4 +- .../command}/CommandParser.java | 18 +-- .../nilstrieb/core/command/MessageSender.java | 107 ++++++++++++++++++ .../{ => core}/reactions/ReactionAdapter.java | 24 +++- .../reactions}/ReactionEventListener.java | 5 +- .../reactions/ReactionEventManager.java | 2 +- .../reactions/ReactionListener.java | 2 +- .../{ => core}/sections/ChannelListener.java | 2 +- .../sections/ChannelMessageEventManager.java | 10 +- .../sections}/ChannelMessageListener.java | 3 +- .../{ => core}/sections/Section.java | 29 +++-- .../{ => core}/util/MultiPageEmbed.java | 26 ++++- 27 files changed, 263 insertions(+), 141 deletions(-) delete mode 100644 src/main/java/com/github/nilstrieb/commands/handler/MessageSender.java rename src/main/java/com/github/nilstrieb/{commands/handler => core/command}/Command.java (92%) rename src/main/java/com/github/nilstrieb/{commands/handler => core/command}/CommandHandler.java (67%) rename src/main/java/com/github/nilstrieb/{listener => core/command}/CommandListener.java (86%) rename src/main/java/com/github/nilstrieb/{commands/handler => core/command}/CommandParser.java (62%) create mode 100644 src/main/java/com/github/nilstrieb/core/command/MessageSender.java rename src/main/java/com/github/nilstrieb/{ => core}/reactions/ReactionAdapter.java (55%) rename src/main/java/com/github/nilstrieb/{listener => core/reactions}/ReactionEventListener.java (90%) rename src/main/java/com/github/nilstrieb/{ => core}/reactions/ReactionEventManager.java (97%) rename src/main/java/com/github/nilstrieb/{ => core}/reactions/ReactionListener.java (87%) rename src/main/java/com/github/nilstrieb/{ => core}/sections/ChannelListener.java (82%) rename src/main/java/com/github/nilstrieb/{ => core}/sections/ChannelMessageEventManager.java (88%) rename src/main/java/com/github/nilstrieb/{listener => core/sections}/ChannelMessageListener.java (80%) rename src/main/java/com/github/nilstrieb/{ => core}/sections/Section.java (65%) rename src/main/java/com/github/nilstrieb/{ => core}/util/MultiPageEmbed.java (74%) diff --git a/src/main/java/com/github/nilstrieb/commands/fun/DepartureCommand.java b/src/main/java/com/github/nilstrieb/commands/fun/DepartureCommand.java index 35c7c3d..1bff85c 100644 --- a/src/main/java/com/github/nilstrieb/commands/fun/DepartureCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/fun/DepartureCommand.java @@ -1,7 +1,7 @@ package com.github.nilstrieb.commands.fun; import com.github.nilstrieb.cofig.Config; -import com.github.nilstrieb.commands.handler.Command; +import com.github.nilstrieb.core.command.Command; import com.github.nilstrieb.util.DepartureSong; import net.dv8tion.jda.api.EmbedBuilder; diff --git a/src/main/java/com/github/nilstrieb/commands/fun/FightCommand.java b/src/main/java/com/github/nilstrieb/commands/fun/FightCommand.java index 8f1cf04..04046e6 100644 --- a/src/main/java/com/github/nilstrieb/commands/fun/FightCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/fun/FightCommand.java @@ -1,7 +1,7 @@ package com.github.nilstrieb.commands.fun; import com.github.nilstrieb.cofig.Config; -import com.github.nilstrieb.commands.handler.Command; +import com.github.nilstrieb.core.command.Command; import net.dv8tion.jda.api.EmbedBuilder; public class FightCommand extends Command { diff --git a/src/main/java/com/github/nilstrieb/commands/fun/QuoteCommand.java b/src/main/java/com/github/nilstrieb/commands/fun/QuoteCommand.java index e86775c..8efddef 100644 --- a/src/main/java/com/github/nilstrieb/commands/fun/QuoteCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/fun/QuoteCommand.java @@ -1,7 +1,7 @@ package com.github.nilstrieb.commands.fun; import com.github.nilstrieb.cofig.Config; -import com.github.nilstrieb.commands.handler.Command; +import com.github.nilstrieb.core.command.Command; import com.github.nilstrieb.util.KilluaQuotes; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; diff --git a/src/main/java/com/github/nilstrieb/commands/fun/SayCommand.java b/src/main/java/com/github/nilstrieb/commands/fun/SayCommand.java index 660ce63..5169f64 100644 --- a/src/main/java/com/github/nilstrieb/commands/fun/SayCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/fun/SayCommand.java @@ -1,7 +1,6 @@ package com.github.nilstrieb.commands.fun; -import com.github.nilstrieb.commands.handler.Command; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import com.github.nilstrieb.core.command.Command; public class SayCommand extends Command { public SayCommand(){ diff --git a/src/main/java/com/github/nilstrieb/commands/fun/trivia/TriviaApproval.java b/src/main/java/com/github/nilstrieb/commands/fun/trivia/TriviaApproval.java index 310b186..160d81d 100644 --- a/src/main/java/com/github/nilstrieb/commands/fun/trivia/TriviaApproval.java +++ b/src/main/java/com/github/nilstrieb/commands/fun/trivia/TriviaApproval.java @@ -1,8 +1,8 @@ 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.core.reactions.ReactionAdapter; +import com.github.nilstrieb.core.reactions.ReactionEventManager; import com.github.nilstrieb.util.ConsoleColors; import com.github.nilstrieb.util.trivia.TriviaQuestion; import com.github.nilstrieb.util.trivia.TriviaQuestionData; diff --git a/src/main/java/com/github/nilstrieb/commands/fun/trivia/TriviaCommand.java b/src/main/java/com/github/nilstrieb/commands/fun/trivia/TriviaCommand.java index 311bc4e..a9b9422 100644 --- a/src/main/java/com/github/nilstrieb/commands/fun/trivia/TriviaCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/fun/trivia/TriviaCommand.java @@ -1,8 +1,8 @@ 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.Section; +import com.github.nilstrieb.core.command.Command; +import com.github.nilstrieb.core.sections.Section; import com.github.nilstrieb.util.ConsoleColors; import com.github.nilstrieb.util.trivia.TriviaQuestion; import com.github.nilstrieb.util.trivia.TriviaQuestionData; diff --git a/src/main/java/com/github/nilstrieb/commands/handler/MessageSender.java b/src/main/java/com/github/nilstrieb/commands/handler/MessageSender.java deleted file mode 100644 index b59086c..0000000 --- a/src/main/java/com/github/nilstrieb/commands/handler/MessageSender.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.github.nilstrieb.commands.handler; - -import com.github.nilstrieb.util.MultiPageEmbed; -import net.dv8tion.jda.api.entities.MessageEmbed; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; - -import java.util.Timer; -import java.util.TimerTask; - -public abstract class MessageSender { - - protected MessageReceivedEvent event; - - public void onMessageReceived(MessageReceivedEvent event, String args){ - this.event = event; - called(args); - } - - - /** - * The method called by the CommandHandler - * @param args The arguments (after the command and an optional whitespace) - */ - public abstract void called(String args); - - - protected void reply(String message) { - if (!message.equals("")) { - event.getTextChannel().sendMessage(message).queue(); - } - } - - protected void reply(MessageEmbed embed) { - if (!embed.isEmpty()) { - event.getTextChannel().sendMessage(embed).queue(); - } - } - - protected void reply(MessageEmbed... embeds) { - if (!embeds[0].isEmpty()) { - new MultiPageEmbed(event, embeds); - } - } - - protected void reply(String emote1, String emote2, MessageEmbed... embeds) { - if (!embeds[0].isEmpty()) { - new MultiPageEmbed(event, emote1, emote2, embeds); - } - } - - protected void deleteMsg(long delay) { - new Timer().schedule( - new TimerTask() { - @Override - public void run() { - event.getMessage().delete().queue(); - } - }, delay - ); - } - - protected void deleteMsg() { - event.getMessage().delete().queue(); - } -} 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 7f13131..5c59948 100644 --- a/src/main/java/com/github/nilstrieb/commands/info/EvalCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/info/EvalCommand.java @@ -1,7 +1,7 @@ package com.github.nilstrieb.commands.info; import com.github.nilstrieb.cofig.Config; -import com.github.nilstrieb.commands.handler.Command; +import com.github.nilstrieb.core.command.Command; import com.github.nilstrieb.util.trivia.TriviaQuestionData; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Message; diff --git a/src/main/java/com/github/nilstrieb/commands/info/HelpCommand.java b/src/main/java/com/github/nilstrieb/commands/info/HelpCommand.java index 0ff56fa..d991afc 100644 --- a/src/main/java/com/github/nilstrieb/commands/info/HelpCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/info/HelpCommand.java @@ -1,7 +1,7 @@ package com.github.nilstrieb.commands.info; -import com.github.nilstrieb.commands.handler.Command; -import com.github.nilstrieb.commands.handler.CommandHandler; +import com.github.nilstrieb.core.command.Command; +import com.github.nilstrieb.core.command.CommandHandler; import net.dv8tion.jda.api.entities.MessageEmbed; public class HelpCommand extends Command { @@ -12,14 +12,13 @@ public class HelpCommand extends Command { @Override public void called(String args) { if(args.length() == 0) { - if (CommandHandler.commandAmount() > CommandHandler.MAX_PAGE_LENGTH) { - reply(CommandHandler.getHelpLists(event)); - + if (CommandHandler.commandAmount() > CommandHandler.MAX_HELP_PAGE_LENGTH) { + reply(CommandHandler.getHelpLists()); } else { - reply(CommandHandler.getHelpList(event).build()); + reply(CommandHandler.getHelpList().build()); } } else { - MessageEmbed help = CommandHandler.getCommandHelp(event, args.split(" ")[0]); + MessageEmbed help = CommandHandler.getCommandHelp(args.split(" ")[0]); if (help != null) { reply(help); } diff --git a/src/main/java/com/github/nilstrieb/commands/info/InviteCommand.java b/src/main/java/com/github/nilstrieb/commands/info/InviteCommand.java index 45124e5..55f3ea1 100644 --- a/src/main/java/com/github/nilstrieb/commands/info/InviteCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/info/InviteCommand.java @@ -1,7 +1,7 @@ package com.github.nilstrieb.commands.info; import com.github.nilstrieb.cofig.Config; -import com.github.nilstrieb.commands.handler.Command; +import com.github.nilstrieb.core.command.Command; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.User; diff --git a/src/main/java/com/github/nilstrieb/commands/info/ToukaCommand.java b/src/main/java/com/github/nilstrieb/commands/info/ToukaCommand.java index da33f97..c6a599e 100644 --- a/src/main/java/com/github/nilstrieb/commands/info/ToukaCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/info/ToukaCommand.java @@ -1,7 +1,7 @@ package com.github.nilstrieb.commands.info; import com.github.nilstrieb.cofig.Config; -import com.github.nilstrieb.commands.handler.Command; +import com.github.nilstrieb.core.command.Command; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.User; diff --git a/src/main/java/com/github/nilstrieb/commands/util/EmoteAddCommand.java b/src/main/java/com/github/nilstrieb/commands/util/EmoteAddCommand.java index dcb6d5e..f9bf578 100644 --- a/src/main/java/com/github/nilstrieb/commands/util/EmoteAddCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/util/EmoteAddCommand.java @@ -1,7 +1,6 @@ package com.github.nilstrieb.commands.util; -import com.github.nilstrieb.commands.handler.Command; -import net.dv8tion.jda.api.JDA; +import com.github.nilstrieb.core.command.Command; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Icon; import net.dv8tion.jda.api.entities.Member; diff --git a/src/main/java/com/github/nilstrieb/core/Main.java b/src/main/java/com/github/nilstrieb/core/Main.java index d29ab8f..46c2d45 100644 --- a/src/main/java/com/github/nilstrieb/core/Main.java +++ b/src/main/java/com/github/nilstrieb/core/Main.java @@ -13,9 +13,9 @@ 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.commands.util.EmoteAddCommand; -import com.github.nilstrieb.listener.ChannelMessageListener; -import com.github.nilstrieb.listener.CommandListener; -import com.github.nilstrieb.listener.ReactionEventListener; +import com.github.nilstrieb.core.sections.ChannelMessageListener; +import com.github.nilstrieb.core.command.CommandListener; +import com.github.nilstrieb.core.reactions.ReactionEventListener; import com.github.nilstrieb.listener.StartUpListener; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; diff --git a/src/main/java/com/github/nilstrieb/commands/handler/Command.java b/src/main/java/com/github/nilstrieb/core/command/Command.java similarity index 92% rename from src/main/java/com/github/nilstrieb/commands/handler/Command.java rename to src/main/java/com/github/nilstrieb/core/command/Command.java index ff0dfde..1227c7a 100644 --- a/src/main/java/com/github/nilstrieb/commands/handler/Command.java +++ b/src/main/java/com/github/nilstrieb/core/command/Command.java @@ -1,14 +1,17 @@ -package com.github.nilstrieb.commands.handler; +package com.github.nilstrieb.core.command; import com.github.nilstrieb.cofig.Config; +/** + * The base command class. Every command should extend this class. + */ public abstract class Command extends MessageSender{ private final String name; private final String description; private final String exampleUsage; private final String arguments; private final String detailDescription; - private final CommandParser parser = CommandParser.getInstance(); + private final CommandParser parser = new CommandParser(); /** diff --git a/src/main/java/com/github/nilstrieb/commands/handler/CommandHandler.java b/src/main/java/com/github/nilstrieb/core/command/CommandHandler.java similarity index 67% rename from src/main/java/com/github/nilstrieb/commands/handler/CommandHandler.java rename to src/main/java/com/github/nilstrieb/core/command/CommandHandler.java index ece3766..ef999f5 100644 --- a/src/main/java/com/github/nilstrieb/commands/handler/CommandHandler.java +++ b/src/main/java/com/github/nilstrieb/core/command/CommandHandler.java @@ -1,21 +1,32 @@ -package com.github.nilstrieb.commands.handler; +package com.github.nilstrieb.core.command; import com.github.nilstrieb.cofig.Config; import com.github.nilstrieb.util.ConsoleColors; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; -import net.dv8tion.jda.api.events.Event; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import java.util.*; +/** + * The CommandHandler handles everything about the commands + */ public class CommandHandler { - public static final int MAX_PAGE_LENGTH = 10; + public static final int MAX_HELP_PAGE_LENGTH = 10; + private static final Map commands = new HashMap<>(); private static final Map hiddenCommands = new HashMap<>(); - private static final CommandParser parser = CommandParser.getInstance(); + private static final CommandParser parser = new CommandParser(); + + /** + * Add a new command to the handler. This is normally done by the{@code Command}. + * + * @param key The key (command name) + * @param cmd The command object + * @param hidden Whether the command should be shown on the help page + */ public static void addCommand(String key, Command cmd, boolean hidden) { if (hidden) { hiddenCommands.put(key, cmd); @@ -28,7 +39,12 @@ public class CommandHandler { commands.put(key, cmd); } - public static void call(MessageReceivedEvent event) { + /** + * This method is called by the{@code CommandListener} + * + * @param event The {@code MessageReceivedEvent} + */ + static void call(MessageReceivedEvent event) { if (event.getMessage().getContentRaw().toLowerCase().startsWith(Config.PREFIX)) { String[] split = parser.splitOffCommandName(event.getMessage().getContentRaw()); String command = split[0]; @@ -45,7 +61,12 @@ public class CommandHandler { return commands.size(); } - public static EmbedBuilder getHelpList(Event event) { + /** + * Get the list of all commands, on one page. + * May lead to problems if there are too many commands. + * @return The page + */ + public static EmbedBuilder getHelpList() { EmbedBuilder builder = Config.getDefaultEmbed(); builder.setTitle("Killua help"); for (Command s : commands.values()) { @@ -54,17 +75,21 @@ public class CommandHandler { return builder; } - public static MessageEmbed[] getHelpLists(Event event) { + /** + * Get a list of all commands, spread over different pages + * @return The pages + */ + public static MessageEmbed[] getHelpLists() { int length = commands.size(); - int pages = length / MAX_PAGE_LENGTH + 1; + int pages = length / MAX_HELP_PAGE_LENGTH + 1; EmbedBuilder[] builders = new EmbedBuilder[pages]; int i = 0, j = 0; EmbedBuilder builder = null; for (Command value : commands.values()) { - if (i % MAX_PAGE_LENGTH == 0) { + if (i % MAX_HELP_PAGE_LENGTH == 0) { builder = Config.getDefaultEmbed(); builder.setTitle("Killua help"); builders[j] = builder; @@ -82,15 +107,19 @@ public class CommandHandler { return messageEmbeds; } - public static MessageEmbed getCommandHelp(Event event, String command) { + /** + * Returns the help page for a single command + * @param command The command name + * @return The help page + */ + public static MessageEmbed getCommandHelp(String command) { Command cmd = commands.get(command); if (cmd != null) { EmbedBuilder builder = Config.getDefaultEmbed() .setTitle("Killua help: " + cmd.getName()) .addField("Name", cmd.getName(), true) - .addField("Description", cmd.getDescription(), true) - .addField("Example usage", "`" + cmd.getExampleUsage() + "`", true) - .addField("Detail:", cmd.getDetailDescription(), true); + .addField("Description:", cmd.getDetailDescription(), true) + .addField("Example usage", "`" + cmd.getExampleUsage() + "`", true); if (!cmd.getArguments().equals("")) { builder.addField("Arguments", "`" + cmd.getArguments() + "`", true); diff --git a/src/main/java/com/github/nilstrieb/listener/CommandListener.java b/src/main/java/com/github/nilstrieb/core/command/CommandListener.java similarity index 86% rename from src/main/java/com/github/nilstrieb/listener/CommandListener.java rename to src/main/java/com/github/nilstrieb/core/command/CommandListener.java index 65d354b..e3d2e93 100644 --- a/src/main/java/com/github/nilstrieb/listener/CommandListener.java +++ b/src/main/java/com/github/nilstrieb/core/command/CommandListener.java @@ -1,6 +1,6 @@ -package com.github.nilstrieb.listener; +package com.github.nilstrieb.core.command; -import com.github.nilstrieb.commands.handler.CommandHandler; +import com.github.nilstrieb.core.command.CommandHandler; import com.github.nilstrieb.util.ConsoleColors; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; diff --git a/src/main/java/com/github/nilstrieb/commands/handler/CommandParser.java b/src/main/java/com/github/nilstrieb/core/command/CommandParser.java similarity index 62% rename from src/main/java/com/github/nilstrieb/commands/handler/CommandParser.java rename to src/main/java/com/github/nilstrieb/core/command/CommandParser.java index 268276c..7fbc5e8 100644 --- a/src/main/java/com/github/nilstrieb/commands/handler/CommandParser.java +++ b/src/main/java/com/github/nilstrieb/core/command/CommandParser.java @@ -1,21 +1,21 @@ -package com.github.nilstrieb.commands.handler; +package com.github.nilstrieb.core.command; import com.github.nilstrieb.cofig.Config; +/** + * A parser for parsing commands + */ public class CommandParser { - private static final CommandParser parser = new CommandParser(); - - private CommandParser() {} - - public static CommandParser getInstance() { - return parser; - } - public static String[] splitArgs(String args){ return args.split(" "); } + /** + * Split the full message into command name + args + * @param contentRaw The full message (including prefix!) + * @return a String array where [0] is the command name and [1] are the args (the text after the name + an optional space) + */ public String[] splitOffCommandName(String contentRaw) { String[] returns = new String[2]; String beheaded = contentRaw.substring(Config.PREFIX.length()); diff --git a/src/main/java/com/github/nilstrieb/core/command/MessageSender.java b/src/main/java/com/github/nilstrieb/core/command/MessageSender.java new file mode 100644 index 0000000..36572c0 --- /dev/null +++ b/src/main/java/com/github/nilstrieb/core/command/MessageSender.java @@ -0,0 +1,107 @@ +package com.github.nilstrieb.core.command; + +import com.github.nilstrieb.core.util.MultiPageEmbed; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.TimeUnit; + +/** + * An abstract class for classes that interact with the chat. + * Removes all the boilerplate code associated with sending messages over JDA + */ +public abstract class MessageSender { + + protected MessageReceivedEvent event; + + /** + * Called by the specific handler. + * + * @param event The MessageReceivedEvent + * @param args The arguments + */ + public void onMessageReceived(MessageReceivedEvent event, String args) { + this.event = event; + called(args); + } + + + /** + * This method gets called by this object after the MessageReceivedEvent has been saved. + * + * @param args The arguments/text of the sent message + */ + public abstract void called(String args); + + /** + * Send a simple String message to the chat the original message came from. + * Equivalent to {@code event.getTextChannel().sendMessage(message).queue();} + * Will not send empty Strings. + * + * @param message The message + */ + protected void reply(String message) { + if (!message.isEmpty()) { + event.getTextChannel().sendMessage(message).queue(); + } + } + + /** + * Send a simple embed message to the chat original message came from. + * Equivalent to {@code event.getTextChannel().sendMessage(embed).queue();} + * Will not send empty Embeds + * + * @param embed The embed + */ + protected void reply(MessageEmbed embed) { + if (!embed.isEmpty()) { + event.getTextChannel().sendMessage(embed).queue(); + } + } + + /** + * Send multiple embeds in one message message as a {@code MultiPageEmbed} to the chat original message came from. + * Equivalent to {@code new MultiPageEmbed(event, embeds);} + * Will not send empty Embeds + * + * @param embeds The embeds + */ + protected void reply(MessageEmbed... embeds) { + if (!embeds[0].isEmpty()) { + new MultiPageEmbed(event, embeds); + } + } + + /** + * Send multiple embeds in one message message as a {@code MultiPageEmbed} to the chat original message came from. + * Equivalent to {@code new MultiPageEmbed(event, embeds);} + * Will not send empty Embeds + * + * @param embeds The embeds + * @param emote1 The emote for scrolling to the left + * @param emote2 The emote for scrolling to the right + */ + protected void reply(String emote1, String emote2, MessageEmbed... embeds) { + if (!embeds[0].isEmpty()) { + new MultiPageEmbed(event, emote1, emote2, embeds); + } + } + + /** + * Delete the original message after a specific delay in s. + * + * @param delay The delay in s + */ + protected void deleteMsg(long delay) { + event.getMessage().delete().queueAfter(delay, TimeUnit.SECONDS); + } + + /** + * Delete the original message after a specific delay in ms. + */ + protected void deleteMsg() { + event.getMessage().delete().queue(); + } +} diff --git a/src/main/java/com/github/nilstrieb/reactions/ReactionAdapter.java b/src/main/java/com/github/nilstrieb/core/reactions/ReactionAdapter.java similarity index 55% rename from src/main/java/com/github/nilstrieb/reactions/ReactionAdapter.java rename to src/main/java/com/github/nilstrieb/core/reactions/ReactionAdapter.java index 516212f..33bf4b0 100644 --- a/src/main/java/com/github/nilstrieb/reactions/ReactionAdapter.java +++ b/src/main/java/com/github/nilstrieb/core/reactions/ReactionAdapter.java @@ -1,25 +1,43 @@ -package com.github.nilstrieb.reactions; +package com.github.nilstrieb.core.reactions; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; +/** + * The adapter for the ReactionListener + */ public abstract class ReactionAdapter implements ReactionListener { private long message; + /** + * This method has to be called with the message ID + * @param message The message ID + */ protected void create(long message) { this.message = message; ReactionEventManager.addMessage(message, this); } + /** + * This method can be called to remove the Listener class from the Handlerr + */ protected void dispose() { ReactionEventManager.removeMessage(message); } + /** + * This method gets called from the Handler after a reaction is added to this message + * @param event The event + */ @Override public void onReactionAdded(MessageReactionAddEvent event) { } + /** + * This method gets called from the Handler after a reaction is removed to this message + * @param event The event + */ @Override public void onReactionRemoved(MessageReactionRemoveEvent event) { } @@ -27,8 +45,4 @@ public abstract class ReactionAdapter implements ReactionListener { public long getMessage() { return message; } - - public void setMessage(long message) { - this.message = message; - } } diff --git a/src/main/java/com/github/nilstrieb/listener/ReactionEventListener.java b/src/main/java/com/github/nilstrieb/core/reactions/ReactionEventListener.java similarity index 90% rename from src/main/java/com/github/nilstrieb/listener/ReactionEventListener.java rename to src/main/java/com/github/nilstrieb/core/reactions/ReactionEventListener.java index e8cbfd8..a6769fa 100644 --- a/src/main/java/com/github/nilstrieb/listener/ReactionEventListener.java +++ b/src/main/java/com/github/nilstrieb/core/reactions/ReactionEventListener.java @@ -1,13 +1,10 @@ -package com.github.nilstrieb.listener; +package com.github.nilstrieb.core.reactions; -import com.github.nilstrieb.reactions.ReactionEventManager; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; -import java.util.Objects; - public class ReactionEventListener extends ListenerAdapter { @Override public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { diff --git a/src/main/java/com/github/nilstrieb/reactions/ReactionEventManager.java b/src/main/java/com/github/nilstrieb/core/reactions/ReactionEventManager.java similarity index 97% rename from src/main/java/com/github/nilstrieb/reactions/ReactionEventManager.java rename to src/main/java/com/github/nilstrieb/core/reactions/ReactionEventManager.java index 2ef4e77..1bf1130 100644 --- a/src/main/java/com/github/nilstrieb/reactions/ReactionEventManager.java +++ b/src/main/java/com/github/nilstrieb/core/reactions/ReactionEventManager.java @@ -1,4 +1,4 @@ -package com.github.nilstrieb.reactions; +package com.github.nilstrieb.core.reactions; import com.github.nilstrieb.util.ConsoleColors; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; diff --git a/src/main/java/com/github/nilstrieb/reactions/ReactionListener.java b/src/main/java/com/github/nilstrieb/core/reactions/ReactionListener.java similarity index 87% rename from src/main/java/com/github/nilstrieb/reactions/ReactionListener.java rename to src/main/java/com/github/nilstrieb/core/reactions/ReactionListener.java index 7917dc1..2e8e214 100644 --- a/src/main/java/com/github/nilstrieb/reactions/ReactionListener.java +++ b/src/main/java/com/github/nilstrieb/core/reactions/ReactionListener.java @@ -1,4 +1,4 @@ -package com.github.nilstrieb.reactions; +package com.github.nilstrieb.core.reactions; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; diff --git a/src/main/java/com/github/nilstrieb/sections/ChannelListener.java b/src/main/java/com/github/nilstrieb/core/sections/ChannelListener.java similarity index 82% rename from src/main/java/com/github/nilstrieb/sections/ChannelListener.java rename to src/main/java/com/github/nilstrieb/core/sections/ChannelListener.java index fbf3b30..ab748d9 100644 --- a/src/main/java/com/github/nilstrieb/sections/ChannelListener.java +++ b/src/main/java/com/github/nilstrieb/core/sections/ChannelListener.java @@ -1,4 +1,4 @@ -package com.github.nilstrieb.sections; +package com.github.nilstrieb.core.sections; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; diff --git a/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java b/src/main/java/com/github/nilstrieb/core/sections/ChannelMessageEventManager.java similarity index 88% rename from src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java rename to src/main/java/com/github/nilstrieb/core/sections/ChannelMessageEventManager.java index 5b834d6..9ea9011 100644 --- a/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java +++ b/src/main/java/com/github/nilstrieb/core/sections/ChannelMessageEventManager.java @@ -1,4 +1,4 @@ -package com.github.nilstrieb.sections; +package com.github.nilstrieb.core.sections; import com.github.nilstrieb.util.ConsoleColors; @@ -26,9 +26,12 @@ public class ChannelMessageEventManager { public static void onMessageReceived(MessageReceivedEvent event) { long id = event.getTextChannel().getIdLong(); + //if the message is relevant if (listeners.containsKey(id)) { System.out.println(ConsoleColors.YELLOW + "[ChannelMsgEvtMgr 30] Message in listened channel " + event.getTextChannel().getName() + " by " + event.getAuthor().getAsTag() + ": " + event.getMessage().getContentRaw() + ConsoleColors.RESET); + + //notify all listeners List list = listeners.get(id); for (ChannelListener channelListener : list) { if (channelListener.getUserID() == 0) { @@ -38,16 +41,21 @@ public class ChannelMessageEventManager { } } + //remove the listeners that got removed during the calling for (ChannelListener channelListener : removeBuffer) { listeners.get(channelListener.getChannelID()).remove(channelListener); removedChannels.add(channelListener.getChannelID()); } + + //remove the channels if all listeners for that channel have been removed for (Long removedChannel : removedChannels) { list = listeners.get(removedChannel); if (list.isEmpty()) { listeners.remove(removedChannel); } } + + //clear the buffers removedChannels.clear(); removeBuffer.clear(); } diff --git a/src/main/java/com/github/nilstrieb/listener/ChannelMessageListener.java b/src/main/java/com/github/nilstrieb/core/sections/ChannelMessageListener.java similarity index 80% rename from src/main/java/com/github/nilstrieb/listener/ChannelMessageListener.java rename to src/main/java/com/github/nilstrieb/core/sections/ChannelMessageListener.java index f689458..229aa35 100644 --- a/src/main/java/com/github/nilstrieb/listener/ChannelMessageListener.java +++ b/src/main/java/com/github/nilstrieb/core/sections/ChannelMessageListener.java @@ -1,6 +1,5 @@ -package com.github.nilstrieb.listener; +package com.github.nilstrieb.core.sections; -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; diff --git a/src/main/java/com/github/nilstrieb/sections/Section.java b/src/main/java/com/github/nilstrieb/core/sections/Section.java similarity index 65% rename from src/main/java/com/github/nilstrieb/sections/Section.java rename to src/main/java/com/github/nilstrieb/core/sections/Section.java index f5dcf19..b86d16d 100644 --- a/src/main/java/com/github/nilstrieb/sections/Section.java +++ b/src/main/java/com/github/nilstrieb/core/sections/Section.java @@ -1,12 +1,20 @@ -package com.github.nilstrieb.sections; +package com.github.nilstrieb.core.sections; -import com.github.nilstrieb.commands.handler.MessageSender; +import com.github.nilstrieb.core.command.MessageSender; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +/** + * The section class can be extended to create sections where the user is asked to write multiple messages + */ public abstract class Section extends MessageSender implements ChannelListener{ private final long textChannelID; private final long userID; + /** + * Create a new section for a specific user + * @param textChannelID The channel ID + * @param userID The user ID + */ public Section(long textChannelID, long userID) { this.textChannelID = textChannelID; this.userID = userID; @@ -14,17 +22,24 @@ public abstract class Section extends MessageSender implements ChannelListener{ ChannelMessageEventManager.addListener(this, textChannelID); } + /** + * Create a new section for all users in a channel + * @param textChannelID The channel ID + */ + public Section(long textChannelID) { + this.textChannelID = textChannelID; + this.userID = 0; + } + @Override public void messageReceived(MessageReceivedEvent event) { this.event = event; called(event.getMessage().getContentRaw()); } - public Section(long textChannelID) { - this.textChannelID = textChannelID; - this.userID = 0; - } - + /** + * End the section. + */ protected void dispose(){ ChannelMessageEventManager.removeListener(this); } diff --git a/src/main/java/com/github/nilstrieb/util/MultiPageEmbed.java b/src/main/java/com/github/nilstrieb/core/util/MultiPageEmbed.java similarity index 74% rename from src/main/java/com/github/nilstrieb/util/MultiPageEmbed.java rename to src/main/java/com/github/nilstrieb/core/util/MultiPageEmbed.java index 23c2746..c477ade 100644 --- a/src/main/java/com/github/nilstrieb/util/MultiPageEmbed.java +++ b/src/main/java/com/github/nilstrieb/core/util/MultiPageEmbed.java @@ -1,6 +1,6 @@ -package com.github.nilstrieb.util; +package com.github.nilstrieb.core.util; -import com.github.nilstrieb.reactions.ReactionAdapter; +import com.github.nilstrieb.core.reactions.ReactionAdapter; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; @@ -8,6 +8,9 @@ import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import java.util.Objects; +/** + * This class sends a message to the channel in the event that contains multiple pages that can be navigated using reactinos + */ 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"; @@ -19,10 +22,22 @@ public class MultiPageEmbed extends ReactionAdapter { private final String nextReaction; + /** + * Create a new MultiPageEmbed with the default emotes + * + * @param event The event + * @param pages The pages + */ public MultiPageEmbed(MessageReceivedEvent event, MessageEmbed... pages) { this(event, PREVIOUS_PAGE_DEFAULT_REACTION, NEXT_PAGE_DEFAULT_REACTION, pages); } + /** + * Create a new MultiPageEmbed with custom emotes + * + * @param event The event + * @param pages The pages + */ public MultiPageEmbed(MessageReceivedEvent event, String prevReaction, String nextReaction, MessageEmbed[] pages) { this.prevReaction = prevReaction; this.nextReaction = nextReaction; @@ -53,7 +68,10 @@ public class MultiPageEmbed extends ReactionAdapter { } } } - Objects.requireNonNull(event.getUser(), "Reaction user was null"); - event.getReaction().removeReaction(event.getUser()).queue(); + if (event.getUser() != null) { + event.getReaction().removeReaction(event.getUser()).queue(); + } else { + System.err.println("[MultiPageEmbed] Reaction user was null"); + } } }