* Added an argument parsing system
* Added an heartbeat argument that send a command every 60 seconds if it's online
This commit is contained in:
@@ -2,7 +2,9 @@ package fr.Skydust.JdrBot;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import javax.security.auth.login.LoginException;
|
||||
|
||||
import fr.Skydust.JdrBot.cmds.*;
|
||||
@@ -11,6 +13,7 @@ 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 fr.Skydust.JdrBot.utils.HeartbeatTask;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
@@ -26,14 +29,32 @@ public class JdrBot {
|
||||
public static String startcmdchar = "!|:";
|
||||
|
||||
public static void main(String args[]) {
|
||||
for (int i=0;i<args.length;i++) {
|
||||
if(args[i].equalsIgnoreCase("--heartbeat")) {
|
||||
if(i == args.length-1) { // Check for an argument after heartbeat
|
||||
System.out.println("USAGE: --heartbeat [Command({ping})]");
|
||||
return; // Quits the program
|
||||
}
|
||||
//"zabbix_sender -z localhost -s \"Lustricru\" -k discordBots.potatos.ping -o {ping}"
|
||||
String command = String.join("", Arrays.copyOfRange(args,i+1,args.length));
|
||||
|
||||
if(command.startsWith("\"")) command = command.split("\"")[1]; // If the command is in "", remove them
|
||||
|
||||
new Timer().scheduleAtFixedRate(new HeartbeatTask(command),1000, 60000);
|
||||
|
||||
System.out.println("Registered Heartbeat Command: "+command);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
basedate = LocalDateTime.now();
|
||||
/*jda = JDABuilder.createDefault("MTY5OTMzMzgxMDMzOTE4NDY0.DerlJg.m7BdNv_OMHlYa-f4T3O0jJ9LldM")
|
||||
.setActivity(Activity.playing("un jeu de rôle"))
|
||||
.enableCache(CacheFlag.VOICE_STATE).build();
|
||||
*/
|
||||
|
||||
jda = JDABuilder.createDefault("ODk0ODc0Nzk5Nzg3MTE0NTA2.YVwXGg.JfIsIIQHIXbVsjoGGv2SfejnT9s")
|
||||
// Prod Bot
|
||||
//String token = "MTY5OTMzMzgxMDMzOTE4NDY0.DerlJg.m7BdNv_OMHlYa-f4T3O0jJ9LldM";
|
||||
// Test Bot
|
||||
String token = "ODk0ODc0Nzk5Nzg3MTE0NTA2.YVwXGg.JfIsIIQHIXbVsjoGGv2SfejnT9s";
|
||||
|
||||
jda = JDABuilder.createDefault(token)
|
||||
.setActivity(Activity.playing("un jeu de rôle"))
|
||||
.enableCache(CacheFlag.VOICE_STATE).build();
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import fr.Skydust.JdrBot.cmds.LastTimeOnline;
|
||||
import fr.Skydust.JdrBot.cmds.playmusic.StopMusic;
|
||||
import fr.Skydust.JdrBot.menu.MenuSystem;
|
||||
import fr.Skydust.JdrBot.stock.Command;
|
||||
import fr.Skydust.JdrBot.utils.HeartbeatTask;
|
||||
import net.dv8tion.jda.api.events.GatewayPingEvent;
|
||||
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;
|
||||
@@ -14,8 +16,6 @@ import net.dv8tion.jda.api.events.user.update.UserUpdateOnlineStatusEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
|
||||
public class JdrBotListener extends ListenerAdapter {
|
||||
Random r = new Random();
|
||||
|
||||
@Override
|
||||
public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
|
||||
// IF it doesn't start with the startcmdchar, ignore for optimization
|
||||
@@ -36,10 +36,12 @@ public class JdrBotListener extends ListenerAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
/* LastTimeOnline listener */
|
||||
@Override
|
||||
public void onUserUpdateOnlineStatus(UserUpdateOnlineStatusEvent e) {
|
||||
LastTimeOnline.onUserUpdateOnlineStatus(e);
|
||||
}
|
||||
/* Empty voice channel listener */
|
||||
@Override
|
||||
public void onGuildVoiceMove(GuildVoiceMoveEvent e) {
|
||||
StopMusic.onGuildVoiceMove(e);
|
||||
@@ -48,8 +50,12 @@ public class JdrBotListener extends ListenerAdapter {
|
||||
public void onGuildVoiceLeave(GuildVoiceLeaveEvent e) {
|
||||
StopMusic.onGuildVoiceLeave(e);
|
||||
}
|
||||
/* Menu system listener */
|
||||
@Override
|
||||
public void onMessageReactionAdd(MessageReactionAddEvent e) { MenuSystem.onMessageReactionAdd(e); }
|
||||
/* Heartbeat command listener */
|
||||
@Override
|
||||
public void onGatewayPing(GatewayPingEvent e) { HeartbeatTask.HeartbeatUpdateEvent(e); }
|
||||
|
||||
public boolean isACommand(String message, String cmd) {
|
||||
return message.matches("^("+ JdrBot.startcmdchar +")("+ cmd +")( .*|)$");
|
||||
|
||||
@@ -52,11 +52,13 @@ public class PlayMusic extends Command {
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO: WHILE LOADING, CAN CALL AGAIN
|
||||
// Create a new JukeboxSystem for the guild
|
||||
JukeboxSystem jukeboxSystem = new JukeboxSystem();
|
||||
jukeboxSystems.put(e.getGuild().getIdLong(), jukeboxSystem);
|
||||
|
||||
// Creates a message to be edited by the jukebox system
|
||||
e.getGuild().getAudioManager().openAudioConnection(e.getMember().getVoiceState().getChannel());
|
||||
e.getChannel().sendMessage("Chargement...").queue(msg -> jukeboxSystems.put(e.getGuild().getIdLong(), new JukeboxSystem(msg)));
|
||||
e.getChannel().sendMessage("Chargement...").queue(jukeboxSystem::load);
|
||||
}
|
||||
|
||||
public static JukeboxSystem getGuildsJukebox(Guild guild) {
|
||||
|
||||
@@ -25,13 +25,13 @@ public class StopMusic 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) {
|
||||
if(e.getGuild().getAudioManager().isConnected() && e.getChannelLeft().getIdLong() == e.getGuild().getAudioManager().getConnectedChannel().getIdLong() && 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) {
|
||||
if(e.getGuild().getAudioManager().isConnected() && e.getChannelLeft().getIdLong() == e.getGuild().getAudioManager().getConnectedChannel().getIdLong() && e.getChannelLeft().getMembers().size() == 1) {
|
||||
stopMusic(e.getGuild());
|
||||
}
|
||||
}
|
||||
@@ -39,15 +39,18 @@ public class StopMusic extends Command {
|
||||
public static void stopMusic(Guild g) {
|
||||
JukeboxSystem jukebox = PlayMusic.getGuildsJukebox(g);
|
||||
|
||||
// Deletes the jukebox message
|
||||
if(jukebox != null) {
|
||||
System.out.println("JukeboxMessageIDStop:"+jukebox.jukeboxMessage.getId());
|
||||
jukebox.jukeboxMessage.delete().queue();
|
||||
}
|
||||
|
||||
// Clears the guild's jukebox system
|
||||
PlayMusic.jukeboxSystems.put(g.getIdLong(), null);
|
||||
|
||||
// Stops the audio
|
||||
PlayMusic.getGuildAudioPlayer(g).scheduler.stop();
|
||||
|
||||
// Leave the voice channel if not recording
|
||||
if(!Record.getGuildRecordState(g).isRecording) {
|
||||
g.getAudioManager().closeAudioConnection();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package fr.Skydust.JdrBot.jukebox;
|
||||
|
||||
import fr.Skydust.JdrBot.cmds.playmusic.PlayMusic;
|
||||
import fr.Skydust.JdrBot.cmds.playmusic.StopMusic;
|
||||
import fr.Skydust.JdrBot.menu.Menu;
|
||||
import fr.Skydust.JdrBot.menu.MenuSystem;
|
||||
import fr.Skydust.JdrBot.utils.Utils;
|
||||
@@ -14,7 +13,7 @@ import java.util.*;
|
||||
public class JukeboxSystem {
|
||||
// Const
|
||||
public static final String initialFolder = "Songs";
|
||||
public static final String musicPlayerThumbnail = "https://cdn4.iconfinder.com/data/icons/miu/24/device-volume-loudspeaker-speaker-up-glyph-128.png";
|
||||
public static final String musicPlayerThumbnail = "https://cd4.iconfinder.com/data/icons/miu/24/device-volume-loudspeaker-speaker-up-glyph-128.png";
|
||||
|
||||
// Dyn
|
||||
// Keeping track of the song currently played to update menus accordingly
|
||||
@@ -22,11 +21,11 @@ public class JukeboxSystem {
|
||||
public String songPlayedPath = "";
|
||||
|
||||
private final HashMap<String, Menu> linkedPath = new HashMap<>();
|
||||
public final Message jukeboxMessage;
|
||||
public Message jukeboxMessage;
|
||||
|
||||
private String currentFolder = "";
|
||||
|
||||
public JukeboxSystem(Message msg) {
|
||||
public void load(Message msg) {
|
||||
jukeboxMessage = msg;
|
||||
openFolder(initialFolder);
|
||||
}
|
||||
@@ -42,7 +41,7 @@ public class JukeboxSystem {
|
||||
File[] files = new File(currentFolder).listFiles();
|
||||
|
||||
// If it isn't the root folder, place the return button
|
||||
if(!isInRootFolder()) {
|
||||
if(isntInRootFolder()) {
|
||||
items.add(Utils.ReturnUnicode + " Retour");
|
||||
}
|
||||
if(files != null) {
|
||||
@@ -87,7 +86,7 @@ public class JukeboxSystem {
|
||||
}
|
||||
|
||||
// Return button
|
||||
if (!isInRootFolder() && itemId == 0) {
|
||||
if (isntInRootFolder() && itemId == 0) {
|
||||
// Back one menu
|
||||
String subFolder = currentFolder.substring(0, currentFolder.lastIndexOf("/"));
|
||||
currentFolder = subFolder;
|
||||
@@ -129,11 +128,10 @@ public class JukeboxSystem {
|
||||
jukeboxMessage.getTextChannel().retrieveMessageById(jukeboxMessage.getIdLong()).queue(msg -> {
|
||||
MenuSystem.changeMenu(msg, finalMenu);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public boolean isInRootFolder() {
|
||||
return currentFolder.equals(initialFolder);
|
||||
public boolean isntInRootFolder() {
|
||||
return !currentFolder.equals(initialFolder);
|
||||
}
|
||||
|
||||
public static String getFileNameFromItem(String item) {
|
||||
@@ -141,21 +139,8 @@ public class JukeboxSystem {
|
||||
}
|
||||
|
||||
private final Comparator<File> sortItemsComp = (o1, o2) -> {
|
||||
if (o1.isDirectory() && !o2.isDirectory())
|
||||
{
|
||||
// Directory before non-directory
|
||||
return -1;
|
||||
}
|
||||
else if (!o1.isDirectory() && o2.isDirectory())
|
||||
{
|
||||
// Non-directory after directory
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Alphabetic order otherwise
|
||||
//return o1.compareTo(o2);
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
if (o1.isDirectory() && !o2.isDirectory()) return -1; // Directory before non-directory
|
||||
else if (!o1.isDirectory() && o2.isDirectory()) return 1; // Non-directory after directory
|
||||
else return o1.compareTo(o2); // Alphabetic order otherwise
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ import net.lingala.zip4j.model.ZipParameters;
|
||||
import net.lingala.zip4j.model.enums.CompressionLevel;
|
||||
import net.lingala.zip4j.model.enums.CompressionMethod;
|
||||
import org.apache.commons.codec.EncoderException;
|
||||
import ws.schild.jave.AudioAttributes;
|
||||
import ws.schild.jave.Encoder;
|
||||
import ws.schild.jave.EncodingAttributes;
|
||||
import ws.schild.jave.MultimediaObject;
|
||||
import ws.schild.jave.encode.AudioAttributes;
|
||||
import ws.schild.jave.encode.EncodingAttributes;
|
||||
|
||||
import javax.sound.sampled.AudioFileFormat;
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
@@ -106,7 +106,7 @@ public class RecordState {
|
||||
audio.setChannels(2);
|
||||
EncodingAttributes ea = new EncodingAttributes();
|
||||
ea.setAudioAttributes(audio);
|
||||
ea.setFormat("mp3");
|
||||
ea.setOutputFormat("mp3");
|
||||
|
||||
encoder.encode(new MultimediaObject(wavFile),mp3File,ea);
|
||||
wavFile.delete();
|
||||
|
||||
36
src/main/java/fr/Skydust/JdrBot/utils/HeartbeatTask.java
Normal file
36
src/main/java/fr/Skydust/JdrBot/utils/HeartbeatTask.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package fr.Skydust.JdrBot.utils;
|
||||
|
||||
import fr.Skydust.JdrBot.JdrBot;
|
||||
import net.dv8tion.jda.api.events.GatewayPingEvent;
|
||||
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class HeartbeatTask extends TimerTask {
|
||||
private String command = "";
|
||||
private static boolean heartbeatUpdated = false;
|
||||
|
||||
public HeartbeatTask(String command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if(heartbeatUpdated) {
|
||||
long ping = JdrBot.jda.getGatewayPing();
|
||||
|
||||
try {
|
||||
Runtime.getRuntime().exec(command.replaceAll("\\{ping\\}", String.valueOf(ping)));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: "+e.getMessage());
|
||||
}
|
||||
|
||||
System.out.println("Sent heartbeat "+ ping);
|
||||
|
||||
heartbeatUpdated = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void HeartbeatUpdateEvent(GatewayPingEvent e) {
|
||||
heartbeatUpdated = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user