Tentative d'ajout de l'api Youtube. Écriture de pas mal de code qui manquait
This commit is contained in:
parent
1320fec143
commit
ec0ce2ae25
4
.gitignore
vendored
4
.gitignore
vendored
@ -4,3 +4,7 @@ bin/
|
|||||||
.settings
|
.settings
|
||||||
.classpath
|
.classpath
|
||||||
.project
|
.project
|
||||||
|
|
||||||
|
gradle/
|
||||||
|
gradlew
|
||||||
|
gradlew.bat
|
||||||
|
|||||||
@ -1,27 +1,47 @@
|
|||||||
package com.bernard.djulia;
|
package com.bernard.djulia;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ShortBuffer;
|
import java.nio.ShortBuffer;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.FutureTask;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.bernard.djulia.DJuLIA.JuliaAudioSendHandler.JuliaAudioChannel;
|
||||||
|
import com.bernard.djulia.DJuLIA.JuliaAudioTrack.Type;
|
||||||
import com.bernard.juliabot.api.Command;
|
import com.bernard.juliabot.api.Command;
|
||||||
import com.bernard.juliabot.api.Discord;
|
import com.bernard.juliabot.api.Discord;
|
||||||
import com.bernard.juliabot.api.DiscordCCommande;
|
import com.bernard.juliabot.api.DiscordCCommande;
|
||||||
import com.bernard.juliabot.api.JuLIAddon;
|
import com.bernard.juliabot.api.JuLIAddon;
|
||||||
import com.bernard.juliabot.api.Trukilie;
|
import com.bernard.juliabot.api.Trukilie;
|
||||||
|
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||||
|
import com.google.api.client.http.javanet.NetHttpTransport;
|
||||||
|
import com.google.api.client.json.JsonFactory;
|
||||||
|
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||||
|
import com.google.api.services.youtube.YouTube;
|
||||||
|
import com.google.api.services.youtube.model.SearchListResponse;
|
||||||
|
import com.google.api.services.youtube.model.SearchResult;
|
||||||
|
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
|
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
|
||||||
@ -34,21 +54,27 @@ import com.sedmelluq.discord.lavaplayer.player.event.TrackExceptionEvent;
|
|||||||
import com.sedmelluq.discord.lavaplayer.player.event.TrackStartEvent;
|
import com.sedmelluq.discord.lavaplayer.player.event.TrackStartEvent;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.event.TrackStuckEvent;
|
import com.sedmelluq.discord.lavaplayer.player.event.TrackStuckEvent;
|
||||||
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
|
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
|
||||||
|
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.AudioTrack;
|
||||||
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
|
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
|
||||||
|
|
||||||
import net.dv8tion.jda.core.EmbedBuilder;
|
import net.dv8tion.jda.core.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.core.Permission;
|
||||||
import net.dv8tion.jda.core.audio.AudioSendHandler;
|
import net.dv8tion.jda.core.audio.AudioSendHandler;
|
||||||
import net.dv8tion.jda.core.entities.Guild;
|
import net.dv8tion.jda.core.entities.Guild;
|
||||||
import net.dv8tion.jda.core.entities.Member;
|
import net.dv8tion.jda.core.entities.Member;
|
||||||
import net.dv8tion.jda.core.entities.Message;
|
import net.dv8tion.jda.core.entities.Message;
|
||||||
import net.dv8tion.jda.core.entities.MessageChannel;
|
import net.dv8tion.jda.core.entities.MessageChannel;
|
||||||
|
import net.dv8tion.jda.core.entities.MessageEmbed;
|
||||||
import net.dv8tion.jda.core.entities.MessageEmbed.Field;
|
import net.dv8tion.jda.core.entities.MessageEmbed.Field;
|
||||||
import net.dv8tion.jda.core.entities.PrivateChannel;
|
import net.dv8tion.jda.core.entities.PrivateChannel;
|
||||||
|
import net.dv8tion.jda.core.entities.Role;
|
||||||
import net.dv8tion.jda.core.entities.TextChannel;
|
import net.dv8tion.jda.core.entities.TextChannel;
|
||||||
import net.dv8tion.jda.core.entities.User;
|
import net.dv8tion.jda.core.entities.User;
|
||||||
import net.dv8tion.jda.core.entities.VoiceChannel;
|
import net.dv8tion.jda.core.entities.VoiceChannel;
|
||||||
import net.dv8tion.jda.core.events.message.react.GenericMessageReactionEvent;
|
import net.dv8tion.jda.core.events.message.react.GenericMessageReactionEvent;
|
||||||
|
import net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent;
|
||||||
import net.dv8tion.jda.core.managers.AudioManager;
|
import net.dv8tion.jda.core.managers.AudioManager;
|
||||||
|
|
||||||
|
|
||||||
@ -63,6 +89,8 @@ public class DJuLIA {
|
|||||||
|
|
||||||
AudioPlayerManager audioManager;
|
AudioPlayerManager audioManager;
|
||||||
|
|
||||||
|
YouTube youtube = getService();
|
||||||
|
|
||||||
public DJuLIA() {
|
public DJuLIA() {
|
||||||
audioManager = new DefaultAudioPlayerManager();
|
audioManager = new DefaultAudioPlayerManager();
|
||||||
AudioSourceManagers.registerRemoteSources(audioManager);
|
AudioSourceManagers.registerRemoteSources(audioManager);
|
||||||
@ -75,9 +103,54 @@ public class DJuLIA {
|
|||||||
bMsg(commande.getChannel(), "Impossible de deviner dans quel serveur vous voulez éxecuter cette commande (y a la commande soundGuild)");
|
bMsg(commande.getChannel(), "Impossible de deviner dans quel serveur vous voulez éxecuter cette commande (y a la commande soundGuild)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
String arg = commande.getStringCommand().substring(commande.getStringCommand().split(" ")[0].length());
|
||||||
|
if(!arg.isEmpty())arg = arg.substring(1);// On enlève l'espace en trop
|
||||||
|
|
||||||
|
if(arg.isEmpty()) {
|
||||||
|
if(!playButton(g, commande.getUser()))
|
||||||
|
bMsg(commande.getChannel(), g.getMember(commande.getUser()).getEffectiveName()+", vous en pouvez pas lancer la lecture ...");
|
||||||
|
return;
|
||||||
|
}else {
|
||||||
|
JuliaAudioTrack jat = understandSong(arg, commande.getChannel(), commande.getUser());
|
||||||
|
if(jat==null) {
|
||||||
|
bMsg(commande.getChannel(),"Je ne sais pas quoi jouer quand tu me dis "+arg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sender(g).getChan(MUSIC).play(jat);
|
||||||
|
if(playingMessages.get(g.getIdLong()).stream().anyMatch(m->m.getGuild().getIdLong()==g.getIdLong()))
|
||||||
|
addPlayingMessage(g, commande.getChannel().sendMessage("DUMMY MESSAGE").complete());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
@Command(admin = false, description = "Permet de rajouter un morcau à la file d'attente", name = "queue")
|
||||||
|
public void queueCommand(DiscordCCommande commande) {
|
||||||
|
Guild g = getGuild(commande);
|
||||||
|
if(g==null) {
|
||||||
|
bMsg(commande.getChannel(), "Impossible de deviner dans quel serveur vous voulez éxecuter cette commande (y a la commande soundGuild)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String arg = commande.getStringCommand().substring(commande.getStringCommand().split(" ")[0].length());
|
||||||
|
if(!arg.isEmpty())arg = arg.substring(1);// On enlève l'espace en trop
|
||||||
|
|
||||||
|
|
||||||
|
JuliaAudioTrack jat = understandSong(arg, commande.getChannel(), commande.getUser());
|
||||||
|
if(jat==null) {
|
||||||
|
bMsg(commande.getChannel(),"Je ne sais pas quoi jouer quand tu me dis "+arg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sender(g).getChan(MUSIC).queue(jat);
|
||||||
|
if(playingMessages.get(g.getIdLong()).stream().anyMatch(m->m.getGuild().getIdLong()==g.getIdLong()))
|
||||||
|
addPlayingMessage(g, commande.getChannel().sendMessage("DUMMY MESSAGE").complete());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Command(admin = false, description = "Définis le serveur utilisé pour les commandes de son de cet utilisateur", name = "soundGuild", synopsis = "!!soundGuild [null|long guildid|string guildName]")
|
@Command(admin = false, description = "Définis le serveur utilisé pour les commandes de son de cet utilisateur", name = "soundGuild", synopsis = "!!soundGuild [null|long guildid|string guildName]")
|
||||||
public void soundGuild(DiscordCCommande commande) {
|
public void soundGuild(DiscordCCommande commande) {
|
||||||
String arg = commande.getStringCommand().substring(commande.getStringCommand().split(" ")[0].length());
|
String arg = commande.getStringCommand().substring(commande.getStringCommand().split(" ")[0].length());
|
||||||
@ -120,27 +193,138 @@ public class DJuLIA {
|
|||||||
localGuilds.put(commande.getUser().getIdLong(), guilds.get(index).getIdLong());
|
localGuilds.put(commande.getUser().getIdLong(), guilds.get(index).getIdLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Commandes internes
|
||||||
|
public boolean playButton(Guild g,User u) {
|
||||||
|
JuliaAudioChannel chan = guilds.get(g.getIdLong()).getChan(MUSIC);
|
||||||
|
if(!isDJ(g, u) && chan.currentTrack().querier.getIdLong() != u.getIdLong())
|
||||||
|
return false;
|
||||||
|
chan.play();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean pauseButton(Guild g,User u) {
|
||||||
|
JuliaAudioChannel chan = guilds.get(g.getIdLong()).getChan(MUSIC);
|
||||||
|
if(!isDJ(g, u) && chan.currentTrack().querier.getIdLong() != u.getIdLong())
|
||||||
|
return false;
|
||||||
|
chan.pause();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean nextButton(Guild g,User u) {
|
||||||
|
JuliaAudioChannel chan = guilds.get(g.getIdLong()).getChan(MUSIC);
|
||||||
|
if(!isDJ(g, u) && chan.currentTrack().querier.getIdLong() != u.getIdLong())
|
||||||
|
return false;
|
||||||
|
chan.next();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean stopButton(Guild g,User u) {
|
||||||
|
JuliaAudioChannel chan = guilds.get(g.getIdLong()).getChan(MUSIC);
|
||||||
|
if(!isDJ(g, u) && chan.currentTrack().querier.getIdLong() != u.getIdLong())
|
||||||
|
return false;
|
||||||
|
chan.stop();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean loopButton(Guild g,User u) {
|
||||||
|
if(!isDJ(g, u))
|
||||||
|
return false;
|
||||||
|
JuliaAudioChannel chan = guilds.get(g.getIdLong()).getChan(MUSIC);
|
||||||
|
chan.looping = !chan.looping;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean emptyButton(Guild g,User u) {
|
||||||
|
if(!isDJ(g, u))
|
||||||
|
return false;
|
||||||
|
JuliaAudioChannel chan = guilds.get(g.getIdLong()).getChan(MUSIC);
|
||||||
|
chan.empty();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Map<Long,Set<Message>> playingMessages = new HashMap<>();
|
||||||
|
public void updatePlayingMessages(Guild g) {
|
||||||
|
if(!playingMessages.containsKey(g.getIdLong()))
|
||||||
|
return;
|
||||||
|
JuliaAudioChannel chan = guilds.get(g.getIdLong()).getChan(MUSIC);
|
||||||
|
JuliaAudioTrack playing = chan.currentTrack();
|
||||||
|
EmbedBuilder builder = new EmbedBuilder();
|
||||||
|
builder.setAuthor("DJulia in the place");
|
||||||
|
builder.setTitle("Je joue "+playing.displayName+" demandé par"+g.getMember(playing.querier));
|
||||||
|
int progressP100 = (int) (chan.playProgress()*10);
|
||||||
|
String t1 = "==========",t2 = "----------";
|
||||||
|
builder.setDescription("`<"+t1.substring(progressP100)+t2.substring(10-progressP100)+">`");
|
||||||
|
builder.addField("Il y a "+chan.trackLeftCount()+" musiques dans la queue", null, false);
|
||||||
|
MessageEmbed embed = builder.build();
|
||||||
|
for(Message m : playingMessages.get(g.getIdLong()))
|
||||||
|
m.editMessage(embed).queue();
|
||||||
|
}
|
||||||
|
public void addPlayingMessage(Guild g,Message m) {
|
||||||
|
if(!playingMessages.containsKey(g.getIdLong()))
|
||||||
|
playingMessages.put(g.getIdLong(),new HashSet<>());
|
||||||
|
playingMessages.get(g.getIdLong()).add(m);
|
||||||
|
updatePlayingMessages(g);
|
||||||
|
m.addReaction("▶").queue();
|
||||||
|
m.addReaction("⏸").queue();
|
||||||
|
m.addReaction("⏭").queue();
|
||||||
|
m.addReaction("⏹").queue();
|
||||||
|
m.addReaction("🔁").queue();
|
||||||
|
m.addReaction("🗑").queue();
|
||||||
|
}
|
||||||
|
@Discord(description = "Vérifie les boutons des playing messages")
|
||||||
|
public void onReact(MessageReactionAddEvent e) {
|
||||||
|
if(!playingMessages.containsKey(e.getGuild().getIdLong()))return;
|
||||||
|
if(!playingMessages.get(e.getGuild().getIdLong()).stream().map(Message::getIdLong).anyMatch(l->l==e.getMessageIdLong()))return;
|
||||||
|
if(e.getReactionEmote().isEmote())return;
|
||||||
|
if(!"▶⏸⏭⏹🔁🗑".contains(e.getReactionEmote().getName()))return;
|
||||||
|
Guild g = getGuild(e.getUser(),e.getChannel());
|
||||||
|
User u = e.getUser();
|
||||||
|
switch(e.getReactionEmote().getName()) {
|
||||||
|
case "▶":
|
||||||
|
playButton(g, u);
|
||||||
|
break;
|
||||||
|
case "⏸":
|
||||||
|
pauseButton(g, u);
|
||||||
|
break;
|
||||||
|
case "⏭":
|
||||||
|
nextButton(g, u);
|
||||||
|
break;
|
||||||
|
case "⏹":
|
||||||
|
stopButton(g, u);
|
||||||
|
break;
|
||||||
|
case "🔁":
|
||||||
|
loopButton(g, u);
|
||||||
|
break;
|
||||||
|
case "🗑":
|
||||||
|
emptyButton(g, u);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Guild getGuild(DiscordCCommande commande) {
|
public Guild getGuild(DiscordCCommande commande) {
|
||||||
|
return getGuild(commande.getUser(), commande.getChannel());
|
||||||
|
}
|
||||||
|
public Guild getGuild(User u,MessageChannel chan) {
|
||||||
|
|
||||||
|
if(chan instanceof TextChannel)
|
||||||
|
return ((TextChannel)chan).getGuild();
|
||||||
|
|
||||||
if(commande.getChannel() instanceof TextChannel)
|
if(localGuilds.containsKey(u.getIdLong()))
|
||||||
return ((TextChannel)commande.getChannel()).getGuild();
|
return Trukilie.jda().getGuildById(localGuilds.get(u.getIdLong()));
|
||||||
|
|
||||||
if(localGuilds.containsKey(commande.getUser().getIdLong()))
|
List<Guild> mutualGuilds = u.getMutualGuilds();
|
||||||
return Trukilie.jda().getGuildById(localGuilds.get(commande.getUser().getIdLong()));
|
|
||||||
|
|
||||||
List<Guild> mutualGuilds = commande.getUser().getMutualGuilds();
|
|
||||||
if(mutualGuilds.size() == 1)
|
if(mutualGuilds.size() == 1)
|
||||||
return mutualGuilds.get(0);
|
return mutualGuilds.get(0);
|
||||||
|
|
||||||
Set<Guild> vocalGuilds = mutualGuilds.stream()
|
Set<Guild> vocalGuilds = mutualGuilds.stream()
|
||||||
.filter(gu->gu.getMember(commande.getUser()).getVoiceState().inVoiceChannel())
|
.filter(gu->gu.getMember(u).getVoiceState().inVoiceChannel())
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
if(vocalGuilds.size() == 1)
|
if(vocalGuilds.size() == 1)
|
||||||
return vocalGuilds.iterator().next();
|
return vocalGuilds.iterator().next();
|
||||||
|
|
||||||
Set<Guild> vocalGuildsWithJulia = vocalGuilds.stream()
|
Set<Guild> vocalGuildsWithJulia = vocalGuilds.stream()
|
||||||
.filter(g->guilds.containsKey(g.getIdLong()))
|
.filter(g->guilds.containsKey(g.getIdLong()))
|
||||||
.filter(g->guilds.get(g.getIdLong()).currentChannel.getId().equals(g.getMember(commande.getUser()).getVoiceState().getAudioChannel().getId()))
|
.filter(g->guilds.get(g.getIdLong()).currentChannel.getId().equals(g.getMember(u).getVoiceState().getAudioChannel().getId()))
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
if(vocalGuildsWithJulia.size() == 1)
|
if(vocalGuildsWithJulia.size() == 1)
|
||||||
return vocalGuildsWithJulia.iterator().next();
|
return vocalGuildsWithJulia.iterator().next();
|
||||||
@ -148,7 +332,74 @@ public class DJuLIA {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JuliaAudioTrack understandSong(String text,MessageChannel chan, User u) {
|
||||||
|
//TODO every case
|
||||||
|
Pattern youtubeURL = Pattern.compile("^(https?\\:\\/\\/)?(www\\.)?(youtube\\.com|youtu\\.?be)\\/.+$");
|
||||||
|
String toLoad = text;
|
||||||
|
String displayName = toLoad;
|
||||||
|
Type type = Type.LOCAL;
|
||||||
|
if(!youtubeURL.matcher(text).find()) {
|
||||||
|
//Search in files
|
||||||
|
//TODO à implémenter : les fichiers locaux*
|
||||||
|
|
||||||
|
//else make youtube search
|
||||||
|
try {
|
||||||
|
YouTube.Search.List request = youtube.search().list("snippet");
|
||||||
|
SearchListResponse response = request.setMaxResults(10L)
|
||||||
|
.setQ(text)
|
||||||
|
.setType("video")
|
||||||
|
.execute();
|
||||||
|
List<SearchResult> results = response.getItems();
|
||||||
|
int index = selector(chan, u, "Quelle vidéo Youtube voulez-vous lire ?", results.stream().map(s->s.getSnippet().getTitle()+" de "+s.getSnippet().getChannelTitle()).collect(Collectors.toList()));
|
||||||
|
toLoad = "htp://youtu.be/"+results.get(index).getId();
|
||||||
|
displayName = results.get(index).getSnippet().getTitle();
|
||||||
|
type = Type.YOUTUBE;
|
||||||
|
//TODO traiter le cas pas de réponce
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AudioTrack[] t = {null};
|
||||||
|
try {
|
||||||
|
audioManager.loadItem(toLoad, new AudioLoadResultHandler() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void trackLoaded(AudioTrack track) {
|
||||||
|
t[0] = track;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playlistLoaded(AudioPlaylist playlist) {
|
||||||
|
//TODO gérer les playlistes
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void noMatches() {
|
||||||
|
System.err.println("Les vérifications n'on pas marchées DJulia");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void loadFailed(FriendlyException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}).get();
|
||||||
|
return new JuliaAudioTrack(t[0], type, toLoad, u, displayName);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDJ(Guild g,User u) {
|
||||||
|
Optional<Role> rr = g.getRolesByName("dj", true).stream().findAny();
|
||||||
|
if(!rr.isPresent())
|
||||||
|
return g.getMember(u).hasPermission(Permission.ADMINISTRATOR);
|
||||||
|
else
|
||||||
|
return g.getMember(u).getRoles().contains(rr.get());
|
||||||
|
}
|
||||||
|
|
||||||
Map<Long,Object> waitingEmojis = new HashMap<>();
|
Map<Long,Object> waitingEmojis = new HashMap<>();
|
||||||
Map<Long,Lock> everyEventLocks = new HashMap<>();
|
Map<Long,Lock> everyEventLocks = new HashMap<>();
|
||||||
@ -256,6 +507,13 @@ public class DJuLIA {
|
|||||||
chan.sendMessage(builder.build()).queue();
|
chan.sendMessage(builder.build()).queue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JuliaAudioSendHandler sender(Guild g) {
|
||||||
|
if(guilds.containsKey(g.getIdLong()))
|
||||||
|
return guilds.get(g.getIdLong());
|
||||||
|
guilds.put(g.getIdLong(), new JuliaAudioSendHandler(g));
|
||||||
|
return guilds.get(g.getIdLong());
|
||||||
|
}
|
||||||
|
|
||||||
public class JuliaAudioSendHandler implements AudioSendHandler{
|
public class JuliaAudioSendHandler implements AudioSendHandler{
|
||||||
VoiceChannel currentChannel;
|
VoiceChannel currentChannel;
|
||||||
Guild currentGuild;
|
Guild currentGuild;
|
||||||
@ -296,10 +554,14 @@ public class DJuLIA {
|
|||||||
playlist = new ArrayList<>();
|
playlist = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void play() {
|
||||||
|
if(playlistIndex<playlist.size())
|
||||||
|
player.setPaused(false);
|
||||||
|
}
|
||||||
|
|
||||||
public void play(JuliaAudioTrack track) {
|
public void play(JuliaAudioTrack track) {
|
||||||
playlist.add(playlistIndex+1, track);
|
playlist.add(playlistIndex+1, track);
|
||||||
playlistIndex++;
|
player.playTrack(playlist.get(playlistIndex).track.makeClone());
|
||||||
player.playTrack(playlist.get(playlistIndex).track);
|
|
||||||
player.setPaused(false);
|
player.setPaused(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,8 +573,41 @@ public class DJuLIA {
|
|||||||
player.setPaused(!player.isPaused());
|
player.setPaused(!player.isPaused());
|
||||||
}
|
}
|
||||||
|
|
||||||
public AudioTrack currentTrack() {
|
public void next() {
|
||||||
return player.getPlayingTrack();
|
player.stopTrack();
|
||||||
|
playlistIndex++;
|
||||||
|
player.playTrack(playlist.get(playlistIndex).track.makeClone());
|
||||||
|
// Let paused state as it is
|
||||||
|
///XXX check TrackEndEvent is not launc
|
||||||
|
}
|
||||||
|
|
||||||
|
public void empty() {
|
||||||
|
player.stopTrack();
|
||||||
|
playlistIndex=0;
|
||||||
|
player.setPaused(true);
|
||||||
|
playlist.clear();
|
||||||
|
looping = false;
|
||||||
|
///XXX check TrackEndEvent is not launced
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
player.stopTrack();
|
||||||
|
playlistIndex++;
|
||||||
|
player.playTrack(playlist.get(playlistIndex).track.makeClone());
|
||||||
|
player.setPaused(true);
|
||||||
|
///XXX check TrackEndEvent is not launched
|
||||||
|
}
|
||||||
|
|
||||||
|
public float playProgress() {
|
||||||
|
return ((float)currentTrack().track.getPosition()) / currentTrack().track.getDuration();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int trackLeftCount() {
|
||||||
|
return playlist.size()-playlistIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JuliaAudioTrack currentTrack() {
|
||||||
|
return playlist.get(playlistIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<JuliaAudioTrack> getPlaylist() {
|
public List<JuliaAudioTrack> getPlaylist() {
|
||||||
@ -326,8 +621,10 @@ public class DJuLIA {
|
|||||||
if(playlist.size()-1>=playlistIndex) {
|
if(playlist.size()-1>=playlistIndex) {
|
||||||
if(!looping)
|
if(!looping)
|
||||||
playlistIndex++;
|
playlistIndex++;
|
||||||
player.playTrack(playlist.get(playlistIndex).track);
|
if(playlistIndex<playlist.size()) {
|
||||||
player.setPaused(false);
|
player.playTrack(playlist.get(playlistIndex).track.makeClone());
|
||||||
|
player.setPaused(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}else if(event instanceof TrackExceptionEvent) {
|
}else if(event instanceof TrackExceptionEvent) {
|
||||||
|
|
||||||
@ -383,6 +680,7 @@ public class DJuLIA {
|
|||||||
int j = i;
|
int j = i;
|
||||||
resultBuffer.put(i, (short) (afs.stream().mapToInt(sb->sb.get(j)/n).sum()));
|
resultBuffer.put(i, (short) (afs.stream().mapToInt(sb->sb.get(j)/n).sum()));
|
||||||
}
|
}
|
||||||
|
updatePlayingMessages(currentGuild);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -398,14 +696,16 @@ public class DJuLIA {
|
|||||||
Type type;
|
Type type;
|
||||||
String identifier;
|
String identifier;
|
||||||
User querier;
|
User querier;
|
||||||
|
String displayName;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public JuliaAudioTrack(AudioTrack track, Type type, String identifier, User querier) {
|
public JuliaAudioTrack(AudioTrack track, Type type, String identifier, User querier,String displayName) {
|
||||||
this.track = track;
|
this.track = track;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.querier = querier;
|
this.querier = querier;
|
||||||
|
this.displayName = displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -416,4 +716,21 @@ public class DJuLIA {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static final String APPLICATION_NAME = "DJulia";
|
||||||
|
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build and return an authorized API client service.
|
||||||
|
*
|
||||||
|
* @return an authorized API client service
|
||||||
|
* @throws GeneralSecurityException, IOException
|
||||||
|
*/
|
||||||
|
public static YouTube getService() throws GeneralSecurityException, IOException {
|
||||||
|
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
|
||||||
|
return new YouTube.Builder(httpTransport, JSON_FACTORY, null)
|
||||||
|
.setApplicationName(APPLICATION_NAME)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user