Quelques changements.
La commande record fonctionne maintenant
This commit is contained in:
@@ -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<Command> 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<Command>();
|
||||
|
||||
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());
|
||||
|
||||
@@ -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 +")( .*|)$");
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
25
src/main/java/fr/Skydust/JdrBot/cmds/record/CancelRecord.java
Executable file
25
src/main/java/fr/Skydust/JdrBot/cmds/record/CancelRecord.java
Executable file
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<String, RecordState> rbs = new HashMap<String, RecordState>();
|
||||
public static HashMap<Long, RecordState> 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;
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<File>();
|
||||
//filesToAdd.add(mp3File);
|
||||
List<File> filesToAdd = new ArrayList<File>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package fr.Skydust.JdrBot.utils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class ByteArrayOutputStreamT extends ByteArrayOutputStream {
|
||||
@Override
|
||||
public byte toByteArray()[] {
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
6
src/main/java/fr/Skydust/JdrBot/utils/MessageType.java
Normal file
6
src/main/java/fr/Skydust/JdrBot/utils/MessageType.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package fr.Skydust.JdrBot.utils;
|
||||
|
||||
public enum MessageType {
|
||||
ERROR,
|
||||
NORMAL
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user