Adding Music !

This commit is contained in:
BrokenFire 2017-10-22 23:48:13 +02:00
parent 04040925bf
commit ffb8fe914b
35 changed files with 717 additions and 122 deletions

2
.gitignore vendored
View File

@ -6,3 +6,5 @@ out/
*.iml *.iml
META-INF/ META-INF/
*.uml *.uml
*.class

View File

@ -1 +1 @@
worker: java -Dlog4j.configurationFile=log4j2.xml -jar Discord_Stroumpf_Beta.jar -t MjQwODA2MDk1NzcyMTg4Njc0.CvIrrg.p3lOx7oXabPWcT1oZiPKcu0uSs4 worker: java -Dlog4j.configurationFile=log4j2.xml -jar Discord_Stroumpf_Beta.jar -t MzY5NDQ1MzY1NDc0MjYzMDUx.DM6e_g.JUG_P9NfH1QHpbgtZf50xYF1RJ8

40
pom.xml Normal file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>central</groupId>
<artifactId>Discord_Stroumpf_Beta</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.sedmelluq</groupId>
<artifactId>lavaplayer</artifactId>
<version>1.2.42</version>
</dependency>
</dependencies>
</project>

View File

@ -45,7 +45,7 @@ public class Help implements Commande {
}}; }};
new MessageTimeOut(messages,MainBot.messageTimeOut).start(); new MessageTimeOut(messages,MainBot.messageTimeOut).start();
} else{ } else{
PrivateMessage.send(event.getAuthor(), MainBot.commandes.get(argsString).help(args),logger); PrivateMessage.send(event.getAuthor(), EmbedMessageUtils.getHelp(argsString, MainBot.commandes.get(argsString).help(args)),logger);
} }

View File

