From 4cb83c8725b4e5f527fc78f2e938c895125a5bd0 Mon Sep 17 00:00:00 2001 From: BrokenFire Date: Mon, 26 Feb 2018 11:39:13 +0100 Subject: [PATCH] Catching all exeption and add error command #22 --- src/main/java/net/Broken/BotListener.java | 63 +++++++++++-------- src/main/java/net/Broken/Commands/Error.java | 41 ++++++++++++ .../net/Broken/Tools/EmbedMessageUtils.java | 8 +-- 3 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 src/main/java/net/Broken/Commands/Error.java diff --git a/src/main/java/net/Broken/BotListener.java b/src/main/java/net/Broken/BotListener.java index f9c68d5..1ecd3e3 100644 --- a/src/main/java/net/Broken/BotListener.java +++ b/src/main/java/net/Broken/BotListener.java @@ -4,11 +4,14 @@ import net.Broken.Commands.Move; import net.Broken.Commands.Music; import net.Broken.Tools.AntiSpam; import net.Broken.Tools.Command.CommandParser; +import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.Moderateur; +import net.Broken.Tools.PrivateMessage; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.TextChannel; +import net.dv8tion.jda.core.events.ExceptionEvent; import net.dv8tion.jda.core.events.ReadyEvent; import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.core.events.guild.member.GuildMemberRoleRemoveEvent; @@ -85,38 +88,48 @@ public class BotListener extends ListenerAdapter { public void onMessageReceived(MessageReceivedEvent event) { // ----------------------Test pour eviter eco de commande------------------------- - if (event.getMessage().getContent().startsWith("//") && !event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) { - //On a detecter que c'etait une commande - //System.out.println(event.getMessage().getContent()); - MainBot.handleCommand(new CommandParser().parse(event.getMessage().getContent(), event)); - } - else if (!event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) - { + try{ + if (event.getMessage().getContent().startsWith("//") && !event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) { + //On a detecter que c'etait une commande + //System.out.println(event.getMessage().getContent()); + MainBot.handleCommand(new CommandParser().parse(event.getMessage().getContent(), event)); - if(!event.isFromType(ChannelType.PRIVATE)) { - if (!event.getTextChannel().getName().equals("le_dongeon")) { - Guild serveur = event.getGuild(); - GuildManager guildManager = serveur.getManager(); - Member user = event.getMember(); - - // appel de la methode d'analyse de message de "Moderateur" - if (!event.getAuthor().getName().equals("Aethex") && event.getMessage().getContent().length() > 0) { - - if (modo.analyse(user, serveur, guildManager, event) == 1) { - antispam.extermine(user, serveur, guildManager, true, event); - } - } else if (event.getMessage().getContent().length() == 0) - logger.error("Image detected, ignoring it."); - - } } + else if (!event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) + { + + if(!event.isFromType(ChannelType.PRIVATE)) { + if (!event.getTextChannel().getName().equals("le_dongeon")) { + Guild serveur = event.getGuild(); + GuildManager guildManager = serveur.getManager(); + Member user = event.getMember(); + + // appel de la methode d'analyse de message de "Moderateur" + if (!event.getAuthor().getName().equals("Aethex") && event.getMessage().getContent().length() > 0) { + + if (modo.analyse(user, serveur, guildManager, event) == 1) { + antispam.extermine(user, serveur, guildManager, true, event); + } + } else if (event.getMessage().getContent().length() == 0) + logger.error("Image detected, ignoring it."); + + } + } + } + }catch (Exception e){ + logger.catching(e); - - + if(event.isFromType(ChannelType.PRIVATE)) + PrivateMessage.send(event.getAuthor(), EmbedMessageUtils.getInternalError(), logger); + else + event.getTextChannel().sendMessage(EmbedMessageUtils.getInternalError()).queue(); } + + } + } diff --git a/src/main/java/net/Broken/Commands/Error.java b/src/main/java/net/Broken/Commands/Error.java new file mode 100644 index 0000000..5c5e7b7 --- /dev/null +++ b/src/main/java/net/Broken/Commands/Error.java @@ -0,0 +1,41 @@ +package net.Broken.Commands; + +import net.Broken.Commande; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.PrivateMessage; +import net.dv8tion.jda.core.entities.ChannelType; +import net.dv8tion.jda.core.events.message.MessageReceivedEvent; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class Error implements Commande{ + + private Logger logger = LogManager.getLogger(); + @Override + public boolean called(String[] args, MessageReceivedEvent event) { + return false; + } + + @Override + public void action(String[] args, MessageReceivedEvent event) { + if(event.isFromType(ChannelType.PRIVATE)) + PrivateMessage.send(event.getAuthor(), EmbedMessageUtils.getInternalError(), logger); + else + event.getTextChannel().sendMessage(EmbedMessageUtils.getInternalError()).queue(); + } + + @Override + public void executed(boolean success, MessageReceivedEvent event) { + + } + + @Override + public boolean isPrivateUsable() { + return true; + } + + @Override + public boolean isAdminCmd() { + return true; + } +} diff --git a/src/main/java/net/Broken/Tools/EmbedMessageUtils.java b/src/main/java/net/Broken/Tools/EmbedMessageUtils.java index 50d7cac..05c793d 100644 --- a/src/main/java/net/Broken/Tools/EmbedMessageUtils.java +++ b/src/main/java/net/Broken/Tools/EmbedMessageUtils.java @@ -15,9 +15,9 @@ public class EmbedMessageUtils { return new EmbedBuilder().setTitle(":warning: Commande inconnue! :warning:").setDescription(":arrow_right: Utilisez `//help` pour voirs les commandes disponible.").setColor(Color.orange).setFooter("bot.seb6596.ovh", MainBot.jda.getSelfUser().getAvatarUrl()).build(); } - public static MessageEmbed getError(String message) { + public static EmbedBuilder getError(String message) { EmbedBuilder temp = new EmbedBuilder().setTitle(":warning: Error! :warning:").setColor(Color.red).setDescription(message); - return buildStandar(temp); + return temp; } @@ -85,7 +85,7 @@ public class EmbedMessageUtils { } public static MessageEmbed getInternalError(){ - return getError("Je... Je... je ne me sent pas bien... :thermometer_face: \nContactez vite mon developeur!"); + return buildStandar(getError("Je... Je... je ne me sens pas bien... :thermometer_face: \nContactez vite mon developeur!").setImage("https://media.giphy.com/media/Rkis28kMJd1aE/giphy.gif")); } public static MessageEmbed buildStandar(EmbedBuilder embedBuilder){ @@ -95,7 +95,7 @@ public class EmbedMessageUtils { } public static MessageEmbed getUnautorized(){ - return getError("Vous n'avez pas l'autorisation de faire ça!"); + return buildStandar(getError("Vous n'avez pas l'autorisation de faire ça!")); } public static MessageEmbed getHelpList(String role, String list){