From 04040925bf63a1491ede145afeda4f282a1297a5 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 16 Oct 2017 17:03:17 +0200 Subject: [PATCH] Adding auto delete of commands on chat --- src/net/Broken/BotListener.java | 5 +- src/net/Broken/Commandes/Help.java | 39 +++++++--- src/net/Broken/Commandes/Move.java | 44 ++++++++++-- src/net/Broken/Commandes/PingCommande.java | 15 +++- src/net/Broken/Commandes/Spam.java | 83 ++++++++++++++++++---- src/net/Broken/MainBot.java | 3 +- src/net/Broken/Outils/AntiSpam.java | 14 +++- src/net/Broken/Outils/MessageTimeOut.java | 2 +- 8 files changed, 169 insertions(+), 36 deletions(-) diff --git a/src/net/Broken/BotListener.java b/src/net/Broken/BotListener.java index 91870eb..84b215b 100644 --- a/src/net/Broken/BotListener.java +++ b/src/net/Broken/BotListener.java @@ -9,6 +9,7 @@ import net.dv8tion.jda.core.JDA; 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; @@ -43,7 +44,9 @@ public class BotListener extends ListenerAdapter { @Override public void onGuildMemberJoin(GuildMemberJoinEvent event) { logger.info(event.getUser().getName()+ "join the guild, move it!"); - new Move().exc(event.getMember(),event.getJDA().getRolesByName("Rat_d'égout",true),false,event.getJDA().getGuilds().get(0),event.getJDA().getGuilds().get(0).getManager()); + new Move().exc(event.getMember(),event.getJDA().getRolesByName("Newbies",true),false,event.getJDA().getGuilds().get(0),event.getJDA().getGuilds().get(0).getManager()); + TextChannel chanel = event.getGuild().getTextChannelsByName("accueil", true).get(0); + chanel.sendMessage("Salut "+event.getUser().getAsMention()+"! Ecris ton nom, prénom, ta promotion et ton groupe ici! Un admin te donnera accées a ton groupe!").complete(); MainBot.roleFlag = false; } diff --git a/src/net/Broken/Commandes/Help.java b/src/net/Broken/Commandes/Help.java index 091cecf..b48e302 100644 --- a/src/net/Broken/Commandes/Help.java +++ b/src/net/Broken/Commandes/Help.java @@ -3,15 +3,19 @@ package net.Broken.Commandes; import net.Broken.Commande; import net.Broken.MainBot; import net.Broken.Outils.EmbedMessageUtils; +import net.Broken.Outils.MessageTimeOut; import net.Broken.Outils.PrivateMessage; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.ChannelType; +import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.exceptions.RateLimitedException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.awt.*; +import java.util.ArrayList; +import java.util.List; import java.util.Map; /** @@ -33,9 +37,14 @@ public class Help implements Commande { if (MainBot.commandes.containsKey(argsString)) { logger.info("Aide demmander pour la cmd "+argsString+" par "+event.getAuthor().getName()); - if(!event.isFromType(ChannelType.PRIVATE)) - event.getTextChannel().sendMessage(EmbedMessageUtils.getHelp(argsString,MainBot.commandes.get(argsString).help(args))).queue(); - else{ + if(!event.isFromType(ChannelType.PRIVATE)) { + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getHelp(argsString, MainBot.commandes.get(argsString).help(args))).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); + } else{ PrivateMessage.send(event.getAuthor(), MainBot.commandes.get(argsString).help(args),logger); } @@ -43,9 +52,14 @@ public class Help implements Commande { } else { - if(!event.isFromType(ChannelType.PRIVATE)) - event.getTextChannel().sendMessage(EmbedMessageUtils.getUnknowCommand()).queue(); - else{ + if(!event.isFromType(ChannelType.PRIVATE)) { + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getUnknowCommand()).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); + } else{ PrivateMessage.send(event.getAuthor(),EmbedMessageUtils.getUnknowCommand(),logger); } logger.info("Commande Inconnue!"); @@ -57,10 +71,17 @@ public class Help implements Commande { for (Map.Entry e : MainBot.commandes.entrySet()) { txt.append("\n- ").append(e.getKey()); } - if(!event.isFromType(ChannelType.PRIVATE)) - event.getTextChannel().sendMessage(new EmbedBuilder().setTitle("Commandes envoyées par message privé").setColor(Color.green).build()).queue(); - PrivateMessage.send(event.getAuthor(),new EmbedBuilder().setTitle("Commandes du bot").setDescription(txt.toString()).setFooter("Utilise '//help ' pour plus de détails.",null).setColor(Color.green).setThumbnail(event.getJDA().getSelfUser().getAvatarUrl()).build(),logger); + if(!event.isFromType(ChannelType.PRIVATE)){ + Message rest = event.getTextChannel().sendMessage(new EmbedBuilder().setTitle("Commandes envoyées par message privé").setColor(Color.green).build()).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); + } + + PrivateMessage.send(event.getAuthor(),new EmbedBuilder().setTitle("Commandes du bot").setDescription(txt.toString()).setFooter("Utilise '//help ' pour plus de détails.",null).setColor(Color.green).setThumbnail(event.getJDA().getSelfUser().getAvatarUrl()).build(),logger); diff --git a/src/net/Broken/Commandes/Move.java b/src/net/Broken/Commandes/Move.java index 309d481..1bb461e 100644 --- a/src/net/Broken/Commandes/Move.java +++ b/src/net/Broken/Commandes/Move.java @@ -3,6 +3,7 @@ package net.Broken.Commandes; import net.Broken.Commande; import net.Broken.MainBot; import net.Broken.Outils.EmbedMessageUtils; +import net.Broken.Outils.MessageTimeOut; import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; @@ -101,7 +102,12 @@ public class Move implements Commande { if(userL.size()<1 ||roleL.size()<1) { logger.info("Mentionnement Incorect."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Utilisateur ou Role mal mentioner.")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Utilisateur ou Role mal mentioner.")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } else { @@ -117,7 +123,12 @@ public class Move implements Commande { boolean erreur=this.exc(user,roleL,true,serveur,serveur.getManager()); if(erreur) { - event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Verifier le rôle cible. ")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Verifier le rôle cible. ")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } else { @@ -137,10 +148,20 @@ public class Move implements Commande { } - event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveOk("Déplacement de "+user.getEffectiveName()+" vers "+roleStr.toString()+" reussi.")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveOk("Déplacement de "+user.getEffectiveName()+" vers "+roleStr.toString()+" reussi.")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } }catch (HierarchyException e){ - event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Impossible de déplacer un "+user.getRoles().get(0).getAsMention())).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Impossible de déplacer un "+user.getRoles().get(0).getAsMention())).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); logger.error("Hierarchy error, please move bot's role on top!"); } @@ -149,7 +170,12 @@ public class Move implements Commande { else { logger.info("Autorisation insuffisante, deplacement refusé"); - event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Vous n'avez pas l'autorisation de faicre ça!")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Vous n'avez pas l'autorisation de faicre ça!")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } @@ -158,8 +184,12 @@ public class Move implements Commande { else { logger.warn("Arguments maquant."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Arguments manquant.")).queue(); - + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Arguments manquant.")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } else diff --git a/src/net/Broken/Commandes/PingCommande.java b/src/net/Broken/Commandes/PingCommande.java index 5bd0d29..c4b502a 100644 --- a/src/net/Broken/Commandes/PingCommande.java +++ b/src/net/Broken/Commandes/PingCommande.java @@ -1,12 +1,17 @@ package net.Broken.Commandes; import net.Broken.Commande; +import net.Broken.MainBot; +import net.Broken.Outils.MessageTimeOut; import net.dv8tion.jda.core.entities.ChannelType; +import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; import java.sql.Timestamp; import java.time.*; +import java.util.ArrayList; +import java.util.List; /** @@ -26,8 +31,14 @@ public class PingCommande implements Commande { long receivedTime = Timestamp.valueOf(LocalDateTime.ofInstant(event.getMessage().getCreationTime().toInstant(), ZoneId.systemDefault())).getTime(); if(event.isFromType(ChannelType.PRIVATE)) event.getPrivateChannel().sendMessage(":arrow_right: Pong! `"+((Timestamp.from(Instant.now()).getTime()-receivedTime))+"ms`").queue(); - else - event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: Pong! `"+((Timestamp.from(Instant.now()).getTime()-receivedTime))+"ms`").queue(); + else { + Message rest = event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: Pong! `"+((Timestamp.from(Instant.now()).getTime()-receivedTime))+"ms`").complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); + } LogManager.getLogger().debug("pong"); } diff --git a/src/net/Broken/Commandes/Spam.java b/src/net/Broken/Commandes/Spam.java index c78c718..73cdc84 100644 --- a/src/net/Broken/Commandes/Spam.java +++ b/src/net/Broken/Commandes/Spam.java @@ -4,10 +4,12 @@ import net.Broken.Commande; import net.Broken.MainBot; import net.Broken.Outils.AntiSpam; import net.Broken.Outils.EmbedMessageUtils; +import net.Broken.Outils.MessageTimeOut; import net.Broken.Outils.UserSpamUtils; import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Member; +import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.exceptions.RateLimitedException; @@ -113,7 +115,12 @@ public class Spam implements Commande { if(userL.size()<1) { logger.error("Utilisateur introuvable."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError(":arrow_right: Utilisateur introuvable. ","pardon")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError(":arrow_right: Utilisateur introuvable. ","pardon")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } else { Member user = serveur.getMember(userL.get(0)); @@ -133,25 +140,45 @@ public class Spam implements Commande { MainBot.spamUtils.get(user.getUser()).setOnSpam(false); } else { logger.warn("Utilisateur pas en spam."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError(":arrow_right: Utilisateur non spammeur.","pardon")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError(":arrow_right: Utilisateur non spammeur.","pardon")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } else { logger.warn("Utilisateur pas en spam."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError(":arrow_right: Utilisateur non spammeur.","pardon")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError(":arrow_right: Utilisateur non spammeur.","pardon")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } else { logger.warn("Autorisation insuffisante, pardon refusé"); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Vous n'avez pas l'autorisation de faire ça!")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Vous n'avez pas l'autorisation de faire ça!")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } } else { logger.warn("Argument manquant."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Argument manquant!","pardon")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Argument manquant!","pardon")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } @@ -175,7 +202,7 @@ public class Spam implements Commande { if(userL.size()<1) { logger.warn("Mentionnement Incorect (Spam)."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Utilisateur mal mentioner. ","extermine")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Utilisateur mal mentioner. ","extermine")).complete(); } else{ @@ -205,7 +232,7 @@ public class Spam implements Commande { else { logger.warn("Utilisateur deja en spam."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Utilisateur déjà spammeur.","extermine")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Utilisateur déjà spammeur.","extermine")).complete(); } @@ -219,7 +246,12 @@ public class Spam implements Commande { else { logger.warn("Autorisation insuffisante, extermination refusé"); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Vous n'avez pas l'autorisation de faire ça!")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Vous n'avez pas l'autorisation de faire ça!")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } @@ -229,7 +261,12 @@ public class Spam implements Commande { else { logger.warn("Argument manquant."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Argument manquant!","extermine")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Argument manquant!","extermine")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } @@ -252,7 +289,12 @@ public class Spam implements Commande { if(userL.size()<1) { logger.warn("Utilisateur introuvable."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Utilisateur introuvable.","reset")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Utilisateur introuvable.","reset")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } else { @@ -269,13 +311,23 @@ public class Spam implements Commande { ****************************/ if (MainBot.spamUtils.containsKey(user.getUser())) { logger.info("Reset du multiplicateur de " + user.getEffectiveName() + " réussi"); - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n *Le multiplcicateur de " + user.getEffectiveName() + " a été remit a zéro.*").queue(); + Message rest = event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n *Le multiplcicateur de " + user.getEffectiveName() + " a été remit a zéro.*").complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); MainBot.spamUtils.remove(user.getUser()); } } else { logger.warn("Autorisation insuffisante, reset refusé"); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Vous n'avez pas l'autorisation de faire ça!")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Vous n'avez pas l'autorisation de faire ça!")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } @@ -283,7 +335,12 @@ public class Spam implements Commande { else { logger.warn("Argument manquant."); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Argument manquant!","reset")).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamError("Argument manquant!","reset")).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } } else diff --git a/src/net/Broken/MainBot.java b/src/net/Broken/MainBot.java index f867259..a91f84d 100644 --- a/src/net/Broken/MainBot.java +++ b/src/net/Broken/MainBot.java @@ -34,9 +34,10 @@ public class MainBot { public static HashMap> historique =new HashMap<>(); public static HashMap message_compteur =new HashMap<>(); public static boolean roleFlag = false; - public static HashMap spamUtils = new HashMap<>(); + public static int messageTimeOut = 10; + private static Logger logger = LogManager.getLogger(); diff --git a/src/net/Broken/Outils/AntiSpam.java b/src/net/Broken/Outils/AntiSpam.java index ed71495..4d45d1d 100644 --- a/src/net/Broken/Outils/AntiSpam.java +++ b/src/net/Broken/Outils/AntiSpam.java @@ -72,7 +72,12 @@ public class AntiSpam { MainBot.spamUtils.get(user.getUser()).setMinuteur(new Minuteur(MainBot.spamUtils.get(user.getUser()).getMultip(), move.user, move.saveRoleUser, move.serveur, move.serveurManager,event)); MainBot.spamUtils.get(user.getUser()).launchMinuteur(); }catch (HierarchyException e){ - event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Impossible de déplacer un "+user.getRoles().get(0).getAsMention())).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Impossible de déplacer un "+user.getRoles().get(0).getAsMention())).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); MainBot.spamUtils.get(user.getUser()).setOnSpam(false); } @@ -128,7 +133,12 @@ public class AntiSpam { try { move.exc(user, saveRoleUser, true, serveur, serveurManager); //aSaveroleUser=saveRoleUser.get(i) }catch (HierarchyException e){ - event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Impossible de déplacer un "+user.getRoles().get(0).getAsMention())).queue(); + Message rest = event.getTextChannel().sendMessage(EmbedMessageUtils.getMoveError("Impossible de déplacer un "+user.getRoles().get(0).getAsMention())).complete(); + List messages = new ArrayList(){{ + add(rest); + add(event.getMessage()); + }}; + new MessageTimeOut(messages,MainBot.messageTimeOut).start(); logger.error("Hierarchy error"); } logger.info("["+user.getEffectiveName()+"] Fin des "+multip+"min"); diff --git a/src/net/Broken/Outils/MessageTimeOut.java b/src/net/Broken/Outils/MessageTimeOut.java index 4d1b466..01984a3 100644 --- a/src/net/Broken/Outils/MessageTimeOut.java +++ b/src/net/Broken/Outils/MessageTimeOut.java @@ -30,7 +30,7 @@ public class MessageTimeOut extends Thread{ { try { logger.debug(aMessage.getContent()); - aMessage.delete().complete(); + aMessage.delete().queue(); }catch (ErrorResponseException e){ logger.warn("Unknown Message"); }