2018-01-12 12:50:12 +01:00
package net.Broken.Commands ;
2017-03-20 20:27:53 +01:00
2017-09-05 02:13:27 +02:00
import net.Broken.Commande ;
import net.Broken.MainBot ;
2018-01-18 10:28:54 +01:00
import net.Broken.Tools.EmbedMessageUtils ;
import net.Broken.Tools.MessageTimeOut ;
import net.Broken.Tools.PrivateMessage ;
import net.Broken.Tools.UserSpamUtils ;
2017-09-26 21:58:04 +02:00
import net.dv8tion.jda.core.EmbedBuilder ;
2017-09-05 02:13:27 +02:00
import net.dv8tion.jda.core.entities.ChannelType ;
2017-09-26 21:58:04 +02:00
import net.dv8tion.jda.core.entities.Message ;
2017-09-05 02:13:27 +02:00
import net.dv8tion.jda.core.entities.User ;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent ;
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2017-09-26 21:58:04 +02:00
import java.awt.* ;
2017-10-04 12:14:21 +02:00
import java.util.ArrayList ;
2017-09-26 21:58:04 +02:00
import java.util.HashMap ;
2017-10-04 12:14:21 +02:00
import java.util.List ;
2017-09-05 02:13:27 +02:00
import java.util.concurrent.TimeUnit ;
2017-03-20 20:27:53 +01:00
/ * *
* Created by sebastien on 13 / 03 / 17 .
* /
2017-09-05 02:13:27 +02:00
public class SpamInfo implements Commande {
2017-09-26 21:58:04 +02:00
private HashMap < User , MessageUpdater > threadHashMap = new HashMap < > ( ) ;
2017-09-05 02:13:27 +02:00
Logger logger = LogManager . getLogger ( ) ;
2017-09-26 21:58:04 +02:00
private String HELP = " `//spaminfo <@utilisateur> ` \ n:arrow_right: \ t*Affiche les infos relatives aux punitions contre le spam de l'utilisateur mentionnée (de l'auteur si pas de mention)* " ;
2017-09-05 02:13:27 +02:00
@Override
public boolean called ( String [ ] args , MessageReceivedEvent event ) {
return false ;
}
@Override
public void action ( String [ ] args , MessageReceivedEvent event ) {
User user ;
if ( event . getMessage ( ) . getMentionedUsers ( ) . size ( ) = = 0 ) {
user = event . getAuthor ( ) ;
}
else {
user = event . getMessage ( ) . getMentionedUsers ( ) . get ( 0 ) ;
}
2017-09-26 21:58:04 +02:00
Message message = null ;
2017-09-05 02:13:27 +02:00
if ( ! MainBot . spamUtils . containsKey ( user ) ) {
if ( ! event . isFromType ( ChannelType . PRIVATE ) )
2017-10-04 10:45:45 +02:00
message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getSpamInfo ( user . getName ( ) + " : \ n \ t- Multiplicateur: `1` \ n \ t- En spam: `Non` " ) ) . complete ( ) ;
2017-09-05 02:13:27 +02:00
else
2017-09-26 21:58:04 +02:00
PrivateMessage . send ( event . getAuthor ( ) , EmbedMessageUtils . getSpamInfo ( user . getName ( ) + " : \ n \ t- Multiplicateur: `1` \ n \ t- En spam: `Non` " ) , logger ) ;
2017-09-05 02:13:27 +02:00
}
else {
UserSpamUtils util = MainBot . spamUtils . get ( user ) ;
if ( ! util . isOnSpam ( ) ) {
if ( ! event . isFromType ( ChannelType . PRIVATE ) )
2017-10-04 10:45:45 +02:00
message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getSpamInfo ( user . getName ( ) + " \ n \ t- Multiplicateur: ` " + util . getMultip ( ) + " ` \ n \ t- En spam: `Non` " ) ) . complete ( ) ;
2017-09-05 02:13:27 +02:00
else
2017-09-26 21:58:04 +02:00
PrivateMessage . send ( event . getAuthor ( ) , EmbedMessageUtils . getSpamInfo ( user . getName ( ) + " : \ n \ t- Multiplicateur: ` " + util . getMultip ( ) + " ` \ n \ t- En spam: `Non` " ) , logger ) ;
2017-09-05 02:13:27 +02:00
}
else {
if ( ! event . isFromType ( ChannelType . PRIVATE ) )
2017-09-26 21:58:04 +02:00
message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getSpamInfo ( user . getName ( ) + " : \ n \ t- Multiplicateur: ` " + util . getMultip ( ) + " ` \ n \ t- En spam: `Oui` \ n \ t- Temps restant: ` " + formatSecond ( util . getTimeLeft ( ) ) + " ` " ) ) . complete ( ) ;
else
message = PrivateMessage . send ( event . getAuthor ( ) , EmbedMessageUtils . getSpamInfo ( user . getName ( ) + " \ n \ t- Multiplicateur: ` " + util . getMultip ( ) + " ` \ n \ t- En spam: `Oui` \ n \ t- Temps restant: ` " + formatSecond ( util . getTimeLeft ( ) ) + " ` " ) , logger ) ;
}
}
if ( message ! = null ) {
if ( threadHashMap . containsKey ( user ) ) {
MessageUpdater startedThread = threadHashMap . get ( user ) ;
if ( ! message . getChannelType ( ) . equals ( startedThread . message . getChannelType ( ) ) ) {
2017-10-04 10:45:45 +02:00
MessageUpdater newThread = new MessageUpdater ( message , event . getMessage ( ) , MainBot . spamUtils . get ( user ) , user ) ;
2017-09-26 21:58:04 +02:00
threadHashMap . put ( user , newThread ) ;
newThread . start ( ) ;
}
2017-09-05 02:13:27 +02:00
else
2017-09-26 21:58:04 +02:00
{
threadHashMap . get ( user ) . stop = true ;
2017-10-04 10:45:45 +02:00
MessageUpdater newThread = new MessageUpdater ( message , event . getMessage ( ) , MainBot . spamUtils . get ( user ) , user ) ;
2017-09-26 21:58:04 +02:00
threadHashMap . replace ( user , newThread ) ;
newThread . start ( ) ;
2017-09-05 02:13:27 +02:00
}
2017-09-26 21:58:04 +02:00
}
else
{
2017-10-04 10:45:45 +02:00
MessageUpdater newThread = new MessageUpdater ( message , event . getMessage ( ) , MainBot . spamUtils . get ( user ) , user ) ;
2017-09-26 21:58:04 +02:00
threadHashMap . put ( user , newThread ) ;
newThread . start ( ) ;
}
2017-09-05 02:13:27 +02:00
}
2017-09-26 21:58:04 +02:00
2017-09-05 02:13:27 +02:00
}
@Override
public String help ( String [ ] args ) {
return HELP ;
}
@Override
public void executed ( boolean success , MessageReceivedEvent event ) {
}
@Override
public boolean isPrivateUsable ( ) {
return true ;
}
public String formatSecond ( int second ) {
long days = TimeUnit . SECONDS . toDays ( second ) ;
second - = TimeUnit . DAYS . toSeconds ( days ) ;
long hours = TimeUnit . SECONDS . toHours ( second ) ;
second - = TimeUnit . HOURS . toSeconds ( hours ) ;
long minutes = TimeUnit . SECONDS . toMinutes ( second ) ;
second - = TimeUnit . MINUTES . toSeconds ( minutes ) ;
long seconds = TimeUnit . SECONDS . toSeconds ( second ) ;
logger . debug ( " " + days + " : " + hours + " : " + minutes + " : " + seconds ) ;
String finalText = " " ;
if ( days ! = 0 )
finalText + = days + " jour(s) " ;
if ( hours ! = 0 )
finalText + = hours + " h " ;
if ( minutes ! = 0 )
finalText + = minutes + " min " ;
finalText + = seconds + " s " ;
return finalText ;
2017-03-20 20:27:53 +01:00
2017-09-05 02:13:27 +02:00
}
2017-09-26 21:58:04 +02:00
private class MessageUpdater extends Thread {
public Message message ;
2017-10-04 10:45:45 +02:00
public Message command ;
2017-09-26 21:58:04 +02:00
public UserSpamUtils util ;
public boolean stop ;
private int oldValue ;
private User user ;
2017-10-04 10:45:45 +02:00
public MessageUpdater ( Message message , Message command , UserSpamUtils util , User user ) {
2017-09-26 21:58:04 +02:00
this . message = message ;
this . util = util ;
this . user = user ;
2017-10-04 10:45:45 +02:00
this . command = command ;
2017-09-26 21:58:04 +02:00
}
@Override
public void run ( ) {
logger . debug ( " Start " + user . getName ( ) + " theard! " ) ;
2017-10-04 10:45:45 +02:00
if ( util ! = null ) {
oldValue = util . getTimeLeft ( ) ;
while ( util . getTimeLeft ( ) ! = 0 & & ! stop & & util . isOnSpam ( ) ) {
try {
Thread . sleep ( 500 ) ;
if ( util . getTimeLeft ( ) % 5 = = 0 & & oldValue - util . getTimeLeft ( ) > = 5 ) {
message . editMessage ( EmbedMessageUtils . getSpamInfo ( user . getName ( ) + " : \ n \ t- Multiplicateur: ` " + util . getMultip ( ) + " ` \ n \ t- En spam: `Oui` \ n \ t- Temps restant: ` " + formatSecond ( util . getTimeLeft ( ) ) + " ` " ) ) . complete ( ) ;
oldValue = util . getTimeLeft ( ) ;
}
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
2017-09-26 21:58:04 +02:00
}
}
2017-10-04 10:45:45 +02:00
logger . debug ( " Kill " + user . getName ( ) + " theard! " ) ;
if ( stop )
message . editMessage ( new EmbedBuilder ( ) . setColor ( Color . RED ) . setTitle ( " Aborted " ) . build ( ) ) . complete ( ) ;
else
message . editMessage ( EmbedMessageUtils . getSpamInfo ( user . getName ( ) + " \ n \ t- Multiplicateur: ` " + util . getMultip ( ) + " ` \ n \ t- En spam: `Non` " ) ) . complete ( ) ;
2017-09-26 21:58:04 +02:00
}
2017-10-04 10:45:45 +02:00
logger . debug ( " Timer for message deletion of " + user . getName ( ) + " stated... " ) ;
2017-09-26 21:58:04 +02:00
threadHashMap . remove ( user ) ;
2017-10-04 12:14:21 +02:00
List < Message > messages = new ArrayList < > ( ) ;
messages . add ( command ) ;
messages . add ( message ) ;
new MessageTimeOut ( messages , 15 ) . start ( ) ;
2017-10-04 10:45:45 +02:00
2017-09-26 21:58:04 +02:00
}
}
2017-03-20 20:27:53 +01:00
}