Merge branch 'devel'

This commit is contained in:
BrokenFire 2018-02-16 16:49:36 +01:00
commit a441af64d9
19 changed files with 293 additions and 77 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

@ -0,0 +1,67 @@
package net.Broken.Commands;
import net.Broken.Commande;
import net.Broken.MainBot;
import net.Broken.Tools.DayListener.DayListener;
import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.MessageTimeOut;
import net.Broken.Tools.PrivateMessage;
import net.dv8tion.jda.core.Permission;
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 org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
public class DayTrigger implements Commande{
Logger logger = LogManager.getLogger();
@Override
public boolean called(String[] args, MessageReceivedEvent event) {
return false;
}
@Override
public void action(String[] args, MessageReceivedEvent event) {
if(event.getMember().hasPermission(Permission.ADMINISTRATOR)){
DayListener.getInstance().trigger();
event.getMessage().delete().queue();
}
else{
logger.warn("Not admin! DENY!");
if(!event.isFromType(ChannelType.PRIVATE)){
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getError("Vous n'avez pas l'autorisation de faire ça!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).start();
}
else{
PrivateMessage.send(event.getAuthor(),EmbedMessageUtils.getError("Vous n'avez pas l'autorisation de faire ça!"),logger);
}
}
}
@Override
public String help(String[] args) {
return null;
}
@Override
public void executed(boolean success, MessageReceivedEvent event) {
}
@Override
public boolean isPrivateUsable() {
return true;
}
}

View File

@ -22,7 +22,8 @@ public class Madame implements Commande{
@Override
public void action(String[] args, MessageReceivedEvent event) {
if(event.getTextChannel().getName().equals("nsfw-over18")) {
if(event.getTextChannel().isNSFW()) {
Redirection redirect = new Redirection();
boolean success=false;
boolean error=false;

View File

@ -1,7 +1,9 @@
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.DailyMadame;
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 +81,12 @@ public class Init {
MainBot.ModoTimer modotimer = new MainBot.ModoTimer();
modotimer.start();
DayListener dayListener = new DayListener();
DayListener dayListener = DayListener.getInstance();
dayListener.addListener(new ResetSpam());
dayListener.addListener(new DailyMadame());
dayListener.start();
logger.debug("-----------------FIN INITIALISATION-----------------");
jda.getPresence().setGame(Game.of("Statut: Ok!"));

View File

@ -1,10 +1,10 @@
package net.Broken;
import net.Broken.RestApi.ApiCommandLoader;
import net.Broken.Tools.Command.CommandParser;
import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.UserManager.UserRegister;
import net.Broken.Tools.UserSpamUtils;
import net.Broken.RestApi.ApiCommandLoader;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.entities.Message;
@ -18,7 +18,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Controller;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
@ -29,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,57 @@
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 = calendar.get(GregorianCalendar.DAY_OF_MONTH);
}
private static DayListener INSTANCE = new DayListener();
public static DayListener getInstance()
{
return INSTANCE;
}
public void addListener(NewDayListener listener){
listeners.add(listener);
}
public void trigger(){
for(NewDayListener listener : listeners){
listener.onNewDay();
}
}
@Override
public void run() {
while(true)
{
if(calendar.get(GregorianCalendar.DAY_OF_MONTH) != previousDay)
{
LogManager.getLogger().info("New day triggered!");
trigger();
previousDay = calendar.get(GregorianCalendar.DAY_OF_MONTH);
}
try {
sleep(1800000);
} catch (InterruptedException e) {
LogManager.getLogger().catching(e);
}
}
}
}

View File

@ -0,0 +1,39 @@
package net.Broken.Tools.DayListener.Listeners;
import net.Broken.MainBot;
import net.Broken.Tools.DayListener.NewDayListener;
import net.Broken.Tools.Redirection;
import net.dv8tion.jda.core.entities.TextChannel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class DailyMadame implements NewDayListener{
Logger logger = LogManager.getLogger();
@Override
public void onNewDay() {
Redirection redirect = new Redirection();
boolean success=false;
boolean error=false;
int errorCp=0;
TextChannel chanel = MainBot.jda.getTextChannelsByName("nsfw-over18", true).get(0);
while(!success && !error)
{
try {
chanel.sendMessage("Le Daily Madame mes petits cochons :kissing_heart:\n" + redirect.get("http://dites.bonjourmadame.fr/random")).queue();
success=true;
} catch (IOException e) {
errorCp++;
logger.warn("Erreur de redirection. (Essais n°"+errorCp+")");
if(errorCp>5)
{
logger.error("5 Erreur de redirection.");
error=true;
}
}
}
}
}

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 {

View File

@ -0,0 +1,13 @@
package net.Broken.webView;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class GeneralWebView {
@RequestMapping("/")
public String music(Model model){
return "index";
}
}

View File

@ -0,0 +1,33 @@
package net.Broken.webView;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.ContentVersionStrategy;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
import org.springframework.web.servlet.resource.VersionResourceResolver;
@Configuration
public class MvcApplication extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
VersionResourceResolver versionResourceResolver = new VersionResourceResolver()
.addVersionStrategy(new ContentVersionStrategy(), "/**");
registry.addResourceHandler("/js/*.js")
.addResourceLocations("classpath:/static/js/")
.setCachePeriod(60 * 60 * 24 * 365) /* one year */
.resourceChain(true)
.addResolver(versionResourceResolver);
}
@Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
}

View File

@ -3,18 +3,19 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0"/>
<title>Discord Bot</title>
<title>Music Control - Discord Bot</title>
<link rel="icon"
type="image/x-icon"
href="favicon.png"/>
href="/favicon.png"/>
<!-- CSS -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link href="/css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link href="/css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
</head>
<body class="blue-grey lighten-5" >
<!--__________________________________________________________-->
<!-- NAV BAR -->
<!-- AND -->
@ -25,10 +26,10 @@
<a id="logo-container" href="/" class="brand-logo">Discord Bot</a>
<ul class="right hide-on-med-and-down">
<li class="active">
<li class="">
<a class="waves-effect waves-light sidenav-trigger" href="/" data-target="slide-out">Home</a>
</li>
<li>
<li class="active">
<a class="waves-effect waves-light" href="/music" >Music Control</a>
</li>
<li id="nav-bar-account">
@ -44,6 +45,8 @@
<li>
<a class="waves-effect waves-light sidenav-trigger" href="/music" data-target="slide-out" >Music Control</a>
</li>
<!--TODO Connection mobile-->
</ul>
<a href="#" data-activates="nav-mobile" class="button-navbar-mobile button-collapse"><i class="material-icons">menu</i></a>
</div>
@ -115,19 +118,17 @@
<!-- -->
<!--__________________________________________________________-->
<div class="section no-pad-bot main" id="index-banner">
</div>
<!-- Scripts-->
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="js/materialize.js"></script>
<script src="js/navabar.js"></script>
<script src="js/js.cookie.js"></script>
<!--<script src="js/init.js"></script>-->
<script th:src="@{/js/materialize.js}"></script>
<script th:src="@{/js/navabar.js}"></script>
<script th:src="@{/js/js.cookie.js}"></script>
</body>
</html>

View File

@ -290,13 +290,14 @@
<!-- Scripts-->
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="js/materialize.js"></script>
<script src="js/init.js"></script>
<script>
var needLogin = true;
</script>
<script src="js/navabar.js"></script>
<script src="js/js.cookie.js"></script>
<script th:src="@{/js/materialize.js}"></script>
<script th:src="@{/js/music.js}"></script>
<script th:src="@{/js/navabar.js}"></script>
<script th:src="@{/js/js.cookie.js}"></script>
</body>
</html>

View File

@ -192,10 +192,10 @@
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="js/materialize.js"></script>
<script src="js/register.js"></script>
<script src="js/navabar.js"></script>
<script src="js/js.cookie.js"></script>
<script th:src="@{/js/materialize.js}"></script>
<script th:src="@{/js/register.js}"></script>
<script th:src="@{/js/navabar.js}"></script>
<script th:src="@{/js/js.cookie.js}"></script>
<script th:inline="javascript">