AntiSpam now delete spam message!

This commit is contained in:
Unknown 2017-10-04 12:14:21 +02:00
parent 00f0c04f37
commit b1f33149b9
7 changed files with 85 additions and 21 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -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<Message> messages = new ArrayList<>();
messages.add(command);
messages.add(message);
new MessageTimeOut(messages,15).start();

View File

@ -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<Message> 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<Role> 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();
// #-----------------------------------------------#

View File

@ -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<Message> messages;
int second;
Logger logger = LogManager.getLogger();
public MessageTimeOut(List<Message> 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<second; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(Message aMessage: messages)
{
try {
logger.debug(aMessage.getContent());
aMessage.delete().complete();
}catch (ErrorResponseException e){
logger.warn("Unknown Message");
}
}
}
}

View File

@ -32,12 +32,15 @@ public class Moderateur {
int nbMessage = 3;
int spam = 0;
if(MainBot.spamUtils.containsKey(user.getUser()) && MainBot.spamUtils.get(user.getUser()).isOnSpam()){
MainBot.spamUtils.get(user.getUser()).addMessage(event.getMessage());
}
/********************************************
* si l'USER a deja envoyé un message *
********************************************/
if(MainBot.historique.containsKey(user.getUser()))// Si le user a deja posté un message
{
logger.debug("ok");
/********************************************
* COPIE des infos d"historique" vers TOI[] *
********************************************/
@ -60,7 +63,6 @@ public class Moderateur {
if(aMessage.getContent().equals(oldMessage))
equalCont++;
oldMessage = aMessage.getContent();
logger.debug( "\t- "+aMessage.getContent());
}
if(equalCont >= 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<Message> histo = MainBot.historique.get(user.getUser());
for (Message aMessage:histo )
{
logger.debug("\t*"+aMessage.getContent());
// logger.debug("\t*"+aMessage.getContent());
}
}
}else{

View File

@ -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<Message> 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<Message> 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<Message> getMessages() {
return messages;
}
public void clearAndAdd(Message message){
messages.clear();
messages.add(message);
}
}