From 3d661951bde1e31331032583d35849a146cb90f7 Mon Sep 17 00:00:00 2001 From: BrokenFire Date: Fri, 12 Jan 2018 19:14:43 +0100 Subject: [PATCH] Adding connect to vocal on web page --- .../Broken/Outils/Command/CommandLoader.java | 9 ++- .../net/Broken/RestApi/Commands/Connect.java | 32 ++++++++ .../java/net/Broken/RestApi/Data/Chanel.java | 14 ++++ .../Broken/RestApi/Data/CommandPostData.java | 1 + .../Broken/RestApi/MusicWebAPIController.java | 26 +++++-- src/main/java/net/Broken/audio/AudioM.java | 8 +- src/main/resources/static/js/init.js | 76 ++++++++++++++++++- src/main/resources/templates/music.html | 24 ++++++ 8 files changed, 179 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/Broken/RestApi/Commands/Connect.java create mode 100644 src/main/java/net/Broken/RestApi/Data/Chanel.java diff --git a/src/main/java/net/Broken/Outils/Command/CommandLoader.java b/src/main/java/net/Broken/Outils/Command/CommandLoader.java index 3ae8b3f..cd6ece5 100644 --- a/src/main/java/net/Broken/Outils/Command/CommandLoader.java +++ b/src/main/java/net/Broken/Outils/Command/CommandLoader.java @@ -2,10 +2,11 @@ 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 org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; import java.util.Set; @@ -13,7 +14,11 @@ public class CommandLoader { private static Logger logger = LogManager.getLogger(); public static void load(){ logger.info("Loading Command..."); - Reflections reflections = new Reflections("net.Broken.Command"); + Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage( + "net.Broken.Commands", + ClasspathHelper.contextClassLoader(), + ClasspathHelper.staticClassLoader())) + ); Set> modules = reflections.getSubTypesOf(Commande.class); logger.info("Find " + modules.size() + " Command:"); diff --git a/src/main/java/net/Broken/RestApi/Commands/Connect.java b/src/main/java/net/Broken/RestApi/Commands/Connect.java new file mode 100644 index 0000000..ba307b2 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Commands/Connect.java @@ -0,0 +1,32 @@ +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.AudioM; +import net.dv8tion.jda.core.entities.VoiceChannel; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public class Connect implements CommandInterface{ + @Override + public ResponseEntity action(Music musicCommande, CommandPostData data) { + AudioM audioM = musicCommande.getAudioManager(); + if(data.chanelId == null) + return new ResponseEntity<>(new CommandResponseData(data.command,"Missing chanelId"),HttpStatus.BAD_REQUEST); + VoiceChannel voiceChannel = null; + try{ + voiceChannel = audioM.getGuild().getVoiceChannelById(data.chanelId); + }catch (NumberFormatException ignored){} + + if(voiceChannel == null){ + return new ResponseEntity<>(new CommandResponseData(data.command,"Channel Not found"), HttpStatus.BAD_REQUEST); + } + + audioM.getGuildAudioPlayer(musicCommande.getAudioManager().getGuild()); + audioM.getGuild().getAudioManager().openAudioConnection(audioM.getGuild().getVoiceChannelById(data.chanelId)); + audioM.setPlayedChanel(voiceChannel); + return new ResponseEntity<>(new CommandResponseData(data.command,"Accepted"),HttpStatus.OK); + } +} diff --git a/src/main/java/net/Broken/RestApi/Data/Chanel.java b/src/main/java/net/Broken/RestApi/Data/Chanel.java new file mode 100644 index 0000000..7bdca94 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/Chanel.java @@ -0,0 +1,14 @@ +package net.Broken.RestApi.Data; + +public class Chanel { + public String name; + public String id; + public int pos; + + public Chanel(String name, String id, int pos) { + this.name = name; + this.id = id; + this.pos = pos; + + } +} diff --git a/src/main/java/net/Broken/RestApi/Data/CommandPostData.java b/src/main/java/net/Broken/RestApi/Data/CommandPostData.java index 1de763c..f08c26d 100644 --- a/src/main/java/net/Broken/RestApi/Data/CommandPostData.java +++ b/src/main/java/net/Broken/RestApi/Data/CommandPostData.java @@ -6,4 +6,5 @@ public class CommandPostData { public boolean onHead; public String url; public int playlistLimit; + public String chanelId; } diff --git a/src/main/java/net/Broken/RestApi/MusicWebAPIController.java b/src/main/java/net/Broken/RestApi/MusicWebAPIController.java index 88154fd..309b837 100644 --- a/src/main/java/net/Broken/RestApi/MusicWebAPIController.java +++ b/src/main/java/net/Broken/RestApi/MusicWebAPIController.java @@ -4,23 +4,25 @@ 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.RestApi.Data.*; import net.Broken.audio.NotConectedException; import net.Broken.audio.NullMusicManager; +import net.dv8tion.jda.core.entities.VoiceChannel; 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 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.ArrayList; import java.util.List; +// import net.Broken.DB.SavedPlaylistRepository; + @RestController @RequestMapping("/api/music/") public class MusicWebAPIController { @@ -80,6 +82,16 @@ public class MusicWebAPIController { return new ResponseEntity<>(new CommandResponseData(null, null), HttpStatus.NO_CONTENT); } + @RequestMapping(value = "/getChanel", method = RequestMethod.GET) + public List getChanel(){ + List temp = new ArrayList<>(); + logger.info(MainBot.jda.getVoiceChannels().size()); + for(VoiceChannel aChanel : MainBot.jda.getVoiceChannels()){ + temp.add(new Chanel(aChanel.getName(),aChanel.getId(),aChanel.getPosition())); + } + return temp; + } + diff --git a/src/main/java/net/Broken/audio/AudioM.java b/src/main/java/net/Broken/audio/AudioM.java index 216b0d3..51f45e2 100644 --- a/src/main/java/net/Broken/audio/AudioM.java +++ b/src/main/java/net/Broken/audio/AudioM.java @@ -25,6 +25,8 @@ import java.util.ArrayList; import java.util.List; public class AudioM { + + private GuildMusicManager musicManager; private AudioPlayerManager playerManager; private VoiceChannel playedChanel; @@ -115,7 +117,7 @@ public class AudioM { } - private GuildMusicManager getGuildAudioPlayer(Guild guild) { + public GuildMusicManager getGuildAudioPlayer(Guild guild) { if (musicManager == null) { musicManager = new GuildMusicManager(playerManager); } @@ -273,4 +275,8 @@ public class AudioM { public VoiceChannel getPlayedChanel() { return playedChanel; } + + public void setPlayedChanel(VoiceChannel playedChanel) { + this.playedChanel = playedChanel; + } } diff --git a/src/main/resources/static/js/init.js b/src/main/resources/static/js/init.js index 9ccaa96..e836857 100644 --- a/src/main/resources/static/js/init.js +++ b/src/main/resources/static/js/init.js @@ -1,11 +1,21 @@ var savedPlaylist; var error = false; var state; +var disconected = false; $(document).ready(function() { + setInterval("getCurentMusic()",1000); // the "href" attribute of the modal trigger must specify the modal ID that wants to be triggered - $('.modal').modal(); + $('#modalAdd').modal(); + + $('#modalChanels').modal({ + dismissible: false, // Modal can be dismissed by clicking outside of the modal + }) + + $('#modalChanels').modal('open'); + + $('.button-collapse-1').sideNav({ menuWidth: 400, // Default is 300 edge: 'right', // Choose the horizontal origin @@ -64,6 +74,11 @@ $(document).ready(function() { } } }); + $('#modalChanels').change(function () { + if ($('#btn_ok_channel').hasClass("disabled")) { + $('#btn_ok_channel').removeClass("disabled"); + } + }); $('#flush_btn').click(function () { var command = { @@ -82,6 +97,7 @@ $(document).ready(function() { $('#input_link').val(''); sendCommand(JSON.stringify(command)); }); + $('#btn_add_bottom').click(function () { var command = { @@ -94,6 +110,15 @@ $(document).ready(function() { sendCommand(JSON.stringify(command)); }); + $('#btn_ok_channel').click(function () { + + var command = { + command: "CONNECT", + chanelId: $('input[name=vocalRadio]:checked').val() + }; + sendCommand(JSON.stringify(command)); + }); + }) @@ -110,6 +135,8 @@ function getCurentMusic() { state = data.state; switch (data.state) { case "STOP": + disconected = false; + $('#modalChanels').modal('close'); $('#music_text').text("Connected on Vocal Channel"); if (!$('#btn_info').hasClass("indeterminate")) { @@ -131,6 +158,13 @@ function getCurentMusic() { $('#flush_btn').removeClass("disabled"); } + if ($('#btn_play').hasClass("disabled")) { + $('#btn_play').removeClass("disabled"); + } + if ($('#btn_next').hasClass("disabled")) { + $('#btn_next').removeClass("disabled"); + } + $('#music_img').attr("src","/img/no_music.jpg"); $('#total_time').text("00:00"); $('#current_time').text("00:00"); @@ -138,12 +172,16 @@ function getCurentMusic() { break; case "PLAYING": + disconected = false; + $('#modalChanels').modal('close'); $('#btn_play').children().text("pause"); updateControl(data); break; case "PAUSE": + disconected = false; + $('#modalChanels').modal('close'); $('#btn_play').children().text("play_arrow"); updateControl(data); @@ -151,6 +189,8 @@ function getCurentMusic() { break; case "LOADING": + disconected = false; + $('#modalChanels').modal('close'); if (!$('#btn_info').hasClass("determinate")) { $('#btn_info').addClass("indeterminate").removeClass("determinate"); } @@ -186,6 +226,14 @@ function getCurentMusic() { $('#music_img').attr("src","/img/disconnected.png"); + + if(!disconected){ + getChannels(); + disconected = true; + } + + + break; } getPlayList(); @@ -236,6 +284,32 @@ function getPlayList() { } +function getChannels(){ + $.get("api/music/getChanel", function (data) { + }).done(function (data) { + console.log(data); + $('#channelForm').empty(); + data.forEach(function(element){ + var template = $('#radioTemplate').clone(); + template.removeAttr("id"); + template.removeAttr("style"); + var content = template.html(); + content = content.replace("@name", element.name); + content = content.replace(/@id/g, element.id); + template.html(content); + + $('#channelForm').append(template); + + }); + $('#modalChanels').modal('open'); + + + + + + }); +} + function updateModal(data){ $('#modal_title').text("Title: "+ data.info.title); $('#modal_author').text("Author: "+ data.info.author); diff --git a/src/main/resources/templates/music.html b/src/main/resources/templates/music.html index 37d9021..c3d7164 100644 --- a/src/main/resources/templates/music.html +++ b/src/main/resources/templates/music.html @@ -181,6 +181,30 @@ + + + + +