refactoring and docs

This commit is contained in:
nora 2021-01-31 13:07:03 +01:00
parent eabd310db6
commit 5c60b60c1b
27 changed files with 263 additions and 141 deletions

View file

@ -1,7 +1,7 @@
package com.github.nilstrieb.commands.fun; package com.github.nilstrieb.commands.fun;
import com.github.nilstrieb.cofig.Config; 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 com.github.nilstrieb.util.DepartureSong;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;

View file

@ -1,7 +1,7 @@
package com.github.nilstrieb.commands.fun; package com.github.nilstrieb.commands.fun;
import com.github.nilstrieb.cofig.Config; 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.EmbedBuilder;
public class FightCommand extends Command { public class FightCommand extends Command {

View file

@ -1,7 +1,7 @@
package com.github.nilstrieb.commands.fun; package com.github.nilstrieb.commands.fun;
import com.github.nilstrieb.cofig.Config; 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 com.github.nilstrieb.util.KilluaQuotes;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;

View file

@ -1,7 +1,6 @@
package com.github.nilstrieb.commands.fun; package com.github.nilstrieb.commands.fun;
import com.github.nilstrieb.commands.handler.Command; import com.github.nilstrieb.core.command.Command;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class SayCommand extends Command { public class SayCommand extends Command {
public SayCommand(){ public SayCommand(){

View file

@ -1,8 +1,8 @@
package com.github.nilstrieb.commands.fun.trivia; package com.github.nilstrieb.commands.fun.trivia;
import com.github.nilstrieb.cofig.Config; import com.github.nilstrieb.cofig.Config;
import com.github.nilstrieb.reactions.ReactionAdapter; import com.github.nilstrieb.core.reactions.ReactionAdapter;
import com.github.nilstrieb.reactions.ReactionEventManager; import com.github.nilstrieb.core.reactions.ReactionEventManager;
import com.github.nilstrieb.util.ConsoleColors; import com.github.nilstrieb.util.ConsoleColors;
import com.github.nilstrieb.util.trivia.TriviaQuestion; import com.github.nilstrieb.util.trivia.TriviaQuestion;
import com.github.nilstrieb.util.trivia.TriviaQuestionData; import com.github.nilstrieb.util.trivia.TriviaQuestionData;

View file

@ -1,8 +1,8 @@
package com.github.nilstrieb.commands.fun.trivia; package com.github.nilstrieb.commands.fun.trivia;
import com.github.nilstrieb.cofig.Config; import com.github.nilstrieb.cofig.Config;
import com.github.nilstrieb.commands.handler.Command; import com.github.nilstrieb.core.command.Command;
import com.github.nilstrieb.sections.Section; import com.github.nilstrieb.core.sections.Section;
import com.github.nilstrieb.util.ConsoleColors; import com.github.nilstrieb.util.ConsoleColors;
import com.github.nilstrieb.util.trivia.TriviaQuestion; import com.github.nilstrieb.util.trivia.TriviaQuestion;
import com.github.nilstrieb.util.trivia.TriviaQuestionData; import com.github.nilstrieb.util.trivia.TriviaQuestionData;

View file

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

View file

@ -1,7 +1,7 @@
package com.github.nilstrieb.commands.info; package com.github.nilstrieb.commands.info;
import com.github.nilstrieb.cofig.Config; 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 com.github.nilstrieb.util.trivia.TriviaQuestionData;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;

View file

@ -1,7 +1,7 @@
package com.github.nilstrieb.commands.info; package com.github.nilstrieb.commands.info;
import com.github.nilstrieb.commands.handler.Command; import com.github.nilstrieb.core.command.Command;
import com.github.nilstrieb.commands.handler.CommandHandler; import com.github.nilstrieb.core.command.CommandHandler;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
public class HelpCommand extends Command { public class HelpCommand extends Command {
@ -12,14 +12,13 @@ public class HelpCommand extends Command {
@Override @Override
public void called(String args) { public void called(String args) {
if(args.length() == 0) { if(args.length() == 0) {
if (CommandHandler.commandAmount() > CommandHandler.MAX_PAGE_LENGTH) { if (CommandHandler.commandAmount() > CommandHandler.MAX_HELP_PAGE_LENGTH) {
reply(CommandHandler.getHelpLists(event)); reply(CommandHandler.getHelpLists());
} else { } else {
reply(CommandHandler.getHelpList(event).build()); reply(CommandHandler.getHelpList().build());
} }
} else { } else {
MessageEmbed help = CommandHandler.getCommandHelp(event, args.split(" ")[0]); MessageEmbed help = CommandHandler.getCommandHelp(args.split(" ")[0]);
if (help != null) { if (help != null) {
reply(help); reply(help);
} }

View file

@ -1,7 +1,7 @@
package com.github.nilstrieb.commands.info; package com.github.nilstrieb.commands.info;
import com.github.nilstrieb.cofig.Config; 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.EmbedBuilder;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;

View file

@ -1,7 +1,7 @@
package com.github.nilstrieb.commands.info; package com.github.nilstrieb.commands.info;
import com.github.nilstrieb.cofig.Config; 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.EmbedBuilder;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;

View file

@ -1,7 +1,6 @@
package com.github.nilstrieb.commands.util; package com.github.nilstrieb.commands.util;
import com.github.nilstrieb.commands.handler.Command; import com.github.nilstrieb.core.command.Command;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Icon; import net.dv8tion.jda.api.entities.Icon;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;

View file

@ -13,9 +13,9 @@ import com.github.nilstrieb.commands.info.EvalCommand;
import com.github.nilstrieb.commands.info.HelpCommand; import com.github.nilstrieb.commands.info.HelpCommand;
import com.github.nilstrieb.commands.info.ToukaCommand; import com.github.nilstrieb.commands.info.ToukaCommand;
import com.github.nilstrieb.commands.util.EmoteAddCommand; import com.github.nilstrieb.commands.util.EmoteAddCommand;
import com.github.nilstrieb.listener.ChannelMessageListener; import com.github.nilstrieb.core.sections.ChannelMessageListener;
import com.github.nilstrieb.listener.CommandListener; import com.github.nilstrieb.core.command.CommandListener;
import com.github.nilstrieb.listener.ReactionEventListener; import com.github.nilstrieb.core.reactions.ReactionEventListener;
import com.github.nilstrieb.listener.StartUpListener; import com.github.nilstrieb.listener.StartUpListener;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;

View file

@ -1,14 +1,17 @@
package com.github.nilstrieb.commands.handler; package com.github.nilstrieb.core.command;
import com.github.nilstrieb.cofig.Config; import com.github.nilstrieb.cofig.Config;
/**
* The base command class. Every command should extend this class.
*/
public abstract class Command extends MessageSender{ public abstract class Command extends MessageSender{
private final String name; private final String name;
private final String description; private final String description;
private final String exampleUsage; private final String exampleUsage;
private final String arguments; private final String arguments;
private final String detailDescription; private final String detailDescription;
private final CommandParser parser = CommandParser.getInstance(); private final CommandParser parser = new CommandParser();
/** /**

View file

@ -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.cofig.Config;
import com.github.nilstrieb.util.ConsoleColors; import com.github.nilstrieb.util.ConsoleColors;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.*; import java.util.*;
/**
* The CommandHandler handles everything about the commands
*/
public class CommandHandler { public class CommandHandler {
public static final int MAX_PAGE_LENGTH = 10; public static final int MAX_HELP_PAGE_LENGTH = 10;
private static final Map<String, Command> commands = new HashMap<>(); private static final Map<String, Command> commands = new HashMap<>();
private static final Map<String, Command> hiddenCommands = new HashMap<>(); private static final Map<String, Command> 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) { public static void addCommand(String key, Command cmd, boolean hidden) {
if (hidden) { if (hidden) {
hiddenCommands.put(key, cmd); hiddenCommands.put(key, cmd);
@ -28,7 +39,12 @@ public class CommandHandler {
commands.put(key, cmd); 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)) { if (event.getMessage().getContentRaw().toLowerCase().startsWith(Config.PREFIX)) {
String[] split = parser.splitOffCommandName(event.getMessage().getContentRaw()); String[] split = parser.splitOffCommandName(event.getMessage().getContentRaw());
String command = split[0]; String command = split[0];
@ -45,7 +61,12 @@ public class CommandHandler {
return commands.size(); 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(); EmbedBuilder builder = Config.getDefaultEmbed();
builder.setTitle("Killua help"); builder.setTitle("Killua help");
for (Command s : commands.values()) { for (Command s : commands.values()) {
@ -54,17 +75,21 @@ public class CommandHandler {
return builder; 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 length = commands.size();
int pages = length / MAX_PAGE_LENGTH + 1; int pages = length / MAX_HELP_PAGE_LENGTH + 1;
EmbedBuilder[] builders = new EmbedBuilder[pages]; EmbedBuilder[] builders = new EmbedBuilder[pages];
int i = 0, j = 0; int i = 0, j = 0;
EmbedBuilder builder = null; EmbedBuilder builder = null;
for (Command value : commands.values()) { for (Command value : commands.values()) {
if (i % MAX_PAGE_LENGTH == 0) { if (i % MAX_HELP_PAGE_LENGTH == 0) {
builder = Config.getDefaultEmbed(); builder = Config.getDefaultEmbed();
builder.setTitle("Killua help"); builder.setTitle("Killua help");
builders[j] = builder; builders[j] = builder;
@ -82,15 +107,19 @@ public class CommandHandler {
return messageEmbeds; 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); Command cmd = commands.get(command);
if (cmd != null) { if (cmd != null) {
EmbedBuilder builder = Config.getDefaultEmbed() EmbedBuilder builder = Config.getDefaultEmbed()
.setTitle("Killua help: " + cmd.getName()) .setTitle("Killua help: " + cmd.getName())
.addField("Name", cmd.getName(), true) .addField("Name", cmd.getName(), true)
.addField("Description", cmd.getDescription(), true) .addField("Description:", cmd.getDetailDescription(), true)
.addField("Example usage", "`" + cmd.getExampleUsage() + "`", true) .addField("Example usage", "`" + cmd.getExampleUsage() + "`", true);
.addField("Detail:", cmd.getDetailDescription(), true);
if (!cmd.getArguments().equals("")) { if (!cmd.getArguments().equals("")) {
builder.addField("Arguments", "`" + cmd.getArguments() + "`", true); builder.addField("Arguments", "`" + cmd.getArguments() + "`", true);

View file

@ -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 com.github.nilstrieb.util.ConsoleColors;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;

View file

@ -1,21 +1,21 @@
package com.github.nilstrieb.commands.handler; package com.github.nilstrieb.core.command;
import com.github.nilstrieb.cofig.Config; import com.github.nilstrieb.cofig.Config;
/**
* A parser for parsing commands
*/
public class CommandParser { public class CommandParser {
private static final CommandParser parser = new CommandParser();
private CommandParser() {}
public static CommandParser getInstance() {
return parser;
}
public static String[] splitArgs(String args){ public static String[] splitArgs(String args){
return args.split(" "); 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) { public String[] splitOffCommandName(String contentRaw) {
String[] returns = new String[2]; String[] returns = new String[2];
String beheaded = contentRaw.substring(Config.PREFIX.length()); String beheaded = contentRaw.substring(Config.PREFIX.length());

View file

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

View file

@ -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.MessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;
/**
* The adapter for the ReactionListener
*/
public abstract class ReactionAdapter implements ReactionListener { public abstract class ReactionAdapter implements ReactionListener {
private long message; private long message;
/**
* This method has to be called with the message ID
* @param message The message ID
*/
protected void create(long message) { protected void create(long message) {
this.message = message; this.message = message;
ReactionEventManager.addMessage(message, this); ReactionEventManager.addMessage(message, this);
} }
/**
* This method can be called to remove the Listener class from the Handlerr
*/
protected void dispose() { protected void dispose() {
ReactionEventManager.removeMessage(message); ReactionEventManager.removeMessage(message);
} }
/**
* This method gets called from the Handler after a reaction is added to this message
* @param event The event
*/
@Override @Override
public void onReactionAdded(MessageReactionAddEvent event) { 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 @Override
public void onReactionRemoved(MessageReactionRemoveEvent event) { public void onReactionRemoved(MessageReactionRemoveEvent event) {
} }
@ -27,8 +45,4 @@ public abstract class ReactionAdapter implements ReactionListener {
public long getMessage() { public long getMessage() {
return message; return message;
} }
public void setMessage(long message) {
this.message = message;
}
} }

View file

@ -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.MessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class ReactionEventListener extends ListenerAdapter { public class ReactionEventListener extends ListenerAdapter {
@Override @Override
public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) {

View file

@ -1,4 +1,4 @@
package com.github.nilstrieb.reactions; package com.github.nilstrieb.core.reactions;
import com.github.nilstrieb.util.ConsoleColors; import com.github.nilstrieb.util.ConsoleColors;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;

View file

@ -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.MessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;

View file

@ -1,4 +1,4 @@
package com.github.nilstrieb.sections; package com.github.nilstrieb.core.sections;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

View file

@ -1,4 +1,4 @@
package com.github.nilstrieb.sections; package com.github.nilstrieb.core.sections;
import com.github.nilstrieb.util.ConsoleColors; import com.github.nilstrieb.util.ConsoleColors;
@ -26,9 +26,12 @@ public class ChannelMessageEventManager {
public static void onMessageReceived(MessageReceivedEvent event) { public static void onMessageReceived(MessageReceivedEvent event) {
long id = event.getTextChannel().getIdLong(); long id = event.getTextChannel().getIdLong();
//if the message is relevant
if (listeners.containsKey(id)) { if (listeners.containsKey(id)) {
System.out.println(ConsoleColors.YELLOW + "[ChannelMsgEvtMgr 30] Message in listened channel " + System.out.println(ConsoleColors.YELLOW + "[ChannelMsgEvtMgr 30] Message in listened channel " +
event.getTextChannel().getName() + " by " + event.getAuthor().getAsTag() + ": " + event.getMessage().getContentRaw() + ConsoleColors.RESET); event.getTextChannel().getName() + " by " + event.getAuthor().getAsTag() + ": " + event.getMessage().getContentRaw() + ConsoleColors.RESET);
//notify all listeners
List<ChannelListener> list = listeners.get(id); List<ChannelListener> list = listeners.get(id);
for (ChannelListener channelListener : list) { for (ChannelListener channelListener : list) {
if (channelListener.getUserID() == 0) { if (channelListener.getUserID() == 0) {
@ -38,16 +41,21 @@ public class ChannelMessageEventManager {
} }
} }
//remove the listeners that got removed during the calling
for (ChannelListener channelListener : removeBuffer) { for (ChannelListener channelListener : removeBuffer) {
listeners.get(channelListener.getChannelID()).remove(channelListener); listeners.get(channelListener.getChannelID()).remove(channelListener);
removedChannels.add(channelListener.getChannelID()); removedChannels.add(channelListener.getChannelID());
} }
//remove the channels if all listeners for that channel have been removed
for (Long removedChannel : removedChannels) { for (Long removedChannel : removedChannels) {
list = listeners.get(removedChannel); list = listeners.get(removedChannel);
if (list.isEmpty()) { if (list.isEmpty()) {
listeners.remove(removedChannel); listeners.remove(removedChannel);
} }
} }
//clear the buffers
removedChannels.clear(); removedChannels.clear();
removeBuffer.clear(); removeBuffer.clear();
} }

View file

@ -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.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View file

@ -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; 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{ public abstract class Section extends MessageSender implements ChannelListener{
private final long textChannelID; private final long textChannelID;
private final long userID; 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) { public Section(long textChannelID, long userID) {
this.textChannelID = textChannelID; this.textChannelID = textChannelID;
this.userID = userID; this.userID = userID;
@ -14,17 +22,24 @@ public abstract class Section extends MessageSender implements ChannelListener{
ChannelMessageEventManager.addListener(this, textChannelID); 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 @Override
public void messageReceived(MessageReceivedEvent event) { public void messageReceived(MessageReceivedEvent event) {
this.event = event; this.event = event;
called(event.getMessage().getContentRaw()); called(event.getMessage().getContentRaw());
} }
public Section(long textChannelID) { /**
this.textChannelID = textChannelID; * End the section.
this.userID = 0; */
}
protected void dispose(){ protected void dispose(){
ChannelMessageEventManager.removeListener(this); ChannelMessageEventManager.removeListener(this);
} }

View file

@ -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.Message;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; 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; 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 { public class MultiPageEmbed extends ReactionAdapter {
private static final String NEXT_PAGE_DEFAULT_REACTION = "\u25b6\ufe0f"; private static final String NEXT_PAGE_DEFAULT_REACTION = "\u25b6\ufe0f";
private static final String PREVIOUS_PAGE_DEFAULT_REACTION = "\u25c0\ufe0f"; private static final String PREVIOUS_PAGE_DEFAULT_REACTION = "\u25c0\ufe0f";
@ -19,10 +22,22 @@ public class MultiPageEmbed extends ReactionAdapter {
private final String nextReaction; 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) { public MultiPageEmbed(MessageReceivedEvent event, MessageEmbed... pages) {
this(event, PREVIOUS_PAGE_DEFAULT_REACTION, NEXT_PAGE_DEFAULT_REACTION, 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) { public MultiPageEmbed(MessageReceivedEvent event, String prevReaction, String nextReaction, MessageEmbed[] pages) {
this.prevReaction = prevReaction; this.prevReaction = prevReaction;
this.nextReaction = nextReaction; this.nextReaction = nextReaction;
@ -53,7 +68,10 @@ public class MultiPageEmbed extends ReactionAdapter {
} }
} }
} }
Objects.requireNonNull(event.getUser(), "Reaction user was null"); if (event.getUser() != null) {
event.getReaction().removeReaction(event.getUser()).queue(); event.getReaction().removeReaction(event.getUser()).queue();
} else {
System.err.println("[MultiPageEmbed] Reaction user was null");
}
} }
} }