fixes and improvments

This commit is contained in:
nora 2021-01-20 20:07:13 +01:00
parent 9684b72cc2
commit 93f665c59a
11 changed files with 159 additions and 30 deletions

View file

@ -5,13 +5,13 @@ import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.Event; import net.dv8tion.jda.api.events.Event;
import java.awt.*; import java.awt.*;
import java.util.Objects;
public class Config { public class Config {
public static final String PREFIX = "kil "; public static final String PREFIX = "kil ";
public static final long NILS_ID = 414755070161453076L;
public static final Color DEFAULT_COLOR = new Color(229, 201, 255); public static final Color DEFAULT_COLOR = new Color(229, 201, 255);
public static final long THIS_ID = 801015254023798825L; public static final long THIS_ID = 801015254023798825L;
public static final long NILS_ID = 414755070161453076L;
public static EmbedBuilder getDefaultEmbed() { public static EmbedBuilder getDefaultEmbed() {
EmbedBuilder builder = new EmbedBuilder(); EmbedBuilder builder = new EmbedBuilder();
@ -20,14 +20,23 @@ public class Config {
} }
public static EmbedBuilder getDefaultEmbed(Event event) { public static EmbedBuilder getDefaultEmbed(Event event) {
User killua = event.getJDA().getUserById(Config.THIS_ID); User killua = event.getJDA().getUserById(THIS_ID);
Objects.requireNonNull(killua, "user killua not found"); if (killua == null) {
event.getJDA().retrieveUserById(THIS_ID).queue(user -> {
System.out.println("[Config] " + user.getAsTag() + " successfully retrieved.");
});
System.err.println("[Config] This bot user not cached. Retrieving user...");
EmbedBuilder builder = new EmbedBuilder();
EmbedBuilder builder = new EmbedBuilder(); builder.setColor(Config.DEFAULT_COLOR)
builder.setColor(Config.DEFAULT_COLOR). .setFooter("KilluaBot");
setThumbnail(killua.getAvatarUrl()) return builder;
.setFooter("KilluaBot"); } else {
return builder; EmbedBuilder builder = new EmbedBuilder();
builder.setColor(Config.DEFAULT_COLOR).
setThumbnail(killua.getAvatarUrl())
.setFooter("KilluaBot");
return builder;
}
} }
} }

View file

@ -35,6 +35,6 @@ public class DepartureCommand extends Command {
japaneseBuilder.addField("", DepartureSong.LYRICS_JAPANESE[i], false); japaneseBuilder.addField("", DepartureSong.LYRICS_JAPANESE[i], false);
} }
reply(event, latinBuilder.build(), japaneseBuilder.build()); reply(event, "\uD83C\uDD70", "\uD83C\uDE35", latinBuilder.build(), japaneseBuilder.build());
} }
} }

View file

@ -4,19 +4,40 @@ import com.github.nilstrieb.cofig.Config;
import com.github.nilstrieb.commands.handler.Command; import com.github.nilstrieb.commands.handler.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.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class QuoteCommand extends Command { public class QuoteCommand extends Command {
private static MessageEmbed[] quotesEmbed;
public QuoteCommand() { public QuoteCommand() {
super("quote", "Get a quote from Killua"); super("quote", "Get a quote from Killua");
} }
@Override @Override
public void called(MessageReceivedEvent event, String args) { public void called(MessageReceivedEvent event, String args) {
EmbedBuilder builder = Config.getDefaultEmbed(event) if (args.startsWith("all")) {
.addField("Killuas Quotes", KilluaQuotes.getRandomQuote(), false); reply(event, getQuotesEmbed(event));
} else {
EmbedBuilder builder = Config.getDefaultEmbed(event)
.addField("Killuas Quotes", KilluaQuotes.getRandomQuote(), false);
reply(event, builder.build()); reply(event, builder.build());
}
} }
public static MessageEmbed[] getQuotesEmbed(MessageReceivedEvent event) {
if (quotesEmbed == null) {
String[] quotes = KilluaQuotes.getAllQuotes();
quotesEmbed = new MessageEmbed[quotes.length];
for (int i = 0; i < quotes.length; i++) {
quotesEmbed[i] = Config.getDefaultEmbed(event)
.addField("Killuas Quotes", quotes[i], false)
.setFooter("Killua Quotes - " + (i + 1) + "/" + quotes.length).build();
}
}
return quotesEmbed;
}
} }

View file

