From b1f33149b93dbd2b3f185b5a5c933c689ae5e65b Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 4 Oct 2017 12:14:21 +0200 Subject: [PATCH] AntiSpam now delete spam message! --- src/net/Broken/BotListener.java | 1 + src/net/Broken/Commandes/Spam.java | 3 +- src/net/Broken/Commandes/SpamInfo.java | 16 ++++----- src/net/Broken/Outils/AntiSpam.java | 16 +++++---- src/net/Broken/Outils/MessageTimeOut.java | 41 +++++++++++++++++++++++ src/net/Broken/Outils/Moderateur.java | 9 ++--- src/net/Broken/Outils/UserSpamUtils.java | 20 ++++++++++- 7 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 src/net/Broken/Outils/MessageTimeOut.java diff --git a/src/net/Broken/BotListener.java b/src/net/Broken/BotListener.java index 3110167..4cd740d 100644 --- a/src/net/Broken/BotListener.java +++ b/src/net/Broken/BotListener.java @@ -3,6 +3,7 @@ package net.Broken; import net.Broken.Commandes.Move; import net.Broken.Commandes.Spam; import net.Broken.Outils.AntiSpam; +import net.Broken.Outils.MessageTimeOut; import net.Broken.Outils.Moderateur; import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.entities.ChannelType; diff --git a/src/net/Broken/Commandes/Spam.java b/src/net/Broken/Commandes/Spam.java index 56e3756..c78c718 100644 --- a/src/net/Broken/Commandes/Spam.java +++ b/src/net/Broken/Commandes/Spam.java @@ -15,6 +15,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -320,7 +321,7 @@ public class Spam implements Commande { } else { - MainBot.spamUtils.put(user.getUser(),new UserSpamUtils(user)); + MainBot.spamUtils.put(user.getUser(),new UserSpamUtils(user,new ArrayList<>())); MainBot.spamUtils.get(user.getUser()).setMultip(multi); } diff --git a/src/net/Broken/Commandes/SpamInfo.java b/src/net/Broken/Commandes/SpamInfo.java index 62e04fc..ef9c6ea 100644 --- a/src/net/Broken/Commandes/SpamInfo.java +++ b/src/net/Broken/Commandes/SpamInfo.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.Broken.Outils.PrivateMessage; import net.Broken.Outils.UserSpamUtils; import net.dv8tion.jda.core.EmbedBuilder; @@ -15,7 +16,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.awt.*; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.concurrent.TimeUnit; /** @@ -178,14 +181,11 @@ public class SpamInfo implements Commande{ } logger.debug("Timer for message deletion of "+user.getName()+" stated..."); threadHashMap.remove(user); - try { - Thread.sleep(15000); - logger.debug("Delete "+user.getName()+" messages"); - message.delete().queue(); - command.delete().queue(); - } catch (InterruptedException e) { - e.printStackTrace(); - } + List messages = new ArrayList<>(); + messages.add(command); + messages.add(message); + new MessageTimeOut(messages,15).start(); + diff --git a/src/net/Broken/Outils/AntiSpam.java b/src/net/Broken/Outils/AntiSpam.java index d0a8065..f1bd642 100644 --- a/src/net/Broken/Outils/AntiSpam.java +++ b/src/net/Broken/Outils/AntiSpam.java @@ -2,10 +2,7 @@ package net.Broken.Outils; import net.Broken.MainBot; import net.Broken.Commandes.Move; -import net.dv8tion.jda.core.entities.Guild; -import net.dv8tion.jda.core.entities.Member; -import net.dv8tion.jda.core.entities.Role; -import net.dv8tion.jda.core.entities.TextChannel; +import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.exceptions.HierarchyException; import net.dv8tion.jda.core.managers.GuildManager; @@ -13,6 +10,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.ArrayList; import java.util.List; import static java.lang.Thread.sleep; @@ -44,7 +42,9 @@ public class AntiSpam { // On créer un nouvelle case dans le tableau des statuts si il n'y est pas if(!MainBot.spamUtils.containsKey(user.getUser())) { - MainBot.spamUtils.put(user.getUser(),new UserSpamUtils(user)); + List messages = new ArrayList<>(); + messages.addAll(MainBot.historique.get(user.getUser())); + MainBot.spamUtils.put(user.getUser(),new UserSpamUtils(user,messages)); } // On verrifie que l'uttilisateur n'est pas deja en spam if(!MainBot.spamUtils.get(user.getUser()).isOnSpam()) @@ -68,7 +68,7 @@ public class AntiSpam { List spm = serveur.getRolesByName("Spammer", false); try{ move.exc(user, spm.get(0), true, serveur, serveurManger); - event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamExtermine(user,MainBot.spamUtils.get(user.getUser()).getMultip())).queue(); + MainBot.spamUtils.get(user.getUser()).addMessage(event.getTextChannel().sendMessage(EmbedMessageUtils.getSpamExtermine(user,MainBot.spamUtils.get(user.getUser()).getMultip())).complete()); 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){ @@ -132,7 +132,9 @@ public class AntiSpam { logger.error("Hierarchy error"); } logger.info("["+user.getEffectiveName()+"] Fin des "+multip+"min"); - chanel.sendMessage(EmbedMessageUtils.getSpamPardon(user)).queue(); + new MessageTimeOut(new ArrayList<>(MainBot.spamUtils.get(user.getUser()).getMessages()),0).start(); + MainBot.spamUtils.get(user.getUser()).clearAndAdd(chanel.sendMessage(EmbedMessageUtils.getSpamPardon(user)).complete()); + new MessageTimeOut(MainBot.spamUtils.get(user.getUser()).getMessages(),60).start(); // #-----------------------------------------------# diff --git a/src/net/Broken/Outils/MessageTimeOut.java b/src/net/Broken/Outils/MessageTimeOut.java new file mode 100644 index 0000000..4d1b466 --- /dev/null +++ b/src/net/Broken/Outils/MessageTimeOut.java @@ -0,0 +1,41 @@ +package net.Broken.Outils; + +import net.dv8tion.jda.core.entities.Message; +import net.dv8tion.jda.core.exceptions.ErrorResponseException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; + +public class MessageTimeOut extends Thread{ + List messages; + int second; + Logger logger = LogManager.getLogger(); + public MessageTimeOut(List messages, int second) { + this.messages = messages; + this.second = second; + } + + @Override + public void run() { + logger.debug("Timer for message deletion stated..."); + for(int i=0; i= nbMessage){ @@ -74,7 +76,6 @@ public class Moderateur { } } else { - logger.debug("ok else "); /******************************************** * SI c'est le 1er message de l'USER * ********************************************/ @@ -107,7 +108,7 @@ public class Moderateur { ArrayList histo = MainBot.historique.get(user.getUser()); for (Message aMessage:histo ) { - logger.debug("\t*"+aMessage.getContent()); +// logger.debug("\t*"+aMessage.getContent()); } } }else{ diff --git a/src/net/Broken/Outils/UserSpamUtils.java b/src/net/Broken/Outils/UserSpamUtils.java index b08b3ea..f99fb1c 100644 --- a/src/net/Broken/Outils/UserSpamUtils.java +++ b/src/net/Broken/Outils/UserSpamUtils.java @@ -1,12 +1,16 @@ package net.Broken.Outils; import net.dv8tion.jda.core.entities.Member; +import net.dv8tion.jda.core.entities.Message; + +import java.util.List; public class UserSpamUtils { private AntiSpam.Minuteur minuteur; private Member user; private int multip = 0; private boolean onSpam = false; + private List messages; public int getTimeLeft(){ return minuteur.timeLeft; @@ -17,8 +21,9 @@ public class UserSpamUtils { return multip; } - public UserSpamUtils(Member user) { + public UserSpamUtils(Member user, List messages) { this.user = user; + this.messages = messages; } @@ -45,4 +50,17 @@ public class UserSpamUtils { public Member getUser() { return user; } + + public void addMessage(Message message){ + messages.add(message); + } + + public List getMessages() { + return messages; + } + + public void clearAndAdd(Message message){ + messages.clear(); + messages.add(message); + } }