Adding connect to vocal on web page

This commit is contained in:
BrokenFire 2018-01-12 19:14:43 +01:00
parent d729b7875c
commit 3d661951bd
8 changed files with 179 additions and 11 deletions

View File

@ -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<Class<? extends Commande>> modules = reflections.getSubTypesOf(Commande.class);
logger.info("Find " + modules.size() + " Command:");

View File

@ -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<CommandResponseData> 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);
}
}

View File

@ -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;
}
}

View File

@ -6,4 +6,5 @@ public class CommandPostData {
public boolean onHead;
public String url;
public int playlistLimit;
public String chanelId;
}

View File

@ -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<Chanel> getChanel(){
List<Chanel> 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;
}

View File

@ -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;
}
}

View File

@ -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);

View File

@ -181,6 +181,30 @@
</div>
</li>
<!-- Modal Structure -->
<div id="modalChanels" class="modal">
<div class="modal-content" style="padding-bottom: 0px">
<div class="row" style="margin-bottom: 0px">
<h3 class="col s12 center">Vocal Channels</h3>
<div class="col offset-s4 s4 center">
<form id="channelForm" action="#" class="">
</form>
</div>
</div>
</div>
<div class="modal-footer">
<a href="#!" id="btn_ok_channel" class="modal-action modal-close waves-effect waves-green btn-flat disabled">Connect</a>
</div>
</div>
<p id="radioTemplate" class="" style="visibility: hidden">
<input name="vocalRadio" class="with-gap" type="radio" value="@id" id="@id"/>
<label for="@id">@name</label>
</p>
<!-- Scripts-->
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="js/materialize.js"></script>