From 11f92f4542b3bfe969a65ef98ebc4c6427a779a6 Mon Sep 17 00:00:00 2001 From: Skydust Date: Sat, 8 May 2021 05:17:01 +0200 Subject: [PATCH] Quelques changements. La commande record fonctionne maintenant --- .idea/.gitignore | 3 + .idea/JdrBot.iml | 9 ++ .idea/compiler.xml | 6 + .idea/gradle.xml | 17 ++ .idea/jarRepositories.xml | 25 +++ .idea/libraries-with-intellij-classes.xml | 65 ++++++++ .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/modules/JdrBot.iml | 2 + .idea/thriftCompiler.xml | 6 + .idea/uiDesigner.xml | 124 ++++++++++++++ .idea/vcs.xml | 6 + gradle/wrapper/gradle-wrapper.properties | 2 +- src/main/java/fr/Skydust/JdrBot/JdrBot.java | 19 ++- .../fr/Skydust/JdrBot/JdrBotListener.java | 9 +- .../JdrBot/audio/handler/JdrBotARH.java | 13 +- .../java/fr/Skydust/JdrBot/cmds/Aide.java | 6 +- .../Skydust/JdrBot/cmds/LastTimeOnline.java | 3 +- .../java/fr/Skydust/JdrBot/cmds/Roll.java | 3 +- .../Skydust/JdrBot/cmds/playmusic/FLoad.java | 18 ++- .../JdrBot/cmds/playmusic/FLoadLoop.java | 18 ++- .../JdrBot/cmds/playmusic/PlayMusic.java | 32 ---- .../JdrBot/cmds/playmusic/StopMusic.java | 42 +++-- .../JdrBot/cmds/record/CancelRecord.java | 25 +++ .../fr/Skydust/JdrBot/cmds/record/Record.java | 51 ++++-- .../JdrBot/cmds/record/StopRecord.java | 11 +- .../java/fr/Skydust/JdrBot/stock/Command.java | 12 +- .../fr/Skydust/JdrBot/stock/RecordState.java | 151 ++++++++++-------- .../JdrBot/utils/ByteArrayOutputStreamT.java | 10 -- .../fr/Skydust/JdrBot/utils/MessageType.java | 6 + .../java/fr/Skydust/JdrBot/utils/Utils.java | 32 ++++ 31 files changed, 573 insertions(+), 168 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/JdrBot.iml create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries-with-intellij-classes.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/modules/JdrBot.iml create mode 100644 .idea/thriftCompiler.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100755 src/main/java/fr/Skydust/JdrBot/cmds/record/CancelRecord.java delete mode 100755 src/main/java/fr/Skydust/JdrBot/utils/ByteArrayOutputStreamT.java create mode 100644 src/main/java/fr/Skydust/JdrBot/utils/MessageType.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/JdrBot.iml b/.idea/JdrBot.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/JdrBot.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..611e7c8 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..b3e9cbd --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries-with-intellij-classes.xml b/.idea/libraries-with-intellij-classes.xml new file mode 100644 index 0000000..9fa3156 --- /dev/null +++ b/.idea/libraries-with-intellij-classes.xml @@ -0,0 +1,65 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..62269df --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..be03264 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/JdrBot.iml b/.idea/modules/JdrBot.iml new file mode 100644 index 0000000..cc815a6 --- /dev/null +++ b/.idea/modules/JdrBot.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/thriftCompiler.xml b/.idea/thriftCompiler.xml new file mode 100644 index 0000000..7bc123c --- /dev/null +++ b/.idea/thriftCompiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index be52383..442d913 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/fr/Skydust/JdrBot/JdrBot.java b/src/main/java/fr/Skydust/JdrBot/JdrBot.java index 97d7c34..a89deac 100755 --- a/src/main/java/fr/Skydust/JdrBot/JdrBot.java +++ b/src/main/java/fr/Skydust/JdrBot/JdrBot.java @@ -7,15 +7,19 @@ import javax.security.auth.login.LoginException; import fr.Skydust.JdrBot.cmds.*; import fr.Skydust.JdrBot.cmds.playmusic.*; +import fr.Skydust.JdrBot.cmds.record.CancelRecord; +import fr.Skydust.JdrBot.cmds.record.Record; +import fr.Skydust.JdrBot.cmds.record.StopRecord; import fr.Skydust.JdrBot.stock.Command; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.utils.cache.CacheFlag; public class JdrBot { - public static String Version = "2.4"; + public static String Version = "2.6"; - static JDA jda; + public static JDA jda; public static LocalDateTime basedate; public static List commandList; @@ -24,17 +28,18 @@ public class JdrBot { public static void main(String args[]) { try { basedate = LocalDateTime.now(); - jda = JDABuilder.createDefault("MTY5OTMzMzgxMDMzOTE4NDY0.DerlJg.m7BdNv_OMHlYa-f4T3O0jJ9LldM").build(); + jda = JDABuilder.createDefault("MTY5OTMzMzgxMDMzOTE4NDY0.DerlJg.m7BdNv_OMHlYa-f4T3O0jJ9LldM") + .setActivity(Activity.playing("un jeu de rôle")) + .enableCache(CacheFlag.VOICE_STATE).build(); jda.awaitReady(); - jda.getPresence().setActivity(Activity.playing("un jeu de rôle")); - commandList = new ArrayList(); registerCommand(new Aide()); registerCommand(new Etat()); registerCommand(new Roll()); - //registerCommand(new Record()); - //registerCommand(new StopRecord()); + registerCommand(new Record()); + registerCommand(new StopRecord()); + registerCommand(new CancelRecord()); registerCommand(new Emote()); registerCommand(new PlayMusic()); registerCommand(new StopMusic()); diff --git a/src/main/java/fr/Skydust/JdrBot/JdrBotListener.java b/src/main/java/fr/Skydust/JdrBot/JdrBotListener.java index d0a9a1a..8c1ee33 100755 --- a/src/main/java/fr/Skydust/JdrBot/JdrBotListener.java +++ b/src/main/java/fr/Skydust/JdrBot/JdrBotListener.java @@ -4,6 +4,7 @@ import java.util.Random; import fr.Skydust.JdrBot.cmds.LastTimeOnline; import fr.Skydust.JdrBot.cmds.playmusic.PlayMusic; +import fr.Skydust.JdrBot.cmds.playmusic.StopMusic; import fr.Skydust.JdrBot.jukebox.JukeboxGUISystem; import fr.Skydust.JdrBot.stock.Command; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent; @@ -19,8 +20,6 @@ public class JdrBotListener extends ListenerAdapter { @Override public void onGuildMessageReceived(GuildMessageReceivedEvent e) { - System.out.println(e.getMessage().getAuthor().getName() + ": " + e.getMessage().getContentRaw()); - // IF it doesn't start with the startcmdchar, ignore for optimization if (!e.getMessage().getContentRaw().matches("^("+ JdrBot.startcmdchar+").*$") || e.getMessage().getAuthor().isBot()) return; @@ -45,16 +44,16 @@ public class JdrBotListener extends ListenerAdapter { } @Override public void onGuildVoiceMove(GuildVoiceMoveEvent e) { - PlayMusic.onGuildVoiceMove(e); + StopMusic.onGuildVoiceMove(e); } @Override public void onGuildVoiceLeave(GuildVoiceLeaveEvent e) { - PlayMusic.onGuildVoiceLeave(e); + StopMusic.onGuildVoiceLeave(e); } @Override public void onMessageReactionAdd(MessageReactionAddEvent e) { JukeboxGUISystem.onMessageReactionAdd(e); } public boolean isACommand(String message, String cmd) { - return message.matches("^("+ JdrBot.startcmdchar+")("+cmd+").*$"); + return message.matches("^("+ JdrBot.startcmdchar +")("+ cmd +")( .*|)$"); } } \ No newline at end of file diff --git a/src/main/java/fr/Skydust/JdrBot/audio/handler/JdrBotARH.java b/src/main/java/fr/Skydust/JdrBot/audio/handler/JdrBotARH.java index b7ae13f..c0d3b19 100755 --- a/src/main/java/fr/Skydust/JdrBot/audio/handler/JdrBotARH.java +++ b/src/main/java/fr/Skydust/JdrBot/audio/handler/JdrBotARH.java @@ -1,14 +1,19 @@ package fr.Skydust.JdrBot.audio.handler; +import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; +import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame; import fr.Skydust.JdrBot.cmds.record.Record; +import fr.Skydust.JdrBot.stock.RecordState; import net.dv8tion.jda.api.audio.AudioReceiveHandler; import net.dv8tion.jda.api.audio.CombinedAudio; import net.dv8tion.jda.api.audio.UserAudio; public class JdrBotARH implements AudioReceiveHandler { + private final RecordState recordState; + String guild; - public JdrBotARH(String guild) { - this.guild = guild; + public JdrBotARH(RecordState rs) { + this.recordState = rs; } @Override @@ -23,8 +28,8 @@ public class JdrBotARH implements AudioReceiveHandler { @Override public void handleCombinedAudio(CombinedAudio arg0) { - if(Record.rbs.get(guild) != null) { - Record.rbs.get(guild).addBytes(arg0.getAudioData(1.0)); + if(recordState.isRecording) { + recordState.addBytes(arg0.getAudioData(1.0)); } } diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/Aide.java b/src/main/java/fr/Skydust/JdrBot/cmds/Aide.java index ba1c405..3c0e095 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/Aide.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/Aide.java @@ -20,10 +20,12 @@ public class Aide extends Command { public void call(GuildMessageReceivedEvent e) { StringBuilder sb = new StringBuilder(); - for (int i = 0; i< JdrBot.commandList.size(); i++) + for (int i = 0; i < JdrBot.commandList.size(); i++) { Command cmd = JdrBot.commandList.get(i); - sb.append("`"+cmd.cmdName+"` - *"+cmd.cmdDesc+"*"+ ((i != JdrBot.commandList.size()-1) ? "\n" : "")); + if(!cmd.hidden) { + sb.append("`"+cmd.cmdName + ((cmd.cmdUsage.equals("")) ? "" : " "+cmd.cmdUsage) +"` - *"+cmd.cmdDesc+"*"+ ((i != JdrBot.commandList.size()-1) ? "\n" : "")); + } } e.getChannel().sendMessage(new MessageBuilder().setEmbed(new EmbedBuilder() diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/LastTimeOnline.java b/src/main/java/fr/Skydust/JdrBot/cmds/LastTimeOnline.java index 3cbebb6..7667223 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/LastTimeOnline.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/LastTimeOnline.java @@ -16,7 +16,8 @@ public class LastTimeOnline extends Command { public LastTimeOnline() { SetName("lasttimeonline|lto"); - SetDesc("Dis depuis combien de temps un utilisateur est en ligne/hors-ligne"); + SetDesc("Dit depuis combien de temps un utilisateur est en ligne/hors-ligne"); + SetUsage("[Mention]"); } @Override diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/Roll.java b/src/main/java/fr/Skydust/JdrBot/cmds/Roll.java index 10c2fa2..198428e 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/Roll.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/Roll.java @@ -11,7 +11,8 @@ public class Roll extends Command { public Roll() { SetName("r|roll"); - SetDesc("Lance un dé (USAGE:roll [NombreDés]d[NombreFaces] OU roll [NombreFaces]"); + SetDesc("Lance un dé"); + SetUsage("[NombreDés]d[NombreFaces] OU [NombreFaces]"); } public void call(GuildMessageReceivedEvent e) { diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/FLoad.java b/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/FLoad.java index 32f69a6..8788056 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/FLoad.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/FLoad.java @@ -1,6 +1,8 @@ package fr.Skydust.JdrBot.cmds.playmusic; import fr.Skydust.JdrBot.stock.Command; +import fr.Skydust.JdrBot.utils.MessageType; +import fr.Skydust.JdrBot.utils.Utils; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import org.apache.commons.lang3.StringUtils; @@ -10,15 +12,23 @@ public class FLoad extends Command { public FLoad() { SetName("fload"); SetDesc("/!\\ Commande test /!\\ Permet de charger une musique de force"); + SetUsage("[URL/Recherche]"); + Hide(); } @Override public void call(GuildMessageReceivedEvent e) { - String args = StringUtils.join(Arrays.copyOfRange(e.getMessage().getContentRaw().split(" "), 1, e.getMessage().getContentRaw().split(" ").length)," "); + if(e.getGuild().getMember(e.getAuthor()).getVoiceState().getChannel() != null) { + String args = StringUtils.join(Arrays.copyOfRange(e.getMessage().getContentRaw().split(" "), 1, e.getMessage().getContentRaw().split(" ").length), " "); - e.getGuild().getAudioManager().openAudioConnection(e.getMember().getVoiceState().getChannel()); - //ytsearch:query + e.getGuild().getAudioManager().openAudioConnection(e.getMember().getVoiceState().getChannel()); + //ytsearch:query - PlayMusic.loadAndPlay(e.getChannel(), args, false, false); + PlayMusic.loadAndPlay(e.getChannel(), args, false, false); + + Utils.sendMessage(e.getChannel(), MessageType.NORMAL, "Forçage du lancement de "+args); + } else { + Utils.sendMessage(e.getChannel(), MessageType.ERROR, "Vous n'êtes dans un salon vocal"); + } } } diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/FLoadLoop.java b/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/FLoadLoop.java index e37ec89..522c05d 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/FLoadLoop.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/FLoadLoop.java @@ -1,6 +1,8 @@ package fr.Skydust.JdrBot.cmds.playmusic; import fr.Skydust.JdrBot.stock.Command; +import fr.Skydust.JdrBot.utils.MessageType; +import fr.Skydust.JdrBot.utils.Utils; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import org.apache.commons.lang3.StringUtils; @@ -10,17 +12,23 @@ public class FLoadLoop extends Command { public FLoadLoop() { SetName("floadloop"); SetDesc("/!\\ Commande test /!\\ Permet de charger une musique de force"); + SetUsage("[URL/Recherche]"); + Hide(); } @Override public void call(GuildMessageReceivedEvent e) { - String args = StringUtils.join(Arrays.copyOfRange(e.getMessage().getContentRaw().split(" "), 1, e.getMessage().getContentRaw().split(" ").length)," "); + if(e.getGuild().getMember(e.getAuthor()).getVoiceState().getChannel() != null) { + String args = StringUtils.join(Arrays.copyOfRange(e.getMessage().getContentRaw().split(" "), 1, e.getMessage().getContentRaw().split(" ").length)," "); - e.getGuild().getAudioManager().openAudioConnection(e.getMember().getVoiceState().getChannel()); + e.getGuild().getAudioManager().openAudioConnection(e.getMember().getVoiceState().getChannel()); - if(!args.matches(".*(https?)://.*")) { - args = "ytsearch:"+args; + if(!args.matches(".*(https?)://.*")) { + args = "ytsearch:"+args; + } + PlayMusic.loadAndPlay(e.getChannel(), StringUtils.join(args," "), true, false); + } else { + Utils.sendMessage(e.getChannel(), MessageType.ERROR, "Vous n'êtes dans un salon vocal"); } - PlayMusic.loadAndPlay(e.getChannel(), StringUtils.join(args," "), true, false); } } \ No newline at end of file diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/PlayMusic.java b/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/PlayMusic.java index 2b406bf..f753540 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/PlayMusic.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/PlayMusic.java @@ -58,38 +58,6 @@ public class PlayMusic extends Command { } - public static void onGuildVoiceLeave(GuildVoiceLeaveEvent e) { - if(e.getGuild().getAudioManager().isConnected() && e.getChannelLeft().getName().equals(e.getGuild().getAudioManager().getConnectedChannel().getName()) && e.getChannelLeft().getMembers().size() == 1) { - JukeboxGUI gui = JukeboxGUISystem.getGuildsJukebox(e.getGuild()); - if(gui != null) { - e.getGuild().getTextChannelById(gui.TextChannelID).deleteMessageById(gui.MessageID) - .queue(msg -> { - JukeboxGUISystem.setGuildsJukebox(e.getGuild(), null); - }); - } - getGuildAudioPlayer(e.getGuild()).scheduler.stop(); - if(Record.rbs.get(e.getGuild().getId()) == null) { - e.getGuild().getAudioManager().closeAudioConnection(); - } - } - } - - public static void onGuildVoiceMove(GuildVoiceMoveEvent e) { - if(e.getGuild().getAudioManager().isConnected() && e.getChannelLeft().getName().equals(e.getGuild().getAudioManager().getConnectedChannel().getName()) && e.getChannelLeft().getMembers().size() == 1) { - JukeboxGUI gui = JukeboxGUISystem.getGuildsJukebox(e.getGuild()); - if(gui != null) { - e.getGuild().getTextChannelById(gui.TextChannelID).deleteMessageById(gui.MessageID) - .queue(msg -> { - JukeboxGUISystem.setGuildsJukebox(e.getGuild(), null); - }); - } - getGuildAudioPlayer(e.getGuild()).scheduler.stop(); - //if(Record.rbs.get(e.getGuild().getId()) == null) { - e.getGuild().getAudioManager().closeAudioConnection(); - //} - } - } - public static void loadAndPlay(final TextChannel channel, final String trackUrl, boolean loop, boolean nomsg) { System.out.println(trackUrl); GuildMusicManager musicManager = getGuildAudioPlayer(channel.getGuild()); diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/StopMusic.java b/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/StopMusic.java index f139f0e..de148d9 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/StopMusic.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/playmusic/StopMusic.java @@ -5,6 +5,9 @@ import fr.Skydust.JdrBot.jukebox.JukeboxGUI; import fr.Skydust.JdrBot.jukebox.JukeboxGUISystem; import fr.Skydust.JdrBot.stock.Command; import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent; +import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class StopMusic extends Command { @@ -15,21 +18,38 @@ public class StopMusic extends Command { @Override public void call(GuildMessageReceivedEvent e) { - if(!e.getGuild().getAudioManager().isConnected() && e.getMember().hasPermission(Permission.ADMINISTRATOR)) { + if(!e.getGuild().getAudioManager().isConnected() || !e.getMember().hasPermission(Permission.ADMINISTRATOR)) { return; } - JukeboxGUI gui = JukeboxGUISystem.getGuildsJukebox(e.getGuild()); + stopMusic(e.getGuild()); + } + + public static void onGuildVoiceLeave(GuildVoiceLeaveEvent e) { + if(e.getGuild().getAudioManager().isConnected() && e.getChannelLeft().getName().equals(e.getGuild().getAudioManager().getConnectedChannel().getName()) && e.getChannelLeft().getMembers().size() == 1) { + stopMusic(e.getGuild()); + } + } + + public static void onGuildVoiceMove(GuildVoiceMoveEvent e) { + if(e.getGuild().getAudioManager().isConnected() && e.getChannelLeft().getName().equals(e.getGuild().getAudioManager().getConnectedChannel().getName()) && e.getChannelLeft().getMembers().size() == 1) { + stopMusic(e.getGuild()); + } + } + + public static void stopMusic(Guild g) { + JukeboxGUI gui = JukeboxGUISystem.getGuildsJukebox(g); if(gui != null) { - e.getGuild().getTextChannelById(gui.TextChannelID).deleteMessageById(gui.MessageID) - .queue(msg -> { - JukeboxGUISystem.setGuildsJukebox(e.getGuild(), null); - }); - } - PlayMusic.getGuildAudioPlayer(e.getGuild()).scheduler.stop(); - if(Record.rbs.get(e.getGuild().getId()) == null) { - e.getGuild().getAudioManager().closeAudioConnection(); - } + g.getTextChannelById(gui.TextChannelID).deleteMessageById(gui.MessageID) + .queue(msg -> { + JukeboxGUISystem.setGuildsJukebox(g, null); + }); + } + PlayMusic.getGuildAudioPlayer(g).scheduler.stop(); + + if(!Record.getGuildRecordState(g).isRecording) { + g.getAudioManager().closeAudioConnection(); + } } } diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/record/CancelRecord.java b/src/main/java/fr/Skydust/JdrBot/cmds/record/CancelRecord.java new file mode 100755 index 0000000..fce599d --- /dev/null +++ b/src/main/java/fr/Skydust/JdrBot/cmds/record/CancelRecord.java @@ -0,0 +1,25 @@ +package fr.Skydust.JdrBot.cmds.record; + +import fr.Skydust.JdrBot.stock.Command; +import fr.Skydust.JdrBot.stock.RecordState; +import fr.Skydust.JdrBot.utils.MessageType; +import fr.Skydust.JdrBot.utils.Utils; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; + +public class CancelRecord extends Command { + public CancelRecord() { + SetName("crecord|cr"); + SetDesc("Annule l'enregistrement"); + } + + @Override + public void call(GuildMessageReceivedEvent e) { + RecordState rs = Record.getGuildRecordState(e.getGuild()); + if(rs.isRecording) { + rs.cancelRecord(e.getGuild()); + Utils.sendMessage(e.getChannel(), MessageType.NORMAL, "L'enregistrement a été annulé"); + } else { + Utils.sendMessage(e.getChannel(), MessageType.ERROR, "Il n'y a aucun enregistrement en cours"); + } + } +} diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/record/Record.java b/src/main/java/fr/Skydust/JdrBot/cmds/record/Record.java index b19cfa0..bd66412 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/record/Record.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/record/Record.java @@ -3,36 +3,57 @@ package fr.Skydust.JdrBot.cmds.record; import java.util.Date; import java.util.HashMap; +import fr.Skydust.JdrBot.audio.GuildMusicManager; import fr.Skydust.JdrBot.audio.handler.JdrBotARH; import fr.Skydust.JdrBot.stock.RecordState; import fr.Skydust.JdrBot.stock.Command; +import fr.Skydust.JdrBot.utils.MessageType; +import fr.Skydust.JdrBot.utils.Utils; +import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class Record extends Command { - public static HashMap rbs = new HashMap(); + public static HashMap recordStates = new HashMap<>(); public Record() { SetName("record"); SetDesc("Enregistre le chat vocal"); } + @Override public void call(GuildMessageReceivedEvent e) { - if(rbs.get(e.getGuild().getId()) == null) { - rbs.put(e.getGuild().getId(), new RecordState()); - } - - if(e.getGuild().getMember(e.getAuthor()).getVoiceState().getChannel() != null && !rbs.get(e.getGuild().getId()).isRecording() && !rbs.get(e.getGuild().getId()).isProcessing()) { - if(rbs.get(e.getGuild().getId()).isProcessing()) { - e.getChannel().sendMessage(e.getAuthor().getAsMention()+" Un enregistrement est en train d'etre sauvegarde envoye"); - return; - } - e.getChannel().sendMessage(e.getAuthor().getAsMention()+" Lancement du record").queue(); - System.out.println("["+new Date().toString()+"] Le serveur ID "+e.getGuild().getId()+"("+e.getGuild().getName()+") vient de lancer un enregistrement"); + RecordState currentRS = getGuildRecordState(e.getGuild()); + + if(e.getGuild().getMember(e.getAuthor()).getVoiceState().getChannel() != null && !currentRS.isRecording && !currentRS.isProcessing) { + Utils.sendMessage(e.getChannel(), MessageType.NORMAL, "Lancement d'un enregistrement par "+e.getAuthor().getAsMention()+"\n```!stoprecord``` pour le terminer\n```!crecord``` pour annuler"); + + System.out.println("["+new Date()+"] Le serveur ID "+e.getGuild().getId()+"("+e.getGuild().getName()+") vient de lancer un enregistrement"); + e.getGuild().getAudioManager().openAudioConnection(e.getGuild().getMember(e.getAuthor()).getVoiceState().getChannel()); - rbs.get(e.getGuild().getId()).setRecording(true);e.getGuild().getAudioManager().setReceivingHandler(new JdrBotARH(e.getGuild().getId())); + + currentRS.newRecord(); + currentRS.isRecording = true; + } else if(currentRS.isProcessing) { + Utils.sendMessage(e.getChannel(), MessageType.ERROR, "Un enregistrement est en cours de finalisation"); + } else if(currentRS.isRecording) { + Utils.sendMessage(e.getChannel(), MessageType.ERROR, "Un enregistrement est déjà en cours"); } else { - e.getChannel().sendMessage(e.getAuthor().getAsMention()+" n'est pas dans la channel vocal et ou a deja demarre un enregistrement!"); - } + Utils.sendMessage(e.getChannel(), MessageType.ERROR, "Vous n'êtes pas dans un salon vocal"); + } } + + public static synchronized RecordState getGuildRecordState(Guild guild) { + long guildId = Long.parseLong(guild.getId()); + RecordState recordState = recordStates.get(guildId); + + if (recordState == null) { + recordState = new RecordState(); + recordStates.put(guildId, recordState); + + guild.getAudioManager().setReceivingHandler(new JdrBotARH(recordState)); + } + + return recordState; + } } \ No newline at end of file diff --git a/src/main/java/fr/Skydust/JdrBot/cmds/record/StopRecord.java b/src/main/java/fr/Skydust/JdrBot/cmds/record/StopRecord.java index 47e466c..8dd861d 100755 --- a/src/main/java/fr/Skydust/JdrBot/cmds/record/StopRecord.java +++ b/src/main/java/fr/Skydust/JdrBot/cmds/record/StopRecord.java @@ -1,6 +1,9 @@ package fr.Skydust.JdrBot.cmds.record; import fr.Skydust.JdrBot.stock.Command; +import fr.Skydust.JdrBot.stock.RecordState; +import fr.Skydust.JdrBot.utils.MessageType; +import fr.Skydust.JdrBot.utils.Utils; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class StopRecord extends Command { @@ -11,11 +14,11 @@ public class StopRecord extends Command { @Override public void call(GuildMessageReceivedEvent e) { - if(Record.rbs.get(e.getGuild().getId()) != null && Record.rbs.get(e.getGuild().getId()).isRecording()) { - Record.rbs.get(e.getGuild().getId()).endRecord(e.getGuild(), e.getChannel()); - Record.rbs.put(e.getGuild().getId(), null); + RecordState rs = Record.getGuildRecordState(e.getGuild()); + if(rs.isRecording) { + rs.endRecord(e.getGuild(), e.getChannel()); } else { - e.getChannel().sendMessage("PERSONNE N'ENREGISTRE <3").queue(); + Utils.sendMessage(e.getChannel(), MessageType.ERROR, "Il n'y a aucun enregistrement en cours"); } } } diff --git a/src/main/java/fr/Skydust/JdrBot/stock/Command.java b/src/main/java/fr/Skydust/JdrBot/stock/Command.java index 56f8b4b..8730605 100755 --- a/src/main/java/fr/Skydust/JdrBot/stock/Command.java +++ b/src/main/java/fr/Skydust/JdrBot/stock/Command.java @@ -5,9 +5,15 @@ import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public abstract class Command { public String cmdName; public String cmdDesc; + public String cmdUsage = ""; + public boolean hidden = false; public abstract void call(GuildMessageReceivedEvent e); + public void Hide() { + hidden = true; + } + public void SetName(String name) { this.cmdName = name; } @@ -15,4 +21,8 @@ public abstract class Command { public void SetDesc(String desc) { this.cmdDesc = desc; } -} + + public void SetUsage(String usage) { + this.cmdUsage = usage; + } +} \ No newline at end of file diff --git a/src/main/java/fr/Skydust/JdrBot/stock/RecordState.java b/src/main/java/fr/Skydust/JdrBot/stock/RecordState.java index ca0e261..9f018ca 100755 --- a/src/main/java/fr/Skydust/JdrBot/stock/RecordState.java +++ b/src/main/java/fr/Skydust/JdrBot/stock/RecordState.java @@ -9,7 +9,8 @@ import java.util.List; import java.util.zip.ZipException; import fr.Skydust.JdrBot.jukebox.JukeboxGUISystem; -import fr.Skydust.JdrBot.utils.ByteArrayOutputStreamT; +import fr.Skydust.JdrBot.utils.MessageType; +import fr.Skydust.JdrBot.utils.Utils; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.TextChannel; import net.lingala.zip4j.ZipFile; @@ -22,95 +23,94 @@ import ws.schild.jave.Encoder; import ws.schild.jave.EncodingAttributes; import ws.schild.jave.MultimediaObject; +import javax.sound.sampled.AudioFileFormat; +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; + public class RecordState { - private boolean isRecording; - private final ByteArrayOutputStreamT bytes; - private boolean isProcessing; - - public RecordState() { - isRecording = false; - isProcessing = false; - bytes = new ByteArrayOutputStreamT(); - } - - public void setRecording(boolean Record) { - isRecording = Record; - } - - public boolean isRecording() { - return isRecording; - } - - public void setProcessing(boolean Process) { - isProcessing = Process; - } - - public boolean isProcessing() { - return isProcessing; - } - + private static final int RECORDER_SAMPLERATE = 44100; + public boolean isRecording; + private ByteArrayOutputStream recordData; + public boolean isProcessing; + public void addBytes(byte[] bytes) { - if(!isProcessing && isRecording) { - try { - this.bytes.write(bytes); - } catch (IOException e) { - e.printStackTrace(); - } + try { + this.recordData.write(bytes); + } catch (IOException e) { + e.printStackTrace(); } } - public ByteArrayOutputStreamT getBytes() { - return bytes; + public ByteArrayOutputStream getRecordData() { + return recordData; + } + + public void newRecord() { + recordData = new ByteArrayOutputStream(); + } + + public void cancelRecord(Guild g) { + isRecording = false; + + // Called from cancelRecord that checks if "isRecording" so it can't be processing + try { + if (recordData != null) { + recordData.close(); + recordData = null; + } + } catch (IOException e) { + e.printStackTrace(); + } + + if(JukeboxGUISystem.getGuildsJukebox(g) == null) { + g.getAudioManager().closeAudioConnection(); + } } public void endRecord(Guild g, TextChannel tc) { - setRecording(false); - setProcessing(true); + isRecording = false; + isProcessing = true; + if(JukeboxGUISystem.getGuildsJukebox(g) == null) { g.getAudioManager().closeAudioConnection(); } - System.out.println("["+new Date().toString()+"] Le serveur ID "+g.getId()+"("+g.getName()+") vient de terminer un enregistrement"); + System.out.println("["+new Date()+"] Le serveur ID "+g.getId()+"("+g.getName()+") vient de terminer un enregistrement"); + new Thread(() -> { try { - recordFinish(tc); - setProcessing(false); + processRecording(tc); } catch (Exception e1) { e1.printStackTrace(); - tc.sendMessage("["+new Date().toString()+"] Une erreur est survenue pendant l'enregistrement sur le serveur ID "+g.getId()+"("+g.getName()+")").queue(); + System.out.println("["+new Date()+"] Une erreur est survenue pendant l'enregistrement sur le serveur ID "+g.getId()+"("+g.getName()+")"); + + Utils.sendMessage(tc, MessageType.ERROR, "Une erreur est survenue pendant l'enregistrement."); + try { - bytes.close(); + recordData.close(); } catch (IOException e) { e.printStackTrace(); } - setProcessing(false); } + isProcessing = false; }).start(); } - public void recordFinish(TextChannel tc) throws ZipException, IOException, IllegalArgumentException, EncoderException, ws.schild.jave.EncoderException { - //Bytes to Wav - - - FileOutputStream out = new FileOutputStream("test.pcm"); - out.write(bytes.toByteArray()); - out.close(); - - + public void processRecording(TextChannel tc) throws IOException, IllegalArgumentException, EncoderException, ws.schild.jave.EncoderException { + //BigEndian, 16 bit signed, 44100hz String OriginalName = new Date().toString().replaceAll(":", "-"); File wavFile = new File(OriginalName+".wav"); File mp3File = new File(OriginalName+".mp3"); - //AudioSystem.write(ais, AudioFileFormat.Type.WAVE, wavFile); - //rawToWave(bytes.toByteArray(), wavFile); + //PCM to Wave + rawToWave(recordData, wavFile); - //bais.close(); - bytes.close(); - - //ais.close(); + recordData.close(); + recordData = null; - //Wav to MP3 + //Wave to MP3 Encoder encoder = new Encoder(); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); @@ -121,7 +121,7 @@ public class RecordState { ea.setFormat("mp3"); encoder.encode(new MultimediaObject(wavFile),mp3File,ea); - //wavFile.delete(); + wavFile.delete(); //MP3 to Zip ZipParameters parameters = new ZipParameters(); @@ -130,12 +130,12 @@ public class RecordState { ZipFile zipFile; zipFile = new ZipFile(OriginalName+".zip"); - //List filesToAdd = new ArrayList(); - //filesToAdd.add(mp3File); + List filesToAdd = new ArrayList(); + filesToAdd.add(mp3File); + + zipFile.createSplitZipFile(filesToAdd, parameters, true, 8283750); + mp3File.delete(); - //zipFile.createSplitZipFile(filesToAdd, parameters, true, 8283750); - //mp3File.delete(); - //Send Files through Discord if(zipFile.getSplitZipFiles().size() > 1) { tc.sendMessage("Envoi du fichier audio(Via plusieurs fichiers zip)...").queue(); @@ -150,5 +150,26 @@ public class RecordState { } } + /** + * Defines a default audio format used to record + */ + private AudioFormat getAudioFormat() { + float sampleRate = 44100; + int sampleSizeInBits = 16; + int channels = 2; + boolean signed = true; + boolean bigEndian = true; + return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); + } + public void rawToWave(ByteArrayOutputStream input, File wavFile) throws IOException { + AudioFormat format = getAudioFormat(); + byte[] audioData = input.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream(audioData); + AudioInputStream audioInputStream = new AudioInputStream(bais, format, audioData.length / format.getFrameSize()); + + AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, wavFile); + + audioInputStream.close(); + } } diff --git a/src/main/java/fr/Skydust/JdrBot/utils/ByteArrayOutputStreamT.java b/src/main/java/fr/Skydust/JdrBot/utils/ByteArrayOutputStreamT.java deleted file mode 100755 index 6cbf19a..0000000 --- a/src/main/java/fr/Skydust/JdrBot/utils/ByteArrayOutputStreamT.java +++ /dev/null @@ -1,10 +0,0 @@ -package fr.Skydust.JdrBot.utils; - -import java.io.ByteArrayOutputStream; - -public class ByteArrayOutputStreamT extends ByteArrayOutputStream { - @Override - public byte toByteArray()[] { - return buf; - } -} diff --git a/src/main/java/fr/Skydust/JdrBot/utils/MessageType.java b/src/main/java/fr/Skydust/JdrBot/utils/MessageType.java new file mode 100644 index 0000000..aa0e14e --- /dev/null +++ b/src/main/java/fr/Skydust/JdrBot/utils/MessageType.java @@ -0,0 +1,6 @@ +package fr.Skydust.JdrBot.utils; + +public enum MessageType { + ERROR, + NORMAL +} diff --git a/src/main/java/fr/Skydust/JdrBot/utils/Utils.java b/src/main/java/fr/Skydust/JdrBot/utils/Utils.java index c94806a..191caa9 100755 --- a/src/main/java/fr/Skydust/JdrBot/utils/Utils.java +++ b/src/main/java/fr/Skydust/JdrBot/utils/Utils.java @@ -1,5 +1,14 @@ package fr.Skydust.JdrBot.utils; +import fr.Skydust.JdrBot.JdrBot; +import fr.Skydust.JdrBot.cmds.record.Record; +import fr.Skydust.JdrBot.jukebox.JukeboxGUISystem; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.TextChannel; + +import java.awt.*; import java.time.Duration; public class Utils { @@ -34,4 +43,27 @@ public class Utils { } return String.format("%d heures", (absSeconds /3600)); } + + public static void sendMessage(TextChannel channel, MessageType msgType, String message) { + Color c = Color.GREEN; + String title = "Message"; + + if(msgType == MessageType.ERROR) { + c = Color.RED; + title = "Erreur"; + } + + channel.sendMessage(new MessageBuilder().setEmbed(new EmbedBuilder(). + setDescription(message) + .setTitle(title, null) + .setFooter("", JdrBot.jda.getSelfUser().getAvatarUrl()) + .setColor(c) + .build()).build()).queue(); + } + + public static void closeAudioConnection(Guild guild) { + if(JukeboxGUISystem.getGuildsJukebox(guild) == null && !Record.getGuildRecordState(guild).isRecording) { + guild.getAudioManager().closeAudioConnection(); + } + } }