Some code cleaning

This commit is contained in:
BrokenFire 2018-02-16 15:02:53 +01:00
parent 3522e31481
commit a95aaa536f
10 changed files with 108 additions and 52 deletions

View File

@ -3,6 +3,7 @@ package net.Broken;
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.Moderateur;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.entities.Guild;
@ -87,7 +88,7 @@ public class BotListener extends ListenerAdapter {
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(MainBot.parser.parse(event.getMessage().getContent(), event));
MainBot.handleCommand(new CommandParser().parse(event.getMessage().getContent(), event));
}
else if (!event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId()))

View File

@ -1,7 +1,8 @@
package net.Broken;
import net.Broken.Tools.Command.CommandLoader;
import net.Broken.Tools.DayListener;
import net.Broken.Tools.DayListener.DayListener;
import net.Broken.Tools.DayListener.Listeners.ResetSpam;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
@ -79,8 +80,11 @@ public class Init {
MainBot.ModoTimer modotimer = new MainBot.ModoTimer();
modotimer.start();
DayListener dayListener = new DayListener();
DayListener dayListener = DayListener.getInstance();
dayListener.addListener(new ResetSpam());
dayListener.start();
logger.debug("-----------------FIN INITIALISATION-----------------");
jda.getPresence().setGame(Game.of("Statut: Ok!"));

View File

@ -28,13 +28,11 @@ import java.util.HashMap;
@Controller
public class MainBot {
public static final CommandParser parser =new CommandParser();
public static HashMap<String, Commande> commandes = new HashMap<>();
public static HashMap<User, ArrayList<Message>> historique =new HashMap<>();
public static HashMap<User, Integer> message_compteur =new HashMap<>();
public static boolean roleFlag = false;
public static HashMap<User, UserSpamUtils> spamUtils = new HashMap<>();
public static UserRegister userRegister = new UserRegister();
public static JDA jda;

View File

@ -12,6 +12,7 @@ import net.Broken.RestApi.Data.UserManager.CheckResposeData;
import net.Broken.RestApi.Data.UserManager.UserInfoData;
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
import net.Broken.Tools.UserManager.Exceptions.UserNotFoundException;
import net.Broken.Tools.UserManager.UserRegister;
import net.Broken.audio.NotConectedException;
import net.Broken.audio.NullMusicManager;
import net.dv8tion.jda.core.entities.VoiceChannel;
@ -41,6 +42,8 @@ public class MusicWebAPIController {
@Autowired
UserRepository userRepository;
UserRegister userRegister = UserRegister.getInstance();
@RequestMapping("/currentMusicInfo")
public CurrentMusicData getCurrentM(){
@ -82,7 +85,7 @@ public class MusicWebAPIController {
if(data.command != null) {
if(data.token != null) {
try {
UserEntity user = MainBot.userRegister.getUserWithApiToken(userRepository, data.token);
UserEntity user = userRegister.getUserWithApiToken(userRepository, data.token);
logger.info("receive command " + data.command + " from " + request.getRemoteAddr() + " USER: " + user.getName());
Music musicCommande = (Music) MainBot.commandes.get("music");

View File

@ -13,6 +13,7 @@ import net.Broken.Tools.UserManager.Exceptions.PasswordNotMatchException;
import net.Broken.Tools.UserManager.Exceptions.TokenNotMatch;
import net.Broken.Tools.UserManager.Exceptions.UserAlreadyRegistered;
import net.Broken.Tools.UserManager.Exceptions.UserNotFoundException;
import net.Broken.Tools.UserManager.UserRegister;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
@ -37,12 +38,14 @@ public class UserManagerAPIController {
@Autowired
private PasswordEncoder passwordEncoder;
UserRegister userRegister = UserRegister.getInstance();
@RequestMapping(value = "/preRegister", method = RequestMethod.POST)
public ResponseEntity<CheckResposeData> command(@RequestBody UserInfoData data){
if(data != null && data.name != null) {
try {
String id = MainBot.userRegister.sendCheckToken(pendingUserRepository, userRepository, passwordEncoder, data);
String id = userRegister.sendCheckToken(pendingUserRepository, userRepository, passwordEncoder, data);
return new ResponseEntity<>(new CheckResposeData(true, data.name, "Message sent", id), HttpStatus.OK);
} catch (UserNotFoundException e) {
logger.warn("User \"" + data.name + "\" not found!");
@ -63,8 +66,8 @@ public class UserManagerAPIController {
public ResponseEntity<UserConnectionData> confirAccount(@RequestBody ConfirmData data){
//TODO move pending user to accepted and return right things
try {
PendingUserEntity pUser = MainBot.userRegister.confirmCheckToken(pendingUserRepository, Integer.parseInt(data.id), data.checkToken);
UserEntity user = new UserEntity(pUser, MainBot.userRegister.generateApiToken());
PendingUserEntity pUser = userRegister.confirmCheckToken(pendingUserRepository, Integer.parseInt(data.id), data.checkToken);
UserEntity user = new UserEntity(pUser, userRegister.generateApiToken());
userRepository.save(user);
pendingUserRepository.delete(pUser);
@ -81,7 +84,7 @@ public class UserManagerAPIController {
@RequestMapping(value = "/requestToken", method = RequestMethod.POST)
public ResponseEntity<UserConnectionData> requestToken(@RequestBody UserInfoData data){
try {
UserEntity user = MainBot.userRegister.getUser(userRepository, passwordEncoder, data);
UserEntity user = userRegister.getUser(userRepository, passwordEncoder, data);
return new ResponseEntity<>(new UserConnectionData(true, user.getName(), user.getApiToken(), ""), HttpStatus.OK);
} catch (UserNotFoundException e) {

View File

@ -1,42 +0,0 @@
package net.Broken.Tools;
import net.Broken.Commands.Spam;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import org.apache.logging.log4j.LogManager;
import java.util.GregorianCalendar;
/**
* Created by seb65 on 09/11/2016.
*/
public class DayListener extends Thread {
GregorianCalendar calendrier;
int datePrecedente;
public DayListener() {
calendrier = new GregorianCalendar();
datePrecedente=0;
}
@Override
public void run() {
while(true)
{
if(calendrier.get(GregorianCalendar.DAY_OF_MONTH)!=datePrecedente)
{
Spam spam=new Spam();
String str[]={"all"};
datePrecedente=calendrier.get(GregorianCalendar.DAY_OF_MONTH);
try {
spam.reset(null,str);
} catch (RateLimitedException e) {
e.printStackTrace();
}
}
try {
sleep(7200000);
} catch (InterruptedException e) {
LogManager.getLogger().catching(e);
}
}
}
}

View File

@ -0,0 +1,53 @@
package net.Broken.Tools.DayListener;
import net.Broken.Commands.Spam;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import org.apache.logging.log4j.LogManager;
import java.util.ArrayList;
import java.util.GregorianCalendar;
/**
* Created by seb65 on 09/11/2016.
*/
public class DayListener extends Thread {
private GregorianCalendar calendar;
private int previousDay;
private ArrayList<NewDayListener> listeners = new ArrayList<>();
private DayListener() {
calendar = new GregorianCalendar();
previousDay = 0;
}
private static DayListener INSTANCE = new DayListener();
public static DayListener getInstance()
{
return INSTANCE;
}
public void addListener(NewDayListener listener){
listeners.add(listener);
}
@Override
public void run() {
while(true)
{
if(calendar.get(GregorianCalendar.DAY_OF_MONTH) != previousDay)
{
LogManager.getLogger().info("New day triggered!");
for(NewDayListener listener : listeners){
listener.onNewDay();
}
previousDay = calendar.get(GregorianCalendar.DAY_OF_MONTH);
}
try {
sleep(7200000);
} catch (InterruptedException e) {
LogManager.getLogger().catching(e);
}
}
}
}

View File

@ -0,0 +1,21 @@
package net.Broken.Tools.DayListener.Listeners;
import net.Broken.Commands.Spam;
import net.Broken.Tools.DayListener.NewDayListener;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import java.util.GregorianCalendar;
public class ResetSpam implements NewDayListener {
@Override
public void onNewDay() {
Spam spam = new Spam();
String str[] = {"all"};
try {
spam.reset(null,str);
} catch (RateLimitedException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,5 @@
package net.Broken.Tools.DayListener;
public interface NewDayListener {
void onNewDay();
}

View File

@ -24,7 +24,17 @@ import java.util.UUID;
public class UserRegister {
private Logger logger = LogManager.getLogger();
private static UserRegister INSTANCE = new UserRegister();
private UserRegister(){}
public static UserRegister getInstance(){
return INSTANCE;
}
public String sendCheckToken(PendingUserRepository pendingUserRepository, UserRepository userRepository, PasswordEncoder passwordEncoder, UserInfoData userInfo) throws UserNotFoundException, PasswordNotMatchException, UserAlreadyRegistered {