Réécriture totale du code. C'est un addon nouveau
This commit is contained in:
@@ -0,0 +1,427 @@
|
||||
package com.bernard.discord.commands;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.bernard.discord.api.Command;
|
||||
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.player.DefaultAudioPlayerManager;
|
||||
import com.sedmelluq.discord.lavaplayer.player.event.AudioEvent;
|
||||
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventListener;
|
||||
import com.sedmelluq.discord.lavaplayer.player.event.TrackEndEvent;
|
||||
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.playback.AudioFrame;
|
||||
|
||||
import net.dv8tion.jda.core.EmbedBuilder;
|
||||
import net.dv8tion.jda.core.MessageBuilder;
|
||||
import net.dv8tion.jda.core.audio.AudioSendHandler;
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.GuildVoiceState;
|
||||
import net.dv8tion.jda.core.entities.Message;
|
||||
import net.dv8tion.jda.core.entities.MessageChannel;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
import net.dv8tion.jda.core.entities.VoiceChannel;
|
||||
import net.dv8tion.jda.core.managers.AudioManager;
|
||||
|
||||
public class JulSoundBox {
|
||||
|
||||
private static final String soundFolder = "/var/julia/julsoundbox/sounds/";
|
||||
public static final String soundExtension = ".opus";
|
||||
static AudioPlayerManager playerManager = new DefaultAudioPlayerManager();
|
||||
static AudioManager manager;
|
||||
static VoiceChannel currentChannel = null;
|
||||
static VoiceChannel oldChannel = null;//Si set, JuL'IA retournera dans ce salon dès qu'une chanson finira
|
||||
static BernardSoundHandler soundHandler;
|
||||
static Queue<AudioTrack> playlist;
|
||||
Random randomizator;
|
||||
static boolean play;
|
||||
|
||||
@Command(name = "playing",grp="snd",group="SoundBox", admin = false, description = "Demande à JuL'IA quel son elle est en train de jouer")
|
||||
public void playing(Guild guild, Message message) {
|
||||
if(soundHandler.currentTrack == null && playlist.isEmpty()) {
|
||||
notifyUser("Ben je suis en fait en train de jouer .... rien du tout", message.getAuthor(), message.getChannel());
|
||||
return;
|
||||
}
|
||||
EmbedBuilder builder = new EmbedBuilder();
|
||||
String playing = "";
|
||||
if(soundHandler.currentTrack == null)
|
||||
playing = "Rien du tout !";
|
||||
else
|
||||
playing = soundHandler.currentTrack.getIdentifier();
|
||||
builder.addField("Je joue ", playing, false);
|
||||
|
||||
String queued = "";
|
||||
if(playlist.isEmpty())
|
||||
queued = "Rien du tout !";
|
||||
else {
|
||||
for (AudioTrack audioTrack : playlist)
|
||||
queued += audioTrack.getIdentifier().substring(soundFolder.length()) + ",";
|
||||
queued = queued.substring(0, queued.length()-1);
|
||||
}
|
||||
builder.addField("J'ai prévu ", queued, false);
|
||||
message.getChannel().sendMessage(builder.build()).complete();
|
||||
}
|
||||
|
||||
public static void sneakPlay(String sound,VoiceChannel chan) {
|
||||
oldChannel = currentChannel;
|
||||
manager.openAudioConnection(chan);
|
||||
currentChannel = chan;
|
||||
|
||||
playerManager.loadItem("/var/julia/julsoundbox/sounds/" + sound + soundExtension, new AudioLoadResultHandler() {
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack track) {
|
||||
playlist = appendFirst(track, playlist);
|
||||
play = true;
|
||||
soundHandler.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
System.err.println("Quoikesquevousavezfaitlà ?!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
System.err.println("A moins de transformer le texte en données sonores brutes, je sais pas quoi prononcer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFailed(FriendlyException exception) {
|
||||
System.err.println("Je n'ai pas réussi à charger le son ... désolé (en meme temps, vu comme mes devs codent !)");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Command(name = "come",grp="snd",group="SoundBox", admin = true, description = "Ammène julia dans le salon vocal auquel l'utilisateur est connécté")
|
||||
public void come(Guild guild, Message message, User user) {
|
||||
final GuildVoiceState memberVoiceState = guild.getMember(user).getVoiceState();
|
||||
System.out.println("Coming into channel of voice state "+memberVoiceState);
|
||||
if(!memberVoiceState.inVoiceChannel())
|
||||
manager.closeAudioConnection();
|
||||
else {
|
||||
manager.openAudioConnection(memberVoiceState.getChannel());
|
||||
currentChannel = memberVoiceState.getChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@Command(name = "go",grp="snd",group="SoundBox", admin = true, description = "Ammène julia dans le salon spécifié")
|
||||
public void go(Guild guild, Message message, User user) {
|
||||
String channelName = message.getContentRaw().substring(message.getContentRaw().indexOf("go")+3);
|
||||
System.out.println("Going to channel : "+channelName);
|
||||
switch(channelName) {
|
||||
case "néant":
|
||||
case "rien":
|
||||
case "away":
|
||||
case "null":
|
||||
case "nichts":
|
||||
if(currentChannel != null)
|
||||
manager.closeAudioConnection();
|
||||
currentChannel = null;
|
||||
return;
|
||||
}
|
||||
List<VoiceChannel> chans = guild.getVoiceChannelsByName(channelName,true);
|
||||
if(chans.size() <= 0) {
|
||||
notifyUser("Je ne connais pas le channel '"+channelName+"'", user, message.getChannel());
|
||||
return;
|
||||
}
|
||||
currentChannel = chans.get(0);
|
||||
manager.openAudioConnection(currentChannel);
|
||||
}
|
||||
|
||||
@Command(name = "play",grp="snd",group="SoundBox", admin = true, description = "Reprends ou lance la lecture d'un son")
|
||||
public void play(Guild guild, Message message, User user) {
|
||||
String soundName = null;
|
||||
if(message.getContentRaw().endsWith("play")) {
|
||||
if(soundHandler.currentTrack != null || !playlist.isEmpty()) {
|
||||
soundHandler.update(true);
|
||||
return;
|
||||
}else {
|
||||
System.out.println("Random mode !!!");
|
||||
File[] songsFiles = new File("/var/julia/julsoundbox/sounds/").listFiles();
|
||||
soundName = songsFiles[randomizator.nextInt(songsFiles.length)].getName().substring(0, soundExtension.length()+1);
|
||||
}
|
||||
}else{
|
||||
soundName = message.getContentRaw().substring(message.getContentRaw().indexOf("play")+5);
|
||||
if(!checkUserSoundPermission(user, soundName)){
|
||||
notifyUser("Vous n'avez pas accès à ce morceau", user, null);
|
||||
return;
|
||||
}
|
||||
}
|
||||
System.out.println("Trying to play : "+soundName);
|
||||
playerManager.loadItem("/var/julia/julsoundbox/sounds/" + soundName + soundExtension, new AudioLoadResultHandler() {
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack track) {
|
||||
playlist = appendFirst(track, playlist);
|
||||
play = true;
|
||||
soundHandler.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
notifyUser("Quoikesquevousavezfaitlà ?!", user, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
notifyUser("A moins de transformer le texte en données sonores brutes, je sais pas quoi prononcer", user, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFailed(FriendlyException exception) {
|
||||
notifyUser("Je n'ai pas réussi à charger le son ... désolé (en meme temps, vu comme mes devs codent !)", user, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Command(name = "play2",grp="snd",group="SoundBox", admin = true, description = "Reprends ou lance la lecture d'un son2")
|
||||
public void play2(Guild guild, Message message, User user) {
|
||||
String soundName = message.getContentRaw().substring(message.getContentRaw().indexOf("play2")+6);
|
||||
System.out.println("Trying to play : "+soundName);
|
||||
playerManager.loadItem("/var/julia/julsoundbox/sounds/" + soundName + soundExtension, new AudioLoadResultHandler() {
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack track) {
|
||||
soundHandler.altTrack = track;
|
||||
soundHandler.altPlayer.startTrack(track, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
notifyUser("Quoikesquevousavezfaitlà ?!", user, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
notifyUser("A moins de transformer le texte en données sonores brutes, je sais pas quoi prononcer", user, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFailed(FriendlyException exception) {
|
||||
notifyUser("Je n'ai pas réussi à charger le son ... désolé (en meme temps, vu comme mes devs codent !)", user, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Command(name = "queue",grp="snd",group="SoundBox", admin = true, description = "Ajoute un son à la liste de lecture")
|
||||
public void queue(Guild guild, Message message, User user) {
|
||||
String soundName = null;
|
||||
if(message.getContentRaw().indexOf("queue")+5 == message.getContentRaw().length()) {
|
||||
File[] songsFiles = new File("/var/julia/julsoundbox/sounds/").listFiles();
|
||||
soundName = songsFiles[randomizator.nextInt(songsFiles.length)].getName().substring(0, ".opus".length());
|
||||
}else{
|
||||
soundName = message.getContentRaw().substring(message.getContentRaw().indexOf("queue")+6);
|
||||
if(!checkUserSoundPermission(user, soundName)){
|
||||
notifyUser("Vous n'avez pas accès à ce morceau", user, null);
|
||||
return;
|
||||
}
|
||||
}
|
||||
playerManager.loadItem(soundFolder + soundName + ".opus", new AudioLoadResultHandler() {
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack track) {
|
||||
playlist.add(track);
|
||||
soundHandler.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
notifyUser("Quoikesquevousavezfaitlà ?!", user, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
notifyUser("A moins de transformer le texte en données sonores brutes, je sais pas quoi prononcer", user, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFailed(FriendlyException exception) {
|
||||
notifyUser("Je n'ai pas réussi à charger le son ... désolé (en meme temps, vu comme mes devs codent !)", user, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Command(name = "unqueue",grp="snd",group="SoundBox", admin = true, description = "Enlève un son de la liste de lecture")//TODO test
|
||||
public void unqueue(Guild guild, Message message, User user) {
|
||||
String soundId = message.getContentRaw().substring(message.getContentRaw().indexOf("unqueue")+8);
|
||||
if(soundId.matches("^[0-9]+$")) {
|
||||
Iterator<AudioTrack> it = playlist.iterator();
|
||||
int end = Integer.parseInt(soundId, 10) - 1;//Mathe way
|
||||
int i = 0;
|
||||
AudioTrack track = null;
|
||||
for(;i<end && it.hasNext();i++)
|
||||
track = it.next();
|
||||
if(i==end && track != null)
|
||||
playlist.remove(track);
|
||||
else
|
||||
notifyUser("Il n'y a pas de son dans la playlist au "+(end+1)+"ième élement", user, null);
|
||||
}else {
|
||||
Iterator<AudioTrack> it = playlist.iterator();
|
||||
while(it.hasNext()) {
|
||||
AudioTrack t = it.next();
|
||||
if(t.getUserData().equals(soundId))
|
||||
playlist.remove(t);
|
||||
}
|
||||
}
|
||||
soundHandler.update();
|
||||
}
|
||||
|
||||
@Command(name = "pause",grp="snd",group="SoundBox", admin = true, description = "Met le son joué en pause (wow)")
|
||||
public void pause(Guild guild, Message message, User user) {
|
||||
soundHandler.update(false);
|
||||
}
|
||||
|
||||
@Command(name = "chut",grp="snd",group="SoundBox", admin = true, description = "Arette le son que JuL'IA joue (un !!play reprendra le morceau suivant ")
|
||||
public void chut(Guild guild, Message message, User user) {
|
||||
play = false;
|
||||
soundHandler.next();
|
||||
}
|
||||
|
||||
@Command(name = "soundInit",grp="snd",group="SoundBox", admin = true, description = "Evite que tout le programme ne crashe .... a ne pas oublier (en attendant que tout 'fonctionne' ...)")
|
||||
public void soundInit(Guild guild) {
|
||||
playerManager = new DefaultAudioPlayerManager();
|
||||
soundHandler = new BernardSoundHandler();
|
||||
manager = guild.getAudioManager();
|
||||
AudioSourceManagers.registerLocalSource(playerManager);
|
||||
manager.setSendingHandler(soundHandler);
|
||||
currentChannel = null;
|
||||
playlist = new LinkedList<>();
|
||||
randomizator = new Random();
|
||||
play = false;
|
||||
}
|
||||
|
||||
public class BernardSoundHandler implements AudioSendHandler,AudioEventListener {
|
||||
|
||||
public AudioTrack currentTrack;
|
||||
public AudioTrack altTrack;
|
||||
|
||||
private final AudioPlayer audioPlayer;
|
||||
final AudioPlayer altPlayer;
|
||||
|
||||
public BernardSoundHandler() {
|
||||
audioPlayer = playerManager.createPlayer();
|
||||
altPlayer = playerManager.createPlayer();
|
||||
audioPlayer.addListener(this);
|
||||
altPlayer.addListener(this);
|
||||
}
|
||||
|
||||
public void next() {
|
||||
currentTrack = playlist.poll();
|
||||
audioPlayer.startTrack(currentTrack, false);
|
||||
update();
|
||||
}
|
||||
|
||||
public void update() {
|
||||
audioPlayer.setPaused(!JulSoundBox.play);
|
||||
}
|
||||
|
||||
public void update(boolean play) {
|
||||
JulSoundBox.play = play;
|
||||
update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(AudioEvent event) {
|
||||
if(event instanceof TrackEndEvent) {
|
||||
altTrack = null;
|
||||
if(oldChannel != null) {
|
||||
manager.openAudioConnection(oldChannel);
|
||||
currentChannel = oldChannel;
|
||||
|
||||
}
|
||||
if(playlist.size() > 0) {
|
||||
currentTrack = playlist.poll();
|
||||
audioPlayer.startTrack(currentTrack, false);
|
||||
update();
|
||||
}else {
|
||||
play = false;
|
||||
currentTrack = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private AudioFrame lastFrame;
|
||||
/////////// Methods for sound playing ///////////
|
||||
@Override
|
||||
public boolean canProvide() {
|
||||
if(altTrack != null) {
|
||||
AudioFrame f1 = audioPlayer.provide();
|
||||
AudioFrame f2 = altPlayer.provide();
|
||||
lastFrame = (Math.random() > 0.5)?f1:f2;
|
||||
}else
|
||||
lastFrame = audioPlayer.provide();
|
||||
return lastFrame != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] provide20MsAudio() {
|
||||
return lastFrame.data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpus() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final boolean checkUserSoundPermission(User user,String sound) {
|
||||
if(sound.indexOf("/") == -1)
|
||||
return true;
|
||||
File soundFile = new File(soundFolder,sound+".opus");
|
||||
if(!soundFile.exists())
|
||||
return true;//will be catched later, returning false will cause the 'not permitted' message appear
|
||||
File lockFile = new File(soundFile.getParent(),"lock");//TODO Change with BernardFilesAPI
|
||||
if(!lockFile.exists())
|
||||
return true;//No lock file : the folder is open
|
||||
try {
|
||||
Scanner sc = new Scanner(new FileInputStream(lockFile));
|
||||
while(sc.hasNextLine())
|
||||
if(user.getId().equals(sc.nextLine())) {
|
||||
sc.close();
|
||||
return true;
|
||||
}
|
||||
sc.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
System.err.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
|
||||
System.err.println("No right to access to "+lockFile.getAbsolutePath());
|
||||
e.printStackTrace();
|
||||
System.err.println("\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//////////// Useful static functions ///////////////
|
||||
|
||||
public static final void notifyUser(String problem, User user, MessageChannel channel) {
|
||||
MessageBuilder builder = new MessageBuilder();
|
||||
builder.append(user);
|
||||
builder.append(" : ");
|
||||
builder.append(problem);
|
||||
user.openPrivateChannel().queue((chan) -> {
|
||||
chan.sendMessage(builder.build()).queue();
|
||||
});
|
||||
}
|
||||
|
||||
public static final <T> Queue<T> appendFirst(T element,Queue<T> queue){
|
||||
Queue<T> out = new LinkedList<T>();
|
||||
out.add(element);
|
||||
for (T t : queue) {
|
||||
out.add(t);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user