Refactored how commands are handled

This commit is contained in:
Isaac Parenteau
2020-01-01 13:36:33 -06:00
parent c8260444bb
commit c85bea52a2
17 changed files with 661 additions and 370 deletions

View File

@@ -0,0 +1,80 @@
package net.locusworks.discord.eighttrack.events;
import java.util.List;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.requests.ErrorResponse;
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
import net.locusworks.discord.eighttrack.handlers.ReactionHandler;
public abstract class AbstractEvent {
private String command;
protected AbstractEvent(String command) {
this.command = command;
}
public String getCommand() {
return this.command;
}
public abstract void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands);
public abstract String help();
protected boolean isAdmin(GuildMessageReceivedEvent event) {
if (!event.getMessage().getMember().hasPermission(Permission.ADMINISTRATOR, Permission.MANAGE_SERVER)) {
event.getChannel().sendMessage(String.format("Sorry <@%s> i can't do that. *psst: you have to have manage server, or administrator role*", event.getMember().getIdLong())).queue();
return false;
}
return true;
}
protected boolean checkCommands(GuildMessageReceivedEvent event, List<String> commands, String msg) {
return checkCommands(event, commands, 0, msg);
}
protected boolean checkCommands(GuildMessageReceivedEvent event, List<String> commands, int minParams, String msg) {
if (commands == null || commands.isEmpty() || commands.size() < minParams) {
event.getChannel().sendMessage(msg).queue();
return false;
}
return true;
}
protected void sendMessage(GuildMessageReceivedEvent event, String msg) {
sendMessage(event, msg, false);
}
protected void sendMessage(GuildMessageReceivedEvent event, String msg, boolean wait) {
if (!wait) {
event.getChannel().sendMessage(msg).queue();
} else {
event.getChannel().sendMessage(msg).complete();
}
}
protected void sendMessage(GuildMessageReceivedEvent event, String msgFmt, Object... args) {
sendMessage(event, String.format(msgFmt, args));
}
protected void sendMessage(GuildMessageReceivedEvent event, boolean wait, String msgFmt, Object... args) {
sendMessage(event, String.format(msgFmt, args), wait);
}
protected void deleteMessage(Message msg, ReactionHandler reactionHandler, Long guildId) {
try {
msg.delete().complete();
if (reactionHandler != null) {
reactionHandler.removeReactionListener(guildId, msg.getIdLong());
}
} catch (ErrorResponseException ex) {
if (ex.getErrorResponse() != ErrorResponse.UNKNOWN_MESSAGE) throw ex;
}
}
}