diff --git a/src/main/java/moe/nekojimi/chords/Main.java b/src/main/java/moe/nekojimi/chords/Main.java index 3546eae..a47d741 100644 --- a/src/main/java/moe/nekojimi/chords/Main.java +++ b/src/main/java/moe/nekojimi/chords/Main.java @@ -7,6 +7,8 @@ package moe.nekojimi.chords; import java.io.File; import java.net.URL; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.*; import javax.security.auth.login.LoginException; import moe.nekojimi.chords.commands.*; @@ -16,6 +18,7 @@ import moe.nekojimi.musicsearcher.providers.Searcher; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.managers.AudioManager; @@ -77,6 +80,7 @@ public class Main extends ListenerAdapter public Main() { + log("INFO", "Starting up..."); downloader = new Downloader(); downloader.setMessageHandler((Song song, Exception ex) -> { @@ -85,8 +89,10 @@ public class Main extends ListenerAdapter if (channel != null) if (ex == null) if (song.getLocation() != null) + { channel.sendMessage(/*bracketNo + */"Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue(); - else + log("DOWN", "Downloaded " + song); + } else { Format format = song.getBestFormat(); String formatDetails = ""; @@ -95,9 +101,13 @@ public class Main extends ListenerAdapter formatDetails = " (" + format.getBitrate() / 1000 + "k, " + String.format("%.2f", format.getSize() / (1024.0 * 1024.0)) + "MiB)"; } channel.sendMessage(/*bracketNo + */"Now downloading " + song + formatDetails + " for " + song.getRequestedBy() + " ...").queue(); + log("DOWN", "Downloading " + song + "..."); } else + { channel.sendMessage(/*bracketNo + */"Failed to download " + song + " for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue(); + log("DOWN", "Failed to download " + song + "! Reason: " + ex.getMessage()); + } }); searcher = MetaSearcher.loadYAML(new File("searchproviders.yml")); @@ -111,6 +121,8 @@ public class Main extends ListenerAdapter addCommand(new SkipCommand(this)); helpCommand = new HelpCommand(this); addCommand(helpCommand); + + log("INFO", "Started OK!"); } private void addCommand(Command command) @@ -123,6 +135,19 @@ public class Main extends ListenerAdapter this.jda = jda; } + @Override + public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) + { + if (this.currentVoiceChannel == null) + return; + + final VoiceChannel channelLeft = event.getChannelLeft(); + + if (channelLeft.getMembers().isEmpty()) + if (channelLeft == currentVoiceChannel) + disconnect(); + } + @Override public void onGuildMessageReceived(GuildMessageReceivedEvent event) { @@ -139,6 +164,8 @@ public class Main extends ListenerAdapter if (author.isBot()) return; + log("MESG", "G:" + guild.getName() + " A:" + author.getName() + " C:" + content); + try { String[] split = content.split("\\s+", 2); @@ -162,6 +189,7 @@ public class Main extends ListenerAdapter } catch (Exception ex) { event.getChannel().sendMessage("Error in command! " + ex.getMessage()).queue(); + log("UERR", "Command error:" + ex.getMessage()); } } @@ -223,6 +251,11 @@ public class Main extends ListenerAdapter } } + public void log(String type, String message) + { + System.out.println(type + " " + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) + "\t" + message); + } + public MusicHandler getMusicHandler() { return musicHandler;