From b174e67be07df370f820e0d420e41f38ad734908 Mon Sep 17 00:00:00 2001 From: BrokenFire Date: Tue, 12 Dec 2017 23:29:49 +0100 Subject: [PATCH] Add RestApi for CurrentMusicInfo --- build.gradle | 1 + src/main/java/net/Broken/BotListener.java | 28 ++++---------- src/main/java/net/Broken/Commandes/Music.java | 16 +++++--- src/main/java/net/Broken/MainBot.java | 2 - .../Broken/RestApi/Data/CurrentMusicData.java | 38 +++++++++++++++++++ .../Broken/RestApi/MusicWebController.java | 31 +++++++++++++++ src/main/java/net/Broken/audio/AudioM.java | 32 ++++++++++------ .../net/Broken/audio/GuildMusicManager.java | 1 + .../Broken/audio/NotConectedException.java | 4 ++ .../net/Broken/audio/NullMusicManager.java | 4 ++ src/main/resources/application.properties | 1 - 11 files changed, 116 insertions(+), 42 deletions(-) create mode 100644 src/main/java/net/Broken/RestApi/Data/CurrentMusicData.java create mode 100644 src/main/java/net/Broken/RestApi/MusicWebController.java create mode 100644 src/main/java/net/Broken/audio/NotConectedException.java create mode 100644 src/main/java/net/Broken/audio/NullMusicManager.java diff --git a/build.gradle b/build.gradle index c75659c..b652b57 100644 --- a/build.gradle +++ b/build.gradle @@ -4,6 +4,7 @@ buildscript { } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE") + classpath 'org.springframework:springloaded:1.2.6.RELEASE' } } diff --git a/src/main/java/net/Broken/BotListener.java b/src/main/java/net/Broken/BotListener.java index a42eb64..4e3dc19 100644 --- a/src/main/java/net/Broken/BotListener.java +++ b/src/main/java/net/Broken/BotListener.java @@ -2,43 +2,30 @@ package net.Broken; import net.Broken.Commandes.Move; import net.Broken.Commandes.Music; -import net.Broken.Commandes.Spam; import net.Broken.Outils.AntiSpam; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.MessageTimeOut; import net.Broken.Outils.Moderateur; -import net.dv8tion.jda.core.JDA; -import net.dv8tion.jda.core.entities.*; -import net.dv8tion.jda.core.events.ExceptionEvent; +import net.dv8tion.jda.core.entities.ChannelType; +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.Member; +import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.events.ReadyEvent; import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.core.events.guild.member.GuildMemberRoleRemoveEvent; -import net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent; import net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; -import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent; -import net.dv8tion.jda.core.exceptions.HierarchyException; import net.dv8tion.jda.core.hooks.ListenerAdapter; import net.dv8tion.jda.core.managers.GuildManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.List; /** * Created by seb65 on 19/10/2016. */ - public class BotListener extends ListenerAdapter { - AntiSpam antispam=new AntiSpam(); - Moderateur modo = new Moderateur(); - Logger logger = LogManager.getLogger(); - @Value("${name}") - public String test; + private AntiSpam antispam=new AntiSpam(); + private Moderateur modo = new Moderateur(); + private Logger logger = LogManager.getLogger(); @Override @@ -97,7 +84,6 @@ public class BotListener extends ListenerAdapter { public void onMessageReceived(MessageReceivedEvent event) { // ----------------------Test pour eviter eco de commande------------------------- - logger.info(test); 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()); diff --git a/src/main/java/net/Broken/Commandes/Music.java b/src/main/java/net/Broken/Commandes/Music.java index e784fe6..e38389a 100644 --- a/src/main/java/net/Broken/Commandes/Music.java +++ b/src/main/java/net/Broken/Commandes/Music.java @@ -56,7 +56,7 @@ public class Music implements Commande { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } } else{ @@ -65,7 +65,7 @@ public class Music implements Commande { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } break; @@ -109,7 +109,7 @@ public class Music implements Commande { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } break; @@ -124,7 +124,7 @@ public class Music implements Commande { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } break; @@ -134,7 +134,7 @@ public class Music implements Commande { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); break; } @@ -145,7 +145,7 @@ public class Music implements Commande { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } } @@ -164,4 +164,8 @@ public class Music implements Commande { public boolean isPrivateUsable() { return false; } + + public AudioM getAudioManager(){ + return audio; + } } diff --git a/src/main/java/net/Broken/MainBot.java b/src/main/java/net/Broken/MainBot.java index 7ae92ad..d05f0bd 100644 --- a/src/main/java/net/Broken/MainBot.java +++ b/src/main/java/net/Broken/MainBot.java @@ -10,7 +10,6 @@ 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; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ExitCodeGenerator; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -25,7 +24,6 @@ import java.util.HashMap; @SpringBootApplication public class MainBot { - public static final CommandParser parser =new CommandParser(); public static HashMap commandes = new HashMap<>(); public static HashMap> historique =new HashMap<>(); diff --git a/src/main/java/net/Broken/RestApi/Data/CurrentMusicData.java b/src/main/java/net/Broken/RestApi/Data/CurrentMusicData.java new file mode 100644 index 0000000..6b2e92a --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/CurrentMusicData.java @@ -0,0 +1,38 @@ +package net.Broken.RestApi.Data; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; +import com.sedmelluq.discord.lavaplayer.track.AudioTrackState; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class CurrentMusicData { + private final AudioTrackInfo info; + private final long currentPos; + private final String state; + private final boolean pause; + + + public CurrentMusicData(AudioTrackInfo info, long currentPos, String state, boolean pause) { + this.info = info; + this.currentPos = currentPos; + this.state = state; + this.pause = pause; + } + + public AudioTrackInfo getInfo() { + return info; + } + + public long getCurrentPos() { + return currentPos; + } + + public String getState() { + if(pause) + return "PAUSE"; + else + return state; + } +} diff --git a/src/main/java/net/Broken/RestApi/MusicWebController.java b/src/main/java/net/Broken/RestApi/MusicWebController.java new file mode 100644 index 0000000..84df22f --- /dev/null +++ b/src/main/java/net/Broken/RestApi/MusicWebController.java @@ -0,0 +1,31 @@ +package net.Broken.RestApi; + +import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; +import com.sedmelluq.discord.lavaplayer.track.AudioTrack; +import net.Broken.Commandes.Music; +import net.Broken.MainBot; +import net.Broken.RestApi.Data.CurrentMusicData; +import net.Broken.audio.NotConectedException; +import net.Broken.audio.NullMusicManager; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/music/") +public class MusicWebController { + @RequestMapping("/currentMusicInfo") + public CurrentMusicData test(){ + Music musicCommande = (Music) MainBot.commandes.get("music"); + 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); + } + } +} diff --git a/src/main/java/net/Broken/audio/AudioM.java b/src/main/java/net/Broken/audio/AudioM.java index ed42297..4ca74f3 100644 --- a/src/main/java/net/Broken/audio/AudioM.java +++ b/src/main/java/net/Broken/audio/AudioM.java @@ -51,7 +51,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); play(event.getGuild(), voiceChannel, musicManager, track, onHead); } @@ -65,7 +65,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); int i = 0; for(AudioTrack track : playlist.getTracks()){ play(event.getGuild(), voiceChannel, musicManager, track,onHead); @@ -84,7 +84,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } @Override @@ -96,7 +96,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } }); } @@ -132,7 +132,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } public void pause(MessageReceivedEvent event) { @@ -144,7 +144,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } public void resume (MessageReceivedEvent event) { @@ -156,7 +156,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } public void info(MessageReceivedEvent event){ @@ -168,7 +168,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } public void flush(MessageReceivedEvent event){ @@ -179,7 +179,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } public void list(MessageReceivedEvent event){ @@ -202,7 +202,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, listTimeOut).run(); + new MessageTimeOut(messages, listTimeOut).start(); } @@ -217,7 +217,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } } @@ -232,7 +232,7 @@ public class AudioM { add(message); add(event.getMessage()); }}; - new MessageTimeOut(messages, MainBot.messageTimeOut).run(); + new MessageTimeOut(messages, MainBot.messageTimeOut).start(); } public void stop (GuildVoiceLeaveEvent event) { @@ -243,6 +243,14 @@ public class AudioM { event.getGuild().getAudioManager().closeAudioConnection(); } + public GuildMusicManager getMusicManager() throws NullMusicManager, NotConectedException { + if( musicManager == null) + throw new NullMusicManager(); + else if( playedChanel == null) + throw new NotConectedException(); + return musicManager; + } + } diff --git a/src/main/java/net/Broken/audio/GuildMusicManager.java b/src/main/java/net/Broken/audio/GuildMusicManager.java index 41ca494..40f9035 100644 --- a/src/main/java/net/Broken/audio/GuildMusicManager.java +++ b/src/main/java/net/Broken/audio/GuildMusicManager.java @@ -24,6 +24,7 @@ public class GuildMusicManager { player = manager.createPlayer(); scheduler = new TrackScheduler(player); player.addListener(scheduler); + } /** diff --git a/src/main/java/net/Broken/audio/NotConectedException.java b/src/main/java/net/Broken/audio/NotConectedException.java new file mode 100644 index 0000000..1c40122 --- /dev/null +++ b/src/main/java/net/Broken/audio/NotConectedException.java @@ -0,0 +1,4 @@ +package net.Broken.audio; + +public class NotConectedException extends Exception { +} diff --git a/src/main/java/net/Broken/audio/NullMusicManager.java b/src/main/java/net/Broken/audio/NullMusicManager.java new file mode 100644 index 0000000..a32d1d3 --- /dev/null +++ b/src/main/java/net/Broken/audio/NullMusicManager.java @@ -0,0 +1,4 @@ +package net.Broken.audio; + +public class NullMusicManager extends Exception { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6825415..4c6606e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1 @@ server.port=${PORT} -#name = "test"