From 4898a51ee5f76cf9cfe9d1b124d5efc2372e1c9b Mon Sep 17 00:00:00 2001 From: BrokenFire Date: Fri, 12 Jan 2018 12:50:12 +0100 Subject: [PATCH] Change command loading method --- build.gradle | 9 ++ gradle/wrapper/gradle-wrapper.properties | 4 +- src/main/java/net/Broken/BotListener.java | 4 +- .../Broken/{Commandes => Commands}/Cat.java | 2 +- .../Broken/{Commandes => Commands}/Flush.java | 2 +- .../Broken/{Commandes => Commands}/Help.java | 7 +- .../Broken/{Commandes => Commands}/Move.java | 3 +- .../Broken/{Commandes => Commands}/Music.java | 6 +- .../{Commandes => Commands}/Over18/Ass.java | 4 +- .../{Commandes => Commands}/Over18/Boobs.java | 4 +- .../Over18/Madame.java | 2 +- .../{Commandes => Commands}/Over18/Pipe.java | 4 +- .../{Commandes => Commands}/Over18/SM.java | 4 +- .../{Commandes => Commands}/PingCommande.java | 13 +- .../Broken/{Commandes => Commands}/Spam.java | 2 +- .../{Commandes => Commands}/SpamInfo.java | 3 +- src/main/java/net/Broken/Init.java | 25 +--- src/main/java/net/Broken/MainBot.java | 13 +- src/main/java/net/Broken/Outils/AntiSpam.java | 5 +- .../Broken/Outils/Command/CommandLoader.java | 35 +++++ .../Outils/{ => Command}/CommandParser.java | 2 +- .../Command}/NumberedCommande.java | 2 +- .../java/net/Broken/Outils/DayListener.java | 2 +- .../net/Broken/RestApi/ApiCommandLoader.java | 36 +++++ .../net/Broken/RestApi/CommandInterface.java | 10 ++ .../java/net/Broken/RestApi/Commands/Add.java | 15 ++ .../net/Broken/RestApi/Commands/Flush.java | 22 +++ .../net/Broken/RestApi/Commands/Next.java | 22 +++ .../net/Broken/RestApi/Commands/Pause.java | 22 +++ .../net/Broken/RestApi/Commands/Play.java | 22 +++ .../net/Broken/RestApi/Commands/Stop.java | 18 +++ .../Broken/RestApi/MusicWebAPIController.java | 105 ++++++++++++++ .../Broken/RestApi/MusicWebController.java | 128 ------------------ .../java/net/Broken/audio/WebLoadUtils.java | 4 +- 34 files changed, 367 insertions(+), 194 deletions(-) rename src/main/java/net/Broken/{Commandes => Commands}/Cat.java (98%) rename src/main/java/net/Broken/{Commandes => Commands}/Flush.java (98%) rename src/main/java/net/Broken/{Commandes => Commands}/Help.java (90%) rename src/main/java/net/Broken/{Commandes => Commands}/Move.java (99%) rename src/main/java/net/Broken/{Commandes => Commands}/Music.java (98%) rename src/main/java/net/Broken/{Commandes => Commands}/Over18/Ass.java (82%) rename src/main/java/net/Broken/{Commandes => Commands}/Over18/Boobs.java (82%) rename src/main/java/net/Broken/{Commandes => Commands}/Over18/Madame.java (98%) rename src/main/java/net/Broken/{Commandes => Commands}/Over18/Pipe.java (95%) rename src/main/java/net/Broken/{Commandes => Commands}/Over18/SM.java (96%) rename src/main/java/net/Broken/{Commandes => Commands}/PingCommande.java (72%) rename src/main/java/net/Broken/{Commandes => Commands}/Spam.java (99%) rename src/main/java/net/Broken/{Commandes => Commands}/SpamInfo.java (99%) create mode 100644 src/main/java/net/Broken/Outils/Command/CommandLoader.java rename src/main/java/net/Broken/Outils/{ => Command}/CommandParser.java (98%) rename src/main/java/net/Broken/{Commandes => Outils/Command}/NumberedCommande.java (99%) create mode 100644 src/main/java/net/Broken/RestApi/ApiCommandLoader.java create mode 100644 src/main/java/net/Broken/RestApi/CommandInterface.java create mode 100644 src/main/java/net/Broken/RestApi/Commands/Add.java create mode 100644 src/main/java/net/Broken/RestApi/Commands/Flush.java create mode 100644 src/main/java/net/Broken/RestApi/Commands/Next.java create mode 100644 src/main/java/net/Broken/RestApi/Commands/Pause.java create mode 100644 src/main/java/net/Broken/RestApi/Commands/Play.java create mode 100644 src/main/java/net/Broken/RestApi/Commands/Stop.java create mode 100644 src/main/java/net/Broken/RestApi/MusicWebAPIController.java delete mode 100644 src/main/java/net/Broken/RestApi/MusicWebController.java diff --git a/build.gradle b/build.gradle index 333788a..c295e5e 100644 --- a/build.gradle +++ b/build.gradle @@ -34,6 +34,15 @@ dependencies { compile("com.sedmelluq:lavaplayer:1.2.45") compile 'net.dv8tion:JDA:3.3.1_303' compile group: 'org.json', name: 'json', version: '20160810' + // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) + compile("org.springframework.boot:spring-boot-starter-data-jpa") { + exclude group:"org.springframework.boot", module: "spring-boot-starter-logging" + } + // Use MySQL Connector-J + compile 'mysql:mysql-connector-java' + compile 'org.reflections:reflections:0.9.11' + + testCompile('org.springframework.boot:spring-boot-starter-test') testCompile('com.jayway.jsonpath:json-path') compile("org.springframework.boot:spring-boot-starter-thymeleaf") { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b0773a7..453f40a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sun Jan 07 23:09:03 CET 2018 +#Wed Jan 10 09:57:49 CET 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip diff --git a/src/main/java/net/Broken/BotListener.java b/src/main/java/net/Broken/BotListener.java index 4e3dc19..48f2afd 100644 --- a/src/main/java/net/Broken/BotListener.java +++ b/src/main/java/net/Broken/BotListener.java @@ -1,7 +1,7 @@ package net.Broken; -import net.Broken.Commandes.Move; -import net.Broken.Commandes.Music; +import net.Broken.Commands.Move; +import net.Broken.Commands.Music; import net.Broken.Outils.AntiSpam; import net.Broken.Outils.Moderateur; import net.dv8tion.jda.core.entities.ChannelType; diff --git a/src/main/java/net/Broken/Commandes/Cat.java b/src/main/java/net/Broken/Commands/Cat.java similarity index 98% rename from src/main/java/net/Broken/Commandes/Cat.java rename to src/main/java/net/Broken/Commands/Cat.java index 7208d18..d8edce5 100644 --- a/src/main/java/net/Broken/Commandes/Cat.java +++ b/src/main/java/net/Broken/Commands/Cat.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Commands; import net.Broken.Commande; diff --git a/src/main/java/net/Broken/Commandes/Flush.java b/src/main/java/net/Broken/Commands/Flush.java similarity index 98% rename from src/main/java/net/Broken/Commandes/Flush.java rename to src/main/java/net/Broken/Commands/Flush.java index 1744ccd..072554a 100644 --- a/src/main/java/net/Broken/Commandes/Flush.java +++ b/src/main/java/net/Broken/Commands/Flush.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Commands; import net.Broken.Commande; import net.Broken.Outils.EmbedMessageUtils; diff --git a/src/main/java/net/Broken/Commandes/Help.java b/src/main/java/net/Broken/Commands/Help.java similarity index 90% rename from src/main/java/net/Broken/Commandes/Help.java rename to src/main/java/net/Broken/Commands/Help.java index 3a7a66e..b29f8ea 100644 --- a/src/main/java/net/Broken/Commandes/Help.java +++ b/src/main/java/net/Broken/Commands/Help.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; @@ -9,7 +9,6 @@ import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; -import net.dv8tion.jda.core.exceptions.RateLimitedException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -84,7 +83,7 @@ public class Help implements Commande { } if(!event.isFromType(ChannelType.PRIVATE)){ - Message rest = event.getTextChannel().sendMessage(new EmbedBuilder().setTitle("Commandes envoyées par message privé").setColor(Color.green).build()).complete(); + Message rest = event.getTextChannel().sendMessage(new EmbedBuilder().setTitle("Command envoyées par message privé").setColor(Color.green).build()).complete(); List messages = new ArrayList(){{ add(rest); add(event.getMessage()); @@ -92,7 +91,7 @@ public class Help implements Commande { new MessageTimeOut(messages,MainBot.messageTimeOut).start(); } - PrivateMessage.send(event.getAuthor(),new EmbedBuilder().setTitle("Commandes du bot").setDescription(txt.toString()).setFooter("Utilise '//help ' pour plus de détails.",null).setColor(Color.green).setThumbnail(event.getJDA().getSelfUser().getAvatarUrl()).build(),logger); + PrivateMessage.send(event.getAuthor(),new EmbedBuilder().setTitle("Command du bot").setDescription(txt.toString()).setFooter("Utilise '//help ' pour plus de détails.",null).setColor(Color.green).setThumbnail(event.getJDA().getSelfUser().getAvatarUrl()).build(),logger); diff --git a/src/main/java/net/Broken/Commandes/Move.java b/src/main/java/net/Broken/Commands/Move.java similarity index 99% rename from src/main/java/net/Broken/Commandes/Move.java rename to src/main/java/net/Broken/Commands/Move.java index 32fa7c9..befab13 100644 --- a/src/main/java/net/Broken/Commandes/Move.java +++ b/src/main/java/net/Broken/Commands/Move.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; @@ -15,7 +15,6 @@ import org.apache.logging.log4j.Logger; import java.util.ArrayList; -import java.util.Collection; import java.util.List; /** diff --git a/src/main/java/net/Broken/Commandes/Music.java b/src/main/java/net/Broken/Commands/Music.java similarity index 98% rename from src/main/java/net/Broken/Commandes/Music.java rename to src/main/java/net/Broken/Commands/Music.java index 019367f..6db5a62 100644 --- a/src/main/java/net/Broken/Commandes/Music.java +++ b/src/main/java/net/Broken/Commands/Music.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; @@ -19,8 +19,8 @@ import java.util.List; public class Music implements Commande { public AudioM audio; Logger logger = LogManager.getLogger(); - public Music(Guild guild) { - audio = new AudioM(guild); + public Music() { + audio = new AudioM(MainBot.jda.getGuilds().get(0)); } @Override diff --git a/src/main/java/net/Broken/Commandes/Over18/Ass.java b/src/main/java/net/Broken/Commands/Over18/Ass.java similarity index 82% rename from src/main/java/net/Broken/Commandes/Over18/Ass.java rename to src/main/java/net/Broken/Commands/Over18/Ass.java index f88e8cb..eabf658 100644 --- a/src/main/java/net/Broken/Commandes/Over18/Ass.java +++ b/src/main/java/net/Broken/Commands/Over18/Ass.java @@ -1,6 +1,6 @@ -package net.Broken.Commandes.Over18; +package net.Broken.Commands.Over18; -import net.Broken.Commandes.NumberedCommande; +import net.Broken.Outils.Command.NumberedCommande; import org.apache.logging.log4j.LogManager; /** diff --git a/src/main/java/net/Broken/Commandes/Over18/Boobs.java b/src/main/java/net/Broken/Commands/Over18/Boobs.java similarity index 82% rename from src/main/java/net/Broken/Commandes/Over18/Boobs.java rename to src/main/java/net/Broken/Commands/Over18/Boobs.java index cc47a9b..c7bf203 100644 --- a/src/main/java/net/Broken/Commandes/Over18/Boobs.java +++ b/src/main/java/net/Broken/Commands/Over18/Boobs.java @@ -1,6 +1,6 @@ -package net.Broken.Commandes.Over18; +package net.Broken.Commands.Over18; -import net.Broken.Commandes.NumberedCommande; +import net.Broken.Outils.Command.NumberedCommande; import org.apache.logging.log4j.LogManager; /** diff --git a/src/main/java/net/Broken/Commandes/Over18/Madame.java b/src/main/java/net/Broken/Commands/Over18/Madame.java similarity index 98% rename from src/main/java/net/Broken/Commandes/Over18/Madame.java rename to src/main/java/net/Broken/Commands/Over18/Madame.java index 145dfb5..4e0f5c9 100644 --- a/src/main/java/net/Broken/Commandes/Over18/Madame.java +++ b/src/main/java/net/Broken/Commands/Over18/Madame.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes.Over18; +package net.Broken.Commands.Over18; import net.Broken.Commande; import net.Broken.Outils.Redirection; diff --git a/src/main/java/net/Broken/Commandes/Over18/Pipe.java b/src/main/java/net/Broken/Commands/Over18/Pipe.java similarity index 95% rename from src/main/java/net/Broken/Commandes/Over18/Pipe.java rename to src/main/java/net/Broken/Commands/Over18/Pipe.java index bb84d2f..c83b5bd 100644 --- a/src/main/java/net/Broken/Commandes/Over18/Pipe.java +++ b/src/main/java/net/Broken/Commands/Over18/Pipe.java @@ -1,6 +1,6 @@ -package net.Broken.Commandes.Over18; +package net.Broken.Commands.Over18; -import net.Broken.Commandes.NumberedCommande; +import net.Broken.Outils.Command.NumberedCommande; import org.apache.logging.log4j.LogManager; /** diff --git a/src/main/java/net/Broken/Commandes/Over18/SM.java b/src/main/java/net/Broken/Commands/Over18/SM.java similarity index 96% rename from src/main/java/net/Broken/Commandes/Over18/SM.java rename to src/main/java/net/Broken/Commands/Over18/SM.java index 6c7f39c..44fac7c 100644 --- a/src/main/java/net/Broken/Commandes/Over18/SM.java +++ b/src/main/java/net/Broken/Commands/Over18/SM.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes.Over18; +package net.Broken.Commands.Over18; import net.Broken.Commande; import net.Broken.Outils.Redirection; @@ -6,8 +6,6 @@ import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.io.IOException; - /** * Created by seb65 on 10/11/2016. */ diff --git a/src/main/java/net/Broken/Commandes/PingCommande.java b/src/main/java/net/Broken/Commands/PingCommande.java similarity index 72% rename from src/main/java/net/Broken/Commandes/PingCommande.java rename to src/main/java/net/Broken/Commands/PingCommande.java index c4b502a..fdbec8a 100644 --- a/src/main/java/net/Broken/Commandes/PingCommande.java +++ b/src/main/java/net/Broken/Commands/PingCommande.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; @@ -8,8 +8,6 @@ import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; -import java.sql.Timestamp; -import java.time.*; import java.util.ArrayList; import java.util.List; @@ -20,7 +18,7 @@ import java.util.List; public class PingCommande implements Commande { - private String HELP = "`//ping` \n :arrow_right:\t*Le bot vous répondra Pong!*"; + private String HELP = "`//ping` \n :arrow_right:\t*Renvoi le ping du bot*"; @Override public boolean called(String[] args, MessageReceivedEvent event) { return true; @@ -28,11 +26,12 @@ public class PingCommande implements Commande { @Override public void action(String[] args, MessageReceivedEvent event) { - long receivedTime = Timestamp.valueOf(LocalDateTime.ofInstant(event.getMessage().getCreationTime().toInstant(), ZoneId.systemDefault())).getTime(); + long ping = event.getJDA().getPing(); +// long receivedTime = Timestamp.valueOf(LocalDateTime.ofInstant(event.getMessage().getCreationTime().toInstant(), ZoneId.systemDefault())).getTime(); if(event.isFromType(ChannelType.PRIVATE)) - event.getPrivateChannel().sendMessage(":arrow_right: Pong! `"+((Timestamp.from(Instant.now()).getTime()-receivedTime))+"ms`").queue(); + event.getPrivateChannel().sendMessage(":arrow_right: Pong! `" + ping+ "ms`").queue(); else { - Message rest = event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: Pong! `"+((Timestamp.from(Instant.now()).getTime()-receivedTime))+"ms`").complete(); + Message rest = event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: Pong! `" + ping + "ms`").complete(); List messages = new ArrayList(){{ add(rest); add(event.getMessage()); diff --git a/src/main/java/net/Broken/Commandes/Spam.java b/src/main/java/net/Broken/Commands/Spam.java similarity index 99% rename from src/main/java/net/Broken/Commandes/Spam.java rename to src/main/java/net/Broken/Commands/Spam.java index 73cdc84..e0f1c4a 100644 --- a/src/main/java/net/Broken/Commandes/Spam.java +++ b/src/main/java/net/Broken/Commands/Spam.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; diff --git a/src/main/java/net/Broken/Commandes/SpamInfo.java b/src/main/java/net/Broken/Commands/SpamInfo.java similarity index 99% rename from src/main/java/net/Broken/Commandes/SpamInfo.java rename to src/main/java/net/Broken/Commands/SpamInfo.java index ef9c6ea..1f64797 100644 --- a/src/main/java/net/Broken/Commandes/SpamInfo.java +++ b/src/main/java/net/Broken/Commands/SpamInfo.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; @@ -8,7 +8,6 @@ import net.Broken.Outils.PrivateMessage; import net.Broken.Outils.UserSpamUtils; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.ChannelType; -import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; diff --git a/src/main/java/net/Broken/Init.java b/src/main/java/net/Broken/Init.java index cc80bd6..58eaa2b 100644 --- a/src/main/java/net/Broken/Init.java +++ b/src/main/java/net/Broken/Init.java @@ -1,7 +1,6 @@ package net.Broken; -import net.Broken.Commandes.*; -import net.Broken.Commandes.Over18.*; +import net.Broken.Outils.Command.CommandLoader; import net.Broken.Outils.DayListener; import net.dv8tion.jda.core.AccountType; import net.dv8tion.jda.core.JDA; @@ -11,7 +10,6 @@ import net.dv8tion.jda.core.entities.Game; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.exceptions.RateLimitedException; -import net.dv8tion.jda.core.managers.GuildManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -40,6 +38,7 @@ public class Init { logger.info("Connection au serveur..."); //connection au bot jda = new JDABuilder(AccountType.BOT).addEventListener(new BotListener()).setToken(token).setBulkDeleteSplittingEnabled(false).buildBlocking(); + MainBot.jda = jda; jda.setAutoReconnect(true); jda.addEventListener(); @@ -48,25 +47,7 @@ public class Init { *************************************/ jda.getPresence().setGame(Game.of("Statut: Loading...")); jda.getTextChannels().forEach(textChannel -> textChannel.sendTyping().queue()); - MainBot.commandes.put("ping", new PingCommande()); - MainBot.commandes.put("help", new Help()); - MainBot.commandes.put("move", new Move()); - MainBot.commandes.put("spam", new Spam()); - MainBot.commandes.put("spaminfo", new SpamInfo()); - MainBot.commandes.put("flush", new Flush()); - MainBot.commandes.put("music", new Music(jda.getGuilds().get(0))); - - if (!dev) { - MainBot.commandes.put("ass", new Ass()); - jda.getTextChannels().forEach(textChannel -> textChannel.sendTyping().queue()); - MainBot.commandes.put("boobs", new Boobs()); - jda.getTextChannels().forEach(textChannel -> textChannel.sendTyping().queue()); - MainBot.commandes.put("pipe", new Pipe()); - jda.getTextChannels().forEach(textChannel -> textChannel.sendTyping().queue()); - MainBot.commandes.put("sm", new SM()); - MainBot.commandes.put("madame", new Madame()); - MainBot.commandes.put("cat", new Cat()); - } + CommandLoader.load(); //On recupere le l'id serveur diff --git a/src/main/java/net/Broken/MainBot.java b/src/main/java/net/Broken/MainBot.java index 23fa64b..b047837 100644 --- a/src/main/java/net/Broken/MainBot.java +++ b/src/main/java/net/Broken/MainBot.java @@ -1,8 +1,9 @@ package net.Broken; -import net.Broken.Outils.CommandParser; +import net.Broken.Outils.Command.CommandParser; import net.Broken.Outils.EmbedMessageUtils; import net.Broken.Outils.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; @@ -32,6 +33,9 @@ public class MainBot { public static HashMap message_compteur =new HashMap<>(); public static boolean roleFlag = false; public static HashMap spamUtils = new HashMap<>(); + public static JDA jda; + + public static int messageTimeOut = 10; @@ -61,7 +65,7 @@ public class MainBot { i++; } - JDA jda = Init.initBot(token, dev); + jda = Init.initBot(token, dev); ConfigurableApplicationContext ctx = SpringApplication.run(MainBot.class, args); if(jda == null) { System.exit(SpringApplication.exit(ctx, (ExitCodeGenerator) () -> { @@ -70,6 +74,11 @@ public class MainBot { })); } + ApiCommandLoader.load(); + + + + diff --git a/src/main/java/net/Broken/Outils/AntiSpam.java b/src/main/java/net/Broken/Outils/AntiSpam.java index 4d45d1d..a8d89d4 100644 --- a/src/main/java/net/Broken/Outils/AntiSpam.java +++ b/src/main/java/net/Broken/Outils/AntiSpam.java @@ -1,7 +1,10 @@ package net.Broken.Outils; import net.Broken.MainBot; -import net.Broken.Commandes.Move; +import net.Broken.Commands.Move; +import net.Broken.Outils.EmbedMessageUtils; +import net.Broken.Outils.MessageTimeOut; +import net.Broken.Outils.UserSpamUtils; import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.exceptions.HierarchyException; diff --git a/src/main/java/net/Broken/Outils/Command/CommandLoader.java b/src/main/java/net/Broken/Outils/Command/CommandLoader.java new file mode 100644 index 0000000..3ae8b3f --- /dev/null +++ b/src/main/java/net/Broken/Outils/Command/CommandLoader.java @@ -0,0 +1,35 @@ +package net.Broken.Outils.Command; + +import net.Broken.Commande; +import net.Broken.MainBot; +import net.Broken.RestApi.CommandInterface; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.reflections.Reflections; + +import java.util.Set; + +public class CommandLoader { + private static Logger logger = LogManager.getLogger(); + public static void load(){ + logger.info("Loading Command..."); + Reflections reflections = new Reflections("net.Broken.Command"); + Set> modules = reflections.getSubTypesOf(Commande.class); + + logger.info("Find " + modules.size() + " Command:"); + for (Class command : modules) { + + String reference = command.getName(); + String[] splited = reference.split("\\."); + String name = splited[splited.length-1].toLowerCase(); + + logger.info("..." + name); + try { + MainBot.commandes.put(name, command.newInstance()); + } catch (InstantiationException | IllegalAccessException e) { + logger.error("Failed to load " + name + "!"); + } + + } + } +} diff --git a/src/main/java/net/Broken/Outils/CommandParser.java b/src/main/java/net/Broken/Outils/Command/CommandParser.java similarity index 98% rename from src/main/java/net/Broken/Outils/CommandParser.java rename to src/main/java/net/Broken/Outils/Command/CommandParser.java index 00fd97c..632070a 100644 --- a/src/main/java/net/Broken/Outils/CommandParser.java +++ b/src/main/java/net/Broken/Outils/Command/CommandParser.java @@ -1,4 +1,4 @@ -package net.Broken.Outils; +package net.Broken.Outils.Command; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; diff --git a/src/main/java/net/Broken/Commandes/NumberedCommande.java b/src/main/java/net/Broken/Outils/Command/NumberedCommande.java similarity index 99% rename from src/main/java/net/Broken/Commandes/NumberedCommande.java rename to src/main/java/net/Broken/Outils/Command/NumberedCommande.java index 3e206c1..e3900da 100644 --- a/src/main/java/net/Broken/Commandes/NumberedCommande.java +++ b/src/main/java/net/Broken/Outils/Command/NumberedCommande.java @@ -1,4 +1,4 @@ -package net.Broken.Commandes; +package net.Broken.Outils.Command; import net.Broken.Commande; import net.Broken.Outils.FindContentOnWebPage; import net.Broken.Outils.LimitChecker; diff --git a/src/main/java/net/Broken/Outils/DayListener.java b/src/main/java/net/Broken/Outils/DayListener.java index cfa0d57..7f4edf1 100644 --- a/src/main/java/net/Broken/Outils/DayListener.java +++ b/src/main/java/net/Broken/Outils/DayListener.java @@ -1,5 +1,5 @@ package net.Broken.Outils; -import net.Broken.Commandes.Spam; +import net.Broken.Commands.Spam; import net.dv8tion.jda.core.exceptions.RateLimitedException; import org.apache.logging.log4j.LogManager; diff --git a/src/main/java/net/Broken/RestApi/ApiCommandLoader.java b/src/main/java/net/Broken/RestApi/ApiCommandLoader.java new file mode 100644 index 0000000..6b36ed0 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/ApiCommandLoader.java @@ -0,0 +1,36 @@ +package net.Broken.RestApi; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.reflections.Reflections; + +import java.util.HashMap; +import java.util.Set; + +public class ApiCommandLoader { + public static HashMap apiCommands = new HashMap<>(); + private static Logger logger = LogManager.getLogger(); + public static void load(){ + logger.info("Loading Api Command..."); + Reflections reflections = new Reflections("net.Broken.RestApi.Command"); + Set> modules = + reflections.getSubTypesOf(CommandInterface.class); + logger.info("Find " + modules.size() + " Command:"); + for (Class apiClass : modules) { + + String reference = apiClass.getName(); + String[] splited = reference.split("\\."); + String name = splited[splited.length-1].toUpperCase(); + + logger.info("..." + name); + + try { + apiCommands.put(name, apiClass.newInstance()); + } catch (InstantiationException | IllegalAccessException e) { + logger.error("Failed to load " + name + "!"); + } + + } + } + +} diff --git a/src/main/java/net/Broken/RestApi/CommandInterface.java b/src/main/java/net/Broken/RestApi/CommandInterface.java new file mode 100644 index 0000000..6e83427 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/CommandInterface.java @@ -0,0 +1,10 @@ +package net.Broken.RestApi; + +import net.Broken.Commands.Music; +import net.Broken.RestApi.Data.CommandPostData; +import net.Broken.RestApi.Data.CommandResponseData; +import org.springframework.http.ResponseEntity; + +public interface CommandInterface { + ResponseEntity action(Music musicCommande, CommandPostData data); +} diff --git a/src/main/java/net/Broken/RestApi/Commands/Add.java b/src/main/java/net/Broken/RestApi/Commands/Add.java new file mode 100644 index 0000000..ae36e38 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Commands/Add.java @@ -0,0 +1,15 @@ +package net.Broken.RestApi.Commands; + +import net.Broken.Commands.Music; +import net.Broken.RestApi.CommandInterface; +import net.Broken.RestApi.Data.CommandPostData; +import net.Broken.RestApi.Data.CommandResponseData; +import net.Broken.audio.WebLoadUtils; +import org.springframework.http.ResponseEntity; + +public class Add implements CommandInterface { + @Override + public ResponseEntity action(Music musicCommande, CommandPostData data) { + return new WebLoadUtils(musicCommande ,data).getResponse(); + } +} diff --git a/src/main/java/net/Broken/RestApi/Commands/Flush.java b/src/main/java/net/Broken/RestApi/Commands/Flush.java new file mode 100644 index 0000000..25c598f --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Commands/Flush.java @@ -0,0 +1,22 @@ +package net.Broken.RestApi.Commands; + +import net.Broken.Commands.Music; +import net.Broken.RestApi.CommandInterface; +import net.Broken.RestApi.Data.CommandPostData; +import net.Broken.RestApi.Data.CommandResponseData; +import net.Broken.audio.NotConectedException; +import net.Broken.audio.NullMusicManager; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public class Flush implements CommandInterface { + @Override + public ResponseEntity action(Music musicCommande, CommandPostData data) { + try { + musicCommande.getAudioManager().getMusicManager().scheduler.flush(); + return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); + } catch (NullMusicManager | NotConectedException nullMusicManager) { + return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE); + } + } +} diff --git a/src/main/java/net/Broken/RestApi/Commands/Next.java b/src/main/java/net/Broken/RestApi/Commands/Next.java new file mode 100644 index 0000000..2338649 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Commands/Next.java @@ -0,0 +1,22 @@ +package net.Broken.RestApi.Commands; + +import net.Broken.Commands.Music; +import net.Broken.RestApi.CommandInterface; +import net.Broken.RestApi.Data.CommandPostData; +import net.Broken.RestApi.Data.CommandResponseData; +import net.Broken.audio.NotConectedException; +import net.Broken.audio.NullMusicManager; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public class Next implements CommandInterface { + @Override + public ResponseEntity action(Music musicCommande, CommandPostData data) { + try { + musicCommande.getAudioManager().getMusicManager().scheduler.nextTrack(); + return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); + } catch (NullMusicManager | NotConectedException nullMusicManager) { + return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE); + } + } +} diff --git a/src/main/java/net/Broken/RestApi/Commands/Pause.java b/src/main/java/net/Broken/RestApi/Commands/Pause.java new file mode 100644 index 0000000..89a1c91 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Commands/Pause.java @@ -0,0 +1,22 @@ +package net.Broken.RestApi.Commands; + +import net.Broken.Commands.Music; +import net.Broken.RestApi.CommandInterface; +import net.Broken.RestApi.Data.CommandPostData; +import net.Broken.RestApi.Data.CommandResponseData; +import net.Broken.audio.NotConectedException; +import net.Broken.audio.NullMusicManager; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public class Pause implements CommandInterface { + @Override + public ResponseEntity action(Music musicCommande, CommandPostData data) { + try { + musicCommande.getAudioManager().getMusicManager().scheduler.pause(); + return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); + } catch (NullMusicManager | NotConectedException nullMusicManager) { + return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE); + } + } +} diff --git a/src/main/java/net/Broken/RestApi/Commands/Play.java b/src/main/java/net/Broken/RestApi/Commands/Play.java new file mode 100644 index 0000000..9a03f94 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Commands/Play.java @@ -0,0 +1,22 @@ +package net.Broken.RestApi.Commands; + +import net.Broken.Commands.Music; +import net.Broken.RestApi.CommandInterface; +import net.Broken.RestApi.Data.CommandPostData; +import net.Broken.RestApi.Data.CommandResponseData; +import net.Broken.audio.NotConectedException; +import net.Broken.audio.NullMusicManager; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public class Play implements CommandInterface { + @Override + public ResponseEntity action(Music musicCommande, CommandPostData data) { + try { + musicCommande.getAudioManager().getMusicManager().scheduler.resume(); + return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); + } catch (NullMusicManager | NotConectedException nullMusicManager) { + return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE); + } + } +} diff --git a/src/main/java/net/Broken/RestApi/Commands/Stop.java b/src/main/java/net/Broken/RestApi/Commands/Stop.java new file mode 100644 index 0000000..7e5958c --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Commands/Stop.java @@ -0,0 +1,18 @@ +package net.Broken.RestApi.Commands; + +import net.Broken.Commands.Music; +import net.Broken.RestApi.CommandInterface; +import net.Broken.RestApi.Data.CommandPostData; +import net.Broken.RestApi.Data.CommandResponseData; +import net.dv8tion.jda.core.events.message.MessageReceivedEvent; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public class Stop implements CommandInterface { + @Override + public ResponseEntity action(Music musicCommande, CommandPostData data) { + musicCommande.getAudioManager().stop((MessageReceivedEvent) null); + return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); + + } +} diff --git a/src/main/java/net/Broken/RestApi/MusicWebAPIController.java b/src/main/java/net/Broken/RestApi/MusicWebAPIController.java new file mode 100644 index 0000000..ae6dea2 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/MusicWebAPIController.java @@ -0,0 +1,105 @@ +package net.Broken.RestApi; + +import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; +import com.sedmelluq.discord.lavaplayer.track.AudioTrack; +import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; +import net.Broken.Commands.Music; +import net.Broken.DB.SavedPlaylistRepository; +import net.Broken.MainBot; +import net.Broken.RestApi.Data.CommandPostData; +import net.Broken.RestApi.Data.CommandResponseData; +import net.Broken.RestApi.Data.CurrentMusicData; +import net.Broken.RestApi.Data.PlaylistData; +import net.Broken.audio.NotConectedException; +import net.Broken.audio.NullMusicManager; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/music/") +public class MusicWebAPIController { + Logger logger = LogManager.getLogger(); + @Autowired + public SavedPlaylistRepository savedPlaylist; + + @RequestMapping("/currentMusicInfo") + public CurrentMusicData getCurrentM(){ + Music musicCommande = (Music) MainBot.commandes.get("music"); + + if(musicCommande.audio.getGuild().getAudioManager().isConnected()){ + try { + AudioPlayer player = musicCommande.audio.getMusicManager().player; + AudioTrack currentTrack = player.getPlayingTrack(); + if(currentTrack == null) + { + return new CurrentMusicData(null,0, "STOP",false); + } + return new CurrentMusicData(currentTrack.getInfo(),currentTrack.getPosition(), currentTrack.getState().toString(), player.isPaused()); + } catch (NullMusicManager | NotConectedException nullMusicManager) { + return new CurrentMusicData(null,0, "STOP",false); + } + }else + { + return new CurrentMusicData(null,0, "DISCONNECTED",false); + } + } + + @RequestMapping("/getPlaylist") + public PlaylistData getPlaylist(){ + Music musicCommande = (Music) MainBot.commandes.get("music"); + List list = null; + try { + list = musicCommande.getAudioManager().getMusicManager().scheduler.getList(); + return new PlaylistData(list); + } catch (NullMusicManager | NotConectedException nullMusicManager) { + return new PlaylistData(list); + } + } + + @RequestMapping(value = "/command", method = RequestMethod.POST) + public ResponseEntity command(@RequestBody CommandPostData data){ + + if(data.command != null) { + logger.info("receive command: " + data.command); + Music musicCommande = (Music) MainBot.commandes.get("music"); + + if(ApiCommandLoader.apiCommands.containsKey(data.command)) + return ApiCommandLoader.apiCommands.get(data.command).action(musicCommande,data); + else + return new ResponseEntity<>(new CommandResponseData(data.command,"Unknown Command"), HttpStatus.BAD_REQUEST); + + } + else + logger.info("Null"); + return new ResponseEntity<>(new CommandResponseData(null, null), HttpStatus.NO_CONTENT); + } + + + + +// DB Test Ignore it + +// @RequestMapping(value = "/test", method = RequestMethod.GET) +// public ResponseEntity test(){ +// SavedPlaylistEntity savedPlaylistEntity = new SavedPlaylistEntity(); +// savedPlaylistEntity.setAutorName("Test"); +// savedPlaylistEntity.setName("Playlist de test"); +// savedPlaylist.save(savedPlaylistEntity); +// logger.info(savedPlaylistEntity); +// return new ResponseEntity("OK",HttpStatus.OK); +// } +// @GetMapping(path="/all") +// public @ResponseBody Iterable getAllUsers() { +// // This returns a JSON or XML with the users +// return savedPlaylist.findAll(); +// } + + +} + diff --git a/src/main/java/net/Broken/RestApi/MusicWebController.java b/src/main/java/net/Broken/RestApi/MusicWebController.java deleted file mode 100644 index 91ce83e..0000000 --- a/src/main/java/net/Broken/RestApi/MusicWebController.java +++ /dev/null @@ -1,128 +0,0 @@ -package net.Broken.RestApi; - -import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler; -import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; -import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; -import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; -import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; -import com.sedmelluq.discord.lavaplayer.track.AudioTrack; -import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; -import net.Broken.Commandes.Music; -import net.Broken.MainBot; -import net.Broken.RestApi.Data.CommandPostData; -import net.Broken.RestApi.Data.CommandResponseData; -import net.Broken.RestApi.Data.CurrentMusicData; -import net.Broken.RestApi.Data.PlaylistData; -import net.Broken.audio.AudioM; -import net.Broken.audio.NotConectedException; -import net.Broken.audio.NullMusicManager; -import net.Broken.audio.WebLoadUtils; -import net.dv8tion.jda.core.events.message.MessageReceivedEvent; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import java.util.List; - -@RestController -@RequestMapping("/api/music/") -public class MusicWebController { - Logger logger = LogManager.getLogger(); - - @RequestMapping("/currentMusicInfo") - public CurrentMusicData test(){ - Music musicCommande = (Music) MainBot.commandes.get("music"); - - if(musicCommande.audio.getGuild().getAudioManager().isConnected()){ - try { - AudioPlayer player = musicCommande.audio.getMusicManager().player; - AudioTrack currentTrack = player.getPlayingTrack(); - if(currentTrack == null) - { - return new CurrentMusicData(null,0, "STOP",false); - } - return new CurrentMusicData(currentTrack.getInfo(),currentTrack.getPosition(), currentTrack.getState().toString(), player.isPaused()); - } catch (NullMusicManager | NotConectedException nullMusicManager) { - return new CurrentMusicData(null,0, "STOP",false); - } - }else - { - return new CurrentMusicData(null,0, "DISCONNECTED",false); - } - } - - @RequestMapping("/getPlaylist") - public PlaylistData getPlaylist(){ - Music musicCommande = (Music) MainBot.commandes.get("music"); - List list = null; - try { - list = musicCommande.getAudioManager().getMusicManager().scheduler.getList(); - return new PlaylistData(list); - } catch (NullMusicManager | NotConectedException nullMusicManager) { - return new PlaylistData(list); - } - } - - @RequestMapping(value = "/command", method = RequestMethod.POST) - public ResponseEntity command(@RequestBody CommandPostData data){ - - if(data.command != null) { - logger.info("receive command: " + data.command); - Music musicCommande = (Music) MainBot.commandes.get("music"); - switch (data.command) { - case "PLAY": - try { - musicCommande.getAudioManager().getMusicManager().scheduler.resume(); - return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); - } catch (NullMusicManager | NotConectedException nullMusicManager) { - return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE); - } - - case "PAUSE": - try { - musicCommande.getAudioManager().getMusicManager().scheduler.pause(); - return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); - } catch (NullMusicManager | NotConectedException nullMusicManager) { - return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE); - } - - case "NEXT": - try { - musicCommande.getAudioManager().getMusicManager().scheduler.nextTrack(); - return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); - } catch (NullMusicManager | NotConectedException nullMusicManager) { - return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE); - } - - case "STOP": - musicCommande.getAudioManager().stop((MessageReceivedEvent) null); - return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); - - case "ADD": - return new WebLoadUtils(musicCommande ,data).getResponse(); - - case "FLUSH": - try { - musicCommande.getAudioManager().getMusicManager().scheduler.flush(); - return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); - } catch (NullMusicManager | NotConectedException nullMusicManager) { - return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE); - } - - - } - } - else - logger.info("Null"); - return new ResponseEntity<>(new CommandResponseData(null, null), HttpStatus.NO_CONTENT); - } - - - -} - diff --git a/src/main/java/net/Broken/audio/WebLoadUtils.java b/src/main/java/net/Broken/audio/WebLoadUtils.java index 7974e78..dd9c56c 100644 --- a/src/main/java/net/Broken/audio/WebLoadUtils.java +++ b/src/main/java/net/Broken/audio/WebLoadUtils.java @@ -5,7 +5,7 @@ import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; -import net.Broken.Commandes.Music; +import net.Broken.Commands.Music; import net.Broken.RestApi.Data.CommandPostData; import net.Broken.RestApi.Data.CommandResponseData; import org.apache.logging.log4j.LogManager; @@ -13,8 +13,6 @@ import org.apache.logging.log4j.Logger; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import java.util.TreeMap; - public class WebLoadUtils { ResponseEntity response; Logger logger = LogManager.getLogger();