Message Reaction Debug messages

This commit is contained in:
Savvy 2018-02-26 18:12:29 -05:00
parent e0d66b816d
commit 5f70ed36a2
8 changed files with 144 additions and 86 deletions

View File

@ -35,6 +35,8 @@ import io.rixa.bot.events.ReadyListener;
import io.rixa.bot.events.UserListener; import io.rixa.bot.events.UserListener;
import io.rixa.bot.guild.RixaGuild; import io.rixa.bot.guild.RixaGuild;
import io.rixa.bot.guild.manager.GuildManager; import io.rixa.bot.guild.manager.GuildManager;
import io.rixa.bot.reactions.ReactManager;
import io.rixa.bot.reactions.react.HelpReaction;
import io.rixa.bot.user.RixaUser; import io.rixa.bot.user.RixaUser;
import io.rixa.bot.user.manager.UserManager; import io.rixa.bot.user.manager.UserManager;
import io.rixa.bot.utils.FileUtils; import io.rixa.bot.utils.FileUtils;
@ -63,6 +65,8 @@ public class Rixa {
@Getter @Getter
private CommandHandler commandHandler; private CommandHandler commandHandler;
@Getter @Getter
private ReactManager reactManager;
@Getter
private Configuration configuration; private Configuration configuration;
@Getter @Getter
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@ -75,15 +79,17 @@ public class Rixa {
private Rixa() { private Rixa() {
instance = this; instance = this;
logger = Logger.getLogger(Rixa.class.getCanonicalName()); this.logger = Logger.getLogger(Rixa.class.getCanonicalName());
objectMapper = new ObjectMapper(new YAMLFactory()); this.objectMapper = new ObjectMapper(new YAMLFactory());
defaultPath = new File("Rixa/"); this.defaultPath = new File("Rixa/");
commandHandler = new CommandHandler(); this.commandHandler = new CommandHandler();
shardList = new ArrayList<>(); this.reactManager = new ReactManager();
defaultPath.mkdirs(); this.shardList = new ArrayList<>();
loadConfiguration(); this.defaultPath.mkdirs();
registerCommands(); this.loadConfiguration();
loadJDA(); this.registerCommands();
this.registerReactions();
this.loadJDA();
} }
public static Rixa getInstance() { public static Rixa getInstance() {
@ -128,6 +134,10 @@ public class Rixa {
timeUp = System.currentTimeMillis(); timeUp = System.currentTimeMillis();
} }
private void registerReactions() {
this.reactManager.registerReact(new HelpReaction("Help"));
}
private void registerCommands() { private void registerCommands() {
this.commandHandler.registerCommands( this.commandHandler.registerCommands(
new AdviceCommand("advice", RixaPermission.NONE, "Receive advice from the great beyond...", new AdviceCommand("advice", RixaPermission.NONE, "Receive advice from the great beyond...",

View File

@ -37,7 +37,7 @@ public class YoutubeSearch {
return items.getJSONObject(index).getJSONObject("id").getString("kind"); return items.getJSONObject(index).getJSONObject("id").getString("kind");
} }
public String getVideoId(int index) { public String getVideoId(int index) throws JSONException {
if (!hasVideoId(index)) { if (!hasVideoId(index)) {
index++; index++;
} }
@ -45,7 +45,7 @@ public class YoutubeSearch {
} }
public boolean hasVideoId(int index) { public boolean hasVideoId(int index) {
return items.getJSONObject(index).getJSONObject("id").has("videoID"); return items.getJSONObject(index).getJSONObject("id").has("videoId");
} }
public String getPublishedTime(int index) { public String getPublishedTime(int index) {

View File

@ -14,18 +14,10 @@ import net.dv8tion.jda.core.entities.TextChannel;
public abstract class Command { public abstract class Command {
@Getter @Getter @Setter private String command, description;
@Setter @Getter @Setter private RixaPermission permission;
private String command, description; @Getter @Setter private List<String> aliases;
@Getter @Getter @Setter private CommandType commandType;
@Setter
private RixaPermission permission;
@Getter
@Setter
private List<String> aliases;
@Getter
@Setter
private CommandType commandType;
public Command(String command) { public Command(String command) {
this(command, RixaPermission.NONE, "Undefined", CommandType.USER, Collections.emptyList()); this(command, RixaPermission.NONE, "Undefined", CommandType.USER, Collections.emptyList());
@ -45,11 +37,11 @@ public abstract class Command {
public Command(String command, RixaPermission rixaPermission, String description, public Command(String command, RixaPermission rixaPermission, String description,
CommandType commandType, List<String> aliases) { CommandType commandType, List<String> aliases) {
setCommand(command); this.setCommand(command);
setPermission(rixaPermission); this.setPermission(rixaPermission);
setDescription(description); this.setDescription(description);
setAliases(aliases); this.setAliases(aliases);
this.commandType = commandType; this.setCommandType(commandType);
} }
// public abstract void execute(GuildMessageReceivedEvent event); // public abstract void execute(GuildMessageReceivedEvent event);

View File

@ -6,32 +6,31 @@ import io.rixa.bot.commands.perms.RixaPermission;
import io.rixa.bot.guild.RixaGuild; import io.rixa.bot.guild.RixaGuild;
import io.rixa.bot.guild.manager.GuildManager; import io.rixa.bot.guild.manager.GuildManager;
import io.rixa.bot.guild.modules.module.LevelsModule; import io.rixa.bot.guild.modules.module.LevelsModule;
import io.rixa.bot.user.RixaUser;
import io.rixa.bot.user.manager.UserManager;
import io.rixa.bot.utils.DiscordUtils; import io.rixa.bot.utils.DiscordUtils;
import io.rixa.bot.utils.MessageFactory; import io.rixa.bot.utils.MessageFactory;
import net.dv8tion.jda.core.MessageBuilder; import java.util.ArrayList;
import java.util.List;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.core.entities.User;
import java.util.ArrayList;
import java.util.List;
public class LeaderboardsCommand extends Command { public class LeaderboardsCommand extends Command {
public LeaderboardsCommand(String command, RixaPermission rixaPermission, String description, CommandType commandType { public LeaderboardsCommand(String command, RixaPermission rixaPermission, String description,
CommandType commandType) {
super(command, rixaPermission, description, commandType); super(command, rixaPermission, description, commandType);
} }
@Override @Override
public void execute(String commandLabel, Guild guild, Member member, TextChannel channel, String[] args) { public void execute(String commandLabel, Guild guild, Member member, TextChannel channel,
String[] args) {
RixaGuild rixaGuild = GuildManager.getInstance().getGuild(guild); RixaGuild rixaGuild = GuildManager.getInstance().getGuild(guild);
LevelsModule levelsModule = (LevelsModule) rixaGuild.getModule("Levels"); LevelsModule levelsModule = (LevelsModule) rixaGuild.getModule("Levels");
int page = 1; int page = 1;
List<String> leaderboard = getLeaderboard(rixaGuild, page); List<String> leaderboard = getLeaderboard(rixaGuild, page);
MessageFactory.create(leaderboard.isEmpty() ? "No users found!" : (String.join("\n", leaderboard))) MessageFactory
.create(leaderboard.isEmpty() ? "No users found!" : (String.join("\n", leaderboard)))
.setAuthor("Leaderboard: " + guild.getName(), guild.getIconUrl()) .setAuthor("Leaderboard: " + guild.getName(), guild.getIconUrl())
.setColor(member.getColor()) .setColor(member.getColor())
.queue(channel, message -> { .queue(channel, message -> {

View File

@ -1,28 +1,64 @@
package io.rixa.bot.events; package io.rixa.bot.events;
import com.mysql.jdbc.StringUtils;
import io.rixa.bot.Rixa; import io.rixa.bot.Rixa;
import io.rixa.bot.commands.Command; import io.rixa.bot.commands.Command;
import io.rixa.bot.commands.exceptions.CommandNotFoundException; import io.rixa.bot.commands.exceptions.CommandNotFoundException;
import io.rixa.bot.commands.exceptions.ReactNotFoundException;
import io.rixa.bot.commands.perms.RixaPermission; import io.rixa.bot.commands.perms.RixaPermission;
import io.rixa.bot.guild.RixaGuild; import io.rixa.bot.guild.RixaGuild;
import io.rixa.bot.guild.manager.GuildManager; import io.rixa.bot.guild.manager.GuildManager;
import io.rixa.bot.guild.modules.module.ConversationModule; import io.rixa.bot.guild.modules.module.ConversationModule;
import io.rixa.bot.reactions.React;
import io.rixa.bot.user.RixaUser; import io.rixa.bot.user.RixaUser;
import io.rixa.bot.user.manager.UserManager; import io.rixa.bot.user.manager.UserManager;
import io.rixa.bot.utils.DiscordUtils; import io.rixa.bot.utils.DiscordUtils;
import io.rixa.bot.utils.MessageFactory; import io.rixa.bot.utils.MessageFactory;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.events.message.guild.react.GuildMessageReactionAddEvent; import net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.core.hooks.SubscribeEvent; import net.dv8tion.jda.core.hooks.SubscribeEvent;
public class MessageListener { public class MessageListener {
@SubscribeEvent @SubscribeEvent
public void onReactionAdded(GuildMessageReactionAddEvent event) { public void onReactionAdded(MessageReactionAddEvent event) {
if (event.getUser().isBot()) return; System.out.println("Reaction Event Triggered - 1");
if (event.getUser().isBot()) {
return;
}
event.getChannel().getMessageById(event.getMessageId()).queue(message -> {
React react = this.check(message);
if (react != null) {
react.onReact(event);
}
});
}
private React check(Message message) {
if (message == null || message.getEmbeds().isEmpty()) {
return null;
}
MessageEmbed messageEmbed = message.getEmbeds().get(0);
if (StringUtils.isNullOrEmpty(messageEmbed.getTitle())) {
return null;
}
String[] titleSplit = messageEmbed.getTitle().split(": ");
if (titleSplit[0].equalsIgnoreCase("Leaderboard")) {
return null; // Not sure if this is required anymore.
}
try {
React react = Rixa.getInstance().getReactManager().getReaction(titleSplit[0]);
if (react != null) {
return react;
}
} catch (ReactNotFoundException ignored) {
}
return null;
} }
@SubscribeEvent @SubscribeEvent

View File

@ -1,10 +1,17 @@
package io.rixa.bot.reactions; package io.rixa.bot.reactions;
import net.dv8tion.jda.core.events.message.guild.react.GuildMessageReactionAddEvent; import lombok.Getter;
import net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.core.hooks.SubscribeEvent;
public interface React { public abstract class React {
void onReact(GuildMessageReactionAddEvent event); @Getter private final String name;
String getName(); public React(String name) {
this.name = name;
}
@SubscribeEvent
public abstract void onReact(MessageReactionAddEvent event);
} }

View File

@ -1,7 +1,5 @@
package io.rixa.bot.reactions; package io.rixa.bot.reactions;
import io.rixa.bot.commands.Command;
import io.rixa.bot.commands.exceptions.CommandNotFoundException;
import io.rixa.bot.commands.exceptions.ReactNotFoundException; import io.rixa.bot.commands.exceptions.ReactNotFoundException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -2,40 +2,56 @@ package io.rixa.bot.reactions.react;
import io.rixa.bot.Rixa; import io.rixa.bot.Rixa;
import io.rixa.bot.commands.handler.CommandHandler; import io.rixa.bot.commands.handler.CommandHandler;
import io.rixa.bot.commands.handler.CommandType;
import io.rixa.bot.guild.RixaGuild; import io.rixa.bot.guild.RixaGuild;
import io.rixa.bot.guild.manager.GuildManager; import io.rixa.bot.guild.manager.GuildManager;
import io.rixa.bot.reactions.React; import io.rixa.bot.reactions.React;
import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageEmbed; import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.events.message.guild.react.GuildMessageReactionAddEvent; import net.dv8tion.jda.core.events.message.guild.react.GuildMessageReactionAddEvent;
import net.dv8tion.jda.core.events.message.priv.react.PrivateMessageReactionAddEvent;
import net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.core.exceptions.ErrorResponseException; import net.dv8tion.jda.core.exceptions.ErrorResponseException;
import net.dv8tion.jda.core.hooks.SubscribeEvent; import net.dv8tion.jda.core.hooks.SubscribeEvent;
import org.intellij.lang.annotations.Subst;
public class HelpReaction implements React { public class HelpReaction extends React {
public HelpReaction(String name) {
super(name);
}
@Override @Override
@SubscribeEvent @SubscribeEvent
public void onReact(GuildMessageReactionAddEvent event) { public void onReact(MessageReactionAddEvent event) {
System.out.println("Help Triggered");
if (event.getUser().getId().equalsIgnoreCase(event.getJDA().getSelfUser().getId())) { if (event.getUser().getId().equalsIgnoreCase(event.getJDA().getSelfUser().getId())) {
return; return;
} }
System.out.println("Not bot");
Message message = event.getChannel().getMessageById(event.getMessageId()).complete(); Message message = event.getChannel().getMessageById(event.getMessageId()).complete();
this.execute(message, event.getReaction().getReactionEmote().getName(), event.getJDA());
}
private void execute(Message message, String emoteName, JDA jda) {
System.out.println("Executed");
MessageEmbed messageEmbed = message.getEmbeds().get(0); MessageEmbed messageEmbed = message.getEmbeds().get(0);
if (!messageEmbed.getTitle().contains(": ")) { if (!messageEmbed.getTitle().contains(": ")) {
return; return;
} }
String title = messageEmbed.getTitle().split(": ")[1]; String title = messageEmbed.getTitle().split(": ")[1];
RixaGuild rixaGuild = GuildManager.getInstance().getGuild(event.getJDA().getGuildById(title)); RixaGuild rixaGuild = GuildManager.getInstance().getGuild(jda.getGuildById(title));
if (rixaGuild == null) { if (rixaGuild == null) {
return; return;
} }
String prefix = rixaGuild.getSettings().getPrefix(); String prefix = rixaGuild.getSettings().getPrefix();
EmbedBuilder embedBuilder; EmbedBuilder embedBuilder;
try { try {
switch (event.getReaction().getReactionEmote().getName()) { switch (emoteName) {
case "\uD83D\uDDD1":// waste bin case "\uD83D\uDDD1":// waste bin
if (message.getAuthor().getId().equalsIgnoreCase(event.getJDA().getSelfUser().getId())) { if (message.getAuthor().getId().equalsIgnoreCase(jda.getSelfUser().getId())) {
message.delete().complete(); message.delete().complete();
} }
break; break;
@ -48,11 +64,12 @@ public class HelpReaction implements React {
embedBuilder.setTitle(String.format("Help: %s", title)); embedBuilder.setTitle(String.format("Help: %s", title));
embedBuilder.setDescription(stringBuilder); embedBuilder.setDescription(stringBuilder);
Rixa.getInstance().getCommandHandler().getAllCommands().values() Rixa.getInstance().getCommandHandler().getAllCommands().values()
.stream().filter(cmd -> cmd.getCommandType() == CommandType.USER)
.forEach(cmd -> embedBuilder.addField(prefix + cmd.getCommand(), .forEach(cmd -> embedBuilder.addField(prefix + cmd.getCommand(),
cmd.getDescription(), false)); cmd.getDescription(), false));
message.editMessage(embedBuilder.build()).queue(); message.editMessage(embedBuilder.build()).queue();
break; break;
/*case "\u0032\u20E3": // two emoji case "\u0032\u20E3": // two emoji
embedBuilder = new EmbedBuilder(); embedBuilder = new EmbedBuilder();
stringBuilder = "\u2753" + stringBuilder = "\u2753" +
" **Staff Commands Help**" + " **Staff Commands Help**" +
@ -61,12 +78,11 @@ public class HelpReaction implements React {
embedBuilder.setTitle(String.format("Help: %s", title)); embedBuilder.setTitle(String.format("Help: %s", title));
embedBuilder.setDescription(stringBuilder); embedBuilder.setDescription(stringBuilder);
Rixa.getInstance().getCommandHandler().getAllCommands().values().stream() Rixa.getInstance().getCommandHandler().getAllCommands().values().stream()
.filter(cmd -> cmd.getAnnotation().type() == CommandType.ADMIN .filter(cmd -> cmd.getCommandType() == CommandType.STAFF)
|| cmd.getAnnotation().type() == CommandType.MOD) .forEach(cmd -> embedBuilder.addField(prefix + cmd.getCommand(),
.forEach(cmd -> embedBuilder.addField(prefix + cmd.getAnnotation().mainCommand(), cmd.getDescription(), false));
cmd.getAnnotation().description(), false));
message.editMessage(embedBuilder.build()).queue(); message.editMessage(embedBuilder.build()).queue();
break;*/ break;
case "\u0033\u20E3": // three emoji case "\u0033\u20E3": // three emoji
embedBuilder = new EmbedBuilder(); embedBuilder = new EmbedBuilder();
stringBuilder = "\u2753" + stringBuilder = "\u2753" +