@ -23,35 +23,41 @@ public abstract class Command {
CommandHandler.addCommand(name, this, hidden); CommandHandler.addCommand(name, this, hidden);
} }
public Command(String name, String description, String exampleUsage, String arguments){ public Command(String name, String description, String exampleUsage, String arguments) {
this(name, description, exampleUsage, arguments, false); this(name, description, exampleUsage, arguments, false);
} }
public Command(String name, String description){ public Command(String name, String description) {
this(name, description, name, "", false); this(name, description, name, "", false);
} }
public abstract void called(MessageReceivedEvent event, String args); public abstract void called(MessageReceivedEvent event, String args);
protected void reply(MessageReceivedEvent event, String message) { protected void reply(MessageReceivedEvent event, String message) {
if(!message.equals("")){ if (!message.equals("")) {
event.getTextChannel().sendMessage(message).queue(); event.getTextChannel().sendMessage(message).queue();
} }
} }
protected void reply(MessageReceivedEvent event, MessageEmbed embed) { protected void reply(MessageReceivedEvent event, MessageEmbed embed) {
if(!embed.isEmpty()){ if (!embed.isEmpty()) {
event.getTextChannel().sendMessage(embed).queue(); event.getTextChannel().sendMessage(embed).queue();
} }
} }
protected void reply(MessageReceivedEvent event, MessageEmbed ... embeds){ protected void reply(MessageReceivedEvent event, MessageEmbed... embeds) {
if(!embeds[0].isEmpty()){ if (!embeds[0].isEmpty()) {
event.getTextChannel().sendMessage(embeds[0]).queue(message -> new MultiPageEmbed(message, embeds)); event.getTextChannel().sendMessage(embeds[0]).queue(message -> new MultiPageEmbed(message, 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));
}
}
protected void deleteMsg(MessageReceivedEvent event, long delay) { protected void deleteMsg(MessageReceivedEvent event, long delay) {
new Timer().schedule( new Timer().schedule(
new TimerTask() { new TimerTask() {

View file

@ -1,6 +1,7 @@
package com.github.nilstrieb.commands.handler; package com.github.nilstrieb.commands.handler;
import com.github.nilstrieb.cofig.Config; import com.github.nilstrieb.cofig.Config;
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.Event;
@ -31,12 +32,10 @@ public class CommandHandler {
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];
System.out.println("[CHandler] cmd: '" + command + "'" + " args: '" + split[1] + "'"); System.out.println(ConsoleColors.GREEN + "[CHandler] cmd: '" + command + "'" + " args: '" + split[1] + "'" + ConsoleColors.RESET);
if (commands.containsKey(command)) { if (commands.containsKey(command)) {
System.out.println("[CHandler] command exists: " + command);
commands.get(command).called(event, split[1]); commands.get(command).called(event, split[1]);
} else if (hiddenCommands.containsKey(command)) { } else if (hiddenCommands.containsKey(command)) {
System.out.println("[CHandler] hidden command exists: " + command);
hiddenCommands.get(command).called(event, split[1]); hiddenCommands.get(command).called(event, split[1]);
} }
} }

View file

@ -1,6 +1,7 @@
package com.github.nilstrieb.listener; package com.github.nilstrieb.listener;
import com.github.nilstrieb.commands.handler.CommandHandler; import com.github.nilstrieb.commands.handler.CommandHandler;
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;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -8,7 +9,7 @@ import org.jetbrains.annotations.NotNull;
public class CommandListener extends ListenerAdapter { public class CommandListener extends ListenerAdapter {
@Override @Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) { public void onMessageReceived(@NotNull MessageReceivedEvent event) {
System.out.println("[CListener] Received message: '" + event.getMessage().getContentRaw() + "'"); System.out.println(ConsoleColors.CYAN + "[CListener] Received message: '" + event.getMessage().getContentRaw() + "'" + ConsoleColors.RESET);
if (!event.getAuthor().isBot()) { if (!event.getAuthor().isBot()) {
CommandHandler.call(event); CommandHandler.call(event);
} }

View file

@ -11,15 +11,23 @@ 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) {
if (!Objects.requireNonNull(event.getUser(), "message author is null").isBot()) { if(event.getUser() == null){
ReactionEventManager.onReactionAdd(event); System.err.println("[ReactionEventListener] ADD Reaction User is null. Message: " + event.getMessageId() + " emote:" + event.getReactionEmote());
} else {
if (!event.getUser().isBot()) {
ReactionEventManager.onReactionAdd(event);
}
} }
} }
@Override @Override
public void onMessageReactionRemove(@NotNull MessageReactionRemoveEvent event) { public void onMessageReactionRemove(@NotNull MessageReactionRemoveEvent event) {
if (!Objects.requireNonNull(event.getUser(), "message author is null").isBot()) { if(event.getUser() == null){
ReactionEventManager.onReactionRemove(event); System.err.println("[ReactionEventListener] REMOVE Reaction User is null. Message: " + event.getMessageId() + " emote:" + event.getReactionEmote());
} else {
if (!event.getUser().isBot()) {
ReactionEventManager.onReactionRemove(event);
}
} }
} }
} }

View file

@ -1,5 +1,6 @@
package com.github.nilstrieb.reactions; package com.github.nilstrieb.reactions;
import com.github.nilstrieb.util.ConsoleColors;
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;
@ -17,6 +18,8 @@ public class ReactionEventManager {
} }
public static void onReactionAdd(MessageReactionAddEvent event){ public static void onReactionAdd(MessageReactionAddEvent event){
System.out.println(ConsoleColors.PURPLE + "[ReactionEventManager] Reaction added: " + event.getReactionEmote() +
" by " + event.getUser().getAsTag() + ConsoleColors.RESET);
long message = event.getMessageIdLong(); long message = event.getMessageIdLong();
ReactionListener listener = currentReactions.get(message); ReactionListener listener = currentReactions.get(message);
if (listener != null) { if (listener != null) {
@ -25,6 +28,8 @@ public class ReactionEventManager {
} }
public static void onReactionRemove(MessageReactionRemoveEvent event){ public static void onReactionRemove(MessageReactionRemoveEvent event){
System.out.println(ConsoleColors.PURPLE + "[ReactionEventManager] Reaction removed: " + event.getReactionEmote() +
" by " + event.getUser().getAsTag() + ConsoleColors.RESET);
long message = event.getMessageIdLong(); long message = event.getMessageIdLong();
ReactionListener listener = currentReactions.get(message); ReactionListener listener = currentReactions.get(message);
if (listener != null) { if (listener != null) {

View file

@ -0,0 +1,76 @@
package com.github.nilstrieb.util;
public class ConsoleColors {
// Reset
public static final String RESET = "\033[0m"; // Text Reset
// Regular Colors
public static final String BLACK = "\033[0;30m"; // BLACK
public static final String RED = "\033[0;31m"; // RED
public static final String GREEN = "\033[0;32m"; // GREEN
public static final String YELLOW = "\033[0;33m"; // YELLOW
public static final String BLUE = "\033[0;34m"; // BLUE
public static final String PURPLE = "\033[0;35m"; // PURPLE
public static final String CYAN = "\033[0;36m"; // CYAN
public static final String WHITE = "\033[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = "\033[1;30m"; // BLACK
public static final String RED_BOLD = "\033[1;31m"; // RED
public static final String GREEN_BOLD = "\033[1;32m"; // GREEN
public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW
public static final String BLUE_BOLD = "\033[1;34m"; // BLUE
public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE
public static final String CYAN_BOLD = "\033[1;36m"; // CYAN
public static final String WHITE_BOLD = "\033[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK
public static final String RED_UNDERLINED = "\033[4;31m"; // RED
public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK
public static final String RED_BACKGROUND = "\033[41m"; // RED
public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN
public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK
public static final String RED_BRIGHT = "\033[0;91m"; // RED
public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN
public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE
}

View file

@ -26,4 +26,8 @@ public class KilluaQuotes {
public static String getRandomQuote() { public static String getRandomQuote() {
return quotes[random.nextInt(quotes.length)]; return quotes[random.nextInt(quotes.length)];
} }
public static String[] getAllQuotes() {
return quotes;
}
} }

View file

@ -10,8 +10,8 @@ import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;
import java.util.Objects; import java.util.Objects;
public class MultiPageEmbed implements ReactionListener { public class MultiPageEmbed implements ReactionListener {
private static final String NEXT_PAGE_DEFAULT_REACTION = "\u27a1"; private static final String NEXT_PAGE_DEFAULT_REACTION = "\u25b6\ufe0f";
private static final String PREVIOUS_PAGE_DEFAULT_REACTION = "\u2b05"; private static final String PREVIOUS_PAGE_DEFAULT_REACTION = "\u25c0\ufe0f";
private final Message message; private final Message message;
private final MessageEmbed[] pages; private final MessageEmbed[] pages;