@ -0,0 +1,131 @@
package net.Broken.Commandes;
import net.Broken.Commande;
import net.Broken.MainBot;
import net.Broken.Outils.EmbedMessageUtils;
import net.Broken.Outils.MessageTimeOut;
import net.Broken.audio.AudioM;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import java.util.ArrayList;
import java.util.List;
public class Music implements Commande {
AudioM audio;
public Music() {
audio = new AudioM();
}
@Override
public boolean called(String[] args, MessageReceivedEvent event) {
return false;
}
@Override
public void action(String[] args, MessageReceivedEvent event) {
if(args.length >= 1){
switch (args[0]){
case "play":
event.getTextChannel().sendTyping().queue();
if(args.length>=3){
List<VoiceChannel> chanels = event.getGuild().getVoiceChannelsByName(args[1], true);
if(chanels.size() >= 1){
VoiceChannel chanel = chanels.get(0);
audio.loadAndPlay(event,chanel,args[2]);
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Chanel introuvable!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments manquant!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
break;
case "pause":
audio.pause(event);
break;
case "resume":
audio.resume(event);
break;
case "next":
audio.skipTrack(event);
break;
case "stop":
audio.stop(event);
event.getGuild().getAudioManager().closeAudioConnection();
break;
case "info":
audio.info(event);
break;
case "flush":
audio.flush(event);
break;
case "list":
audio.list(event);
break;
case "add":
event.getTextChannel().sendTyping().queue();
if(args.length>=2){
audio.add(event,args[1]);
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments manquant!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
break;
default:
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments inconu!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
break;
}
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments manquant!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
}
@Override
public String help(String[] args) {
return "`//music play <ChanelVocal> <url>`\n:arrow_right:\t*Let's dance!.*\n\n`//music pause`\n:arrow_right:\t*Mise en pause de la piste en cour.*\n\n`//music resume`\n:arrow_right:\t*Reprise de la lecture de la piste en cour.*\n\n`//music next`\n:arrow_right:\t*Change le piste en cour.*\n\n`//music stop`\n:arrow_right:\t*Arrete la piste ne cour.*\n\n`//music info`\n:arrow_right:\t*Affiche les infos de la piste ne cour.*\n\n`//music flush`\n:arrow_right:\t*Suprime la playlist en cour.*\n\n`//music list`\n:arrow_right:\t*Affiche la playlist en cour.*\n\n`//music add <url>`\n:arrow_right:\t*Ajoute l'url à la playlist en cour.*";
}
@Override
public void executed(boolean success, MessageReceivedEvent event) {
}
@Override
public boolean isPrivateUsable() {
return false;
}
}

View File

@ -6,6 +6,7 @@ import net.Broken.Outils.CommandParser;
import net.Broken.Outils.DayListener; import net.Broken.Outils.DayListener;
import net.Broken.Outils.EmbedMessageUtils; import net.Broken.Outils.EmbedMessageUtils;
import net.Broken.Outils.UserSpamUtils; import net.Broken.Outils.UserSpamUtils;
import net.Broken.audio.AudioM;
import net.dv8tion.jda.core.*; import net.dv8tion.jda.core.*;
import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
@ -107,6 +108,7 @@ public class MainBot {
commandes.put("spam", new Spam()); commandes.put("spam", new Spam());
commandes.put("spaminfo",new SpamInfo()); commandes.put("spaminfo",new SpamInfo());
commandes.put("flush",new Flush()); commandes.put("flush",new Flush());
commandes.put("music",new Music());
if(!dev){ if(!dev){
commandes.put("ass",new Ass()); commandes.put("ass",new Ass());
@ -125,6 +127,7 @@ public class MainBot {
//On recupere le l'id serveur //On recupere le l'id serveur
Guild serveur = jda.getGuilds().get(0); Guild serveur = jda.getGuilds().get(0);
//On recupere le serveur manageur //On recupere le serveur manageur
GuildManager guildManager = serveur.getManager(); GuildManager guildManager = serveur.getManager();

View File

@ -19,6 +19,15 @@ public class EmbedMessageUtils {
} }
public static MessageEmbed getMusicError(String mesage){
return new EmbedBuilder().setTitle(":warning: Musique Error :warning:").setDescription(":arrow_right: "+mesage).setFooter("'//help music' pour plus d'info",null).setColor(Color.red).build();
}
public static MessageEmbed getMusicOk(String message) {
return new EmbedBuilder().setTitle(":loud_sound: Music :loud_sound:").setDescription(message).setColor(Color.green).setFooter("'//help music' pour plus d'info ", null).build();
}
public static MessageEmbed getSpamExtermine(Member autor, int multi) { public static MessageEmbed getSpamExtermine(Member autor, int multi) {
return new EmbedBuilder().setTitle(":mute: Spam Hunter :mute:").setDescription(autor.getAsMention() + " détecté comme spammer !\n\nOn te revoit dans __**" + multi + "**__ min!").setImage("https://media.giphy.com/media/WVudyGEaizNeg/giphy.gif").setFooter("Spam info disponible via '//spaminfo' en privé", null).setColor(Color.orange).build(); return new EmbedBuilder().setTitle(":mute: Spam Hunter :mute:").setDescription(autor.getAsMention() + " détecté comme spammer !\n\nOn te revoit dans __**" + multi + "**__ min!").setImage("https://media.giphy.com/media/WVudyGEaizNeg/giphy.gif").setFooter("Spam info disponible via '//spaminfo' en privé", null).setColor(Color.orange).build();
} }

View File

@ -0,0 +1,229 @@
package net.Broken.audio;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
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.AudioTrackInfo;
import net.Broken.MainBot;
import net.Broken.Outils.EmbedMessageUtils;
import net.Broken.Outils.MessageTimeOut;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.managers.AudioManager;
import java.util.ArrayList;
import java.util.List;
public class AudioM {
GuildMusicManager musicManager;
AudioPlayerManager playerManager;
VoiceChannel playedChanel;
int listTimeOut = 30;
public AudioM() {
this.playerManager = new DefaultAudioPlayerManager();
AudioSourceManagers.registerRemoteSources(playerManager);
AudioSourceManagers.registerLocalSource(playerManager);
}
public void loadAndPlay(MessageReceivedEvent event, VoiceChannel voiceChannel, final String trackUrl) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
playedChanel = voiceChannel;
playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack track) {
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Ajout de "+track.getInfo().title+" à la file d'attente!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
play(event.getGuild(), voiceChannel, musicManager, track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
AudioTrack firstTrack = playlist.getSelectedTrack();
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Ajout de "+firstTrack.getInfo().title+" et les 30 premiers titres à la file d'attente!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
int i = 0;
for(AudioTrack track : playlist.getTracks()){
play(event.getGuild(), voiceChannel, musicManager, track);
i++;
if(i>30)
break;
}
}
@Override
public void noMatches() {
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Musique introuvable!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
@Override
public void loadFailed(FriendlyException exception) {
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Erreur de lecture!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
});
}
private GuildMusicManager getGuildAudioPlayer(Guild guild) {
if (musicManager == null) {
musicManager = new GuildMusicManager(playerManager);
}
guild.getAudioManager().setSendingHandler(musicManager.getSendHandler());
return musicManager;
}
private void play(Guild guild, VoiceChannel channel, GuildMusicManager musicManager, AudioTrack track) {
guild.getAudioManager().openAudioConnection(channel);
musicManager.scheduler.queue(track);
}
public void skipTrack(MessageReceivedEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.nextTrack();
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Musique suivante!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
public void pause(MessageReceivedEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.pause();
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Musique en pause !")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
public void resume (MessageReceivedEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.resume();
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Reprise de la piste en cour !")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
public void info(MessageReceivedEvent event){
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
AudioTrackInfo info = musicManager.scheduler.getInfo();
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk(info.title+"\n"+info.uri)).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
public void flush(MessageReceivedEvent event){
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.flush();
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("RAZ de la playlist!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
public void list(MessageReceivedEvent event){
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
List<AudioTrackInfo> list = musicManager.scheduler.getList();
StringBuilder resp = new StringBuilder();
if(list.size() == 0){
resp.append("Oh mon dieux!\nElle est vide! \n:astonished: ");
}
else
{
for(AudioTrackInfo trackInfo : list){
resp.append("- ");
resp.append(trackInfo.title);
resp.append("\n");
}
}
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Playlist:\n\n"+resp.toString())).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, listTimeOut).run();
}
public void add(MessageReceivedEvent event,String url) {
if(playedChanel != null){
loadAndPlay(event,playedChanel, url);
}
else
{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Aucune lecture en cour!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
}
public void stop (MessageReceivedEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.stop();
playedChanel = null;
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Arret de la musique!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
}

View File

@ -0,0 +1,50 @@
package net.Broken.audio;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
import net.dv8tion.jda.core.audio.AudioSendHandler;
/**
* This is a wrapper around AudioPlayer which makes it behave as an AudioSendHandler for JDA. As JDA calls canProvide
* before every call to provide20MsAudio(), we pull the frame in canProvide() and use the frame we already pulled in
* provide20MsAudio().
*/
public class AudioPlayerSendHandler implements AudioSendHandler {
private final AudioPlayer audioPlayer;
private AudioFrame lastFrame;
/**
* @param audioPlayer Audio player to wrap.
*/
public AudioPlayerSendHandler(AudioPlayer audioPlayer) {
this.audioPlayer = audioPlayer;
}
@Override
public boolean canProvide() {
if (lastFrame == null) {
lastFrame = audioPlayer.provide();
}
return lastFrame != null;
}
@Override
public byte[] provide20MsAudio() {
if (lastFrame == null) {
lastFrame = audioPlayer.provide();
}
byte[] data = lastFrame != null ? lastFrame.data : null;
lastFrame = null;
return data;
}
@Override
public boolean isOpus() {
return true;
}
}

View File

@ -0,0 +1,35 @@
package net.Broken.audio;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
/**
* Holder for both the player and a track scheduler for one guild.
*/
public class GuildMusicManager {
/**
* Audio player for the guild.
*/
public final AudioPlayer player;
/**
* Track scheduler for the player.
*/
public final TrackScheduler scheduler;
/**
* Creates a player and a track scheduler.
* @param manager Audio player manager to use for creating the player.
*/
public GuildMusicManager(AudioPlayerManager manager) {
player = manager.createPlayer();
scheduler = new TrackScheduler(player);
player.addListener(scheduler);
}
/**
* @return Wrapper around AudioPlayer to use it as an AudioSendHandler.
*/
public AudioPlayerSendHandler getSendHandler() {
return new AudioPlayerSendHandler(player);
}
}

View File

@ -0,0 +1,96 @@
package net.Broken.audio;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* This class schedules tracks for the audio player. It contains the queue of tracks.
*/
public class TrackScheduler extends AudioEventAdapter {
private final AudioPlayer player;
private final BlockingQueue<AudioTrack> queue;
/**
* @param player The audio player this scheduler uses
*/
public TrackScheduler(AudioPlayer player) {
this.player = player;
player.setVolume(25);
this.queue = new LinkedBlockingQueue<>();
}
/**
* Add the next track to queue or play right away if nothing is in the queue.
*
* @param track The track to play or add to queue.
*/
public void queue(AudioTrack track) {
// Calling startTrack with the noInterrupt set to true will start the track only if nothing is currently playing. If
// something is playing, it returns false and does nothing. In that case the player was already playing so this
// track goes to the queue instead.
if (!player.startTrack(track, true)) {
queue.offer(track);
}
}
public void pause() {
player.setPaused(true);
}
public void resume() {
player.setPaused(false);
}
public void stop(){
player.stopTrack();
player.destroy();
}
public void flush(){
queue.clear();
}
public List<AudioTrackInfo> getList(){
// AudioTrack[] test = (AudioTrack[]) queue.toArray();
List<AudioTrackInfo> temp = new ArrayList<>();
Object[] test = queue.toArray();
for(Object track: test){
AudioTrack casted = (AudioTrack) track;
temp.add(casted.getInfo());
}
return temp;
}
public AudioTrackInfo getInfo(){
return player.getPlayingTrack().getInfo();
}
/**
* Start the next track, stopping the current one if it is playing.
*/
public void nextTrack() {
// Start the next track, regardless of if something is already playing or not. In case queue was empty, we are
// giving null to startTrack, which is a valid argument and will simply stop the player.
player.startTrack(queue.poll(), false);
}
@Override
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
// Only start the next track if the end reason is suitable for it (FINISHED or LOAD_FAILED)
if (endReason.mayStartNext) {
nextTrack();
}
}
}