|
|
@ -8,6 +8,7 @@ package moe.nekojimi.chords; |
|
|
|
import com.amihaiemil.eoyaml.Yaml; |
|
|
|
import com.amihaiemil.eoyaml.Yaml; |
|
|
|
import com.amihaiemil.eoyaml.YamlMapping; |
|
|
|
import com.amihaiemil.eoyaml.YamlMapping; |
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.io.FileWriter; |
|
|
|
import java.io.FilenameFilter; |
|
|
|
import java.io.FilenameFilter; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.MalformedURLException; |
|
|
|
import java.net.MalformedURLException; |
|
|
@ -22,6 +23,10 @@ import java.util.logging.Level; |
|
|
|
import java.util.logging.Logger; |
|
|
|
import java.util.logging.Logger; |
|
|
|
import javax.security.auth.login.LoginException; |
|
|
|
import javax.security.auth.login.LoginException; |
|
|
|
import moe.nekojimi.chords.commands.*; |
|
|
|
import moe.nekojimi.chords.commands.*; |
|
|
|
|
|
|
|
import moe.nekojimi.chords.commands.playlists.AddToPlaylistCommand; |
|
|
|
|
|
|
|
import moe.nekojimi.chords.commands.playlists.MakePlaylistCommand; |
|
|
|
|
|
|
|
import moe.nekojimi.chords.commands.playlists.PlaylistCommand; |
|
|
|
|
|
|
|
import moe.nekojimi.chords.commands.playlists.StopPlaylistCommand; |
|
|
|
import moe.nekojimi.musicsearcher.providers.MetaSearcher; |
|
|
|
import moe.nekojimi.musicsearcher.providers.MetaSearcher; |
|
|
|
import moe.nekojimi.musicsearcher.providers.Searcher; |
|
|
|
import moe.nekojimi.musicsearcher.providers.Searcher; |
|
|
|
import net.dv8tion.jda.api.JDA; |
|
|
|
import net.dv8tion.jda.api.JDA; |
|
|
@ -110,7 +115,7 @@ public final class Main extends ListenerAdapter |
|
|
|
if (ex == null) |
|
|
|
if (ex == null) |
|
|
|
if (song.getLocation() != null) |
|
|
|
if (song.getLocation() != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
request.respond("Finished downloading " + song + ", added to queue!"); |
|
|
|
request.getInvocation().respond("Finished downloading " + song + ", added to queue!"); |
|
|
|
log("DOWN", "Downloaded " + song); |
|
|
|
log("DOWN", "Downloaded " + song); |
|
|
|
} else |
|
|
|
} else |
|
|
|
{ |
|
|
|
{ |
|
|
@ -124,12 +129,12 @@ public final class Main extends ListenerAdapter |
|
|
|
String bitFmt = (bitrate <= 0 ? "??" : bitrate) + "k"; |
|
|
|
String bitFmt = (bitrate <= 0 ? "??" : bitrate) + "k"; |
|
|
|
formatDetails = " (" + bitFmt + ", " + sizeFmt + ")"; |
|
|
|
formatDetails = " (" + bitFmt + ", " + sizeFmt + ")"; |
|
|
|
} |
|
|
|
} |
|
|
|
request.respond("Now downloading " + song + formatDetails + " ..."); |
|
|
|
request.getInvocation().respond("Now downloading " + song + formatDetails + " ..."); |
|
|
|
log("DOWN", "Downloading " + song + "..."); |
|
|
|
log("DOWN", "Downloading " + song + "..."); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
request.respond("Failed to download " + song + "! Reason: " + ex.getMessage()); |
|
|
|
request.getInvocation().respond("Failed to download " + song + "! Reason: " + ex.getMessage()); |
|
|
|
log("DOWN", "Failed to download " + song + "! Reason: " + ex.getMessage()); |
|
|
|
log("DOWN", "Failed to download " + song + "! Reason: " + ex.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -165,6 +170,11 @@ public final class Main extends ListenerAdapter |
|
|
|
helpCommand = new HelpCommand(this); |
|
|
|
helpCommand = new HelpCommand(this); |
|
|
|
addCommand(helpCommand); |
|
|
|
addCommand(helpCommand); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addCommand(new AddToPlaylistCommand(this)); |
|
|
|
|
|
|
|
addCommand(new PlaylistCommand(this)); |
|
|
|
|
|
|
|
addCommand(new MakePlaylistCommand(this)); |
|
|
|
|
|
|
|
addCommand(new StopPlaylistCommand(this)); |
|
|
|
|
|
|
|
|
|
|
|
// load playlists
|
|
|
|
// load playlists
|
|
|
|
loadPlaylists(); |
|
|
|
loadPlaylists(); |
|
|
|
|
|
|
|
|
|
|
@ -216,7 +226,7 @@ public final class Main extends ListenerAdapter |
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
{ |
|
|
|
URL parseURL = new URL(content.trim()); |
|
|
|
URL parseURL = new URL(content.trim()); |
|
|
|
playCommand.call(event, List.of(parseURL.toExternalForm())); |
|
|
|
playCommand.call(new Invocation(event, List.of(parseURL.toExternalForm()))); |
|
|
|
} catch (MalformedURLException ex) |
|
|
|
} catch (MalformedURLException ex) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// not a URL, then
|
|
|
|
// not a URL, then
|
|
|
@ -224,7 +234,7 @@ public final class Main extends ListenerAdapter |
|
|
|
|
|
|
|
|
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
{ |
|
|
|
String[] split = content.split("\\s+", 2); |
|
|
|
String[] split = content.split("\\s+"); |
|
|
|
String cmd = split[0].toLowerCase(); |
|
|
|
String cmd = split[0].toLowerCase(); |
|
|
|
|
|
|
|
|
|
|
|
if (!cmd.startsWith("!")) |
|
|
|
if (!cmd.startsWith("!")) |
|
|
@ -232,17 +242,21 @@ public final class Main extends ListenerAdapter |
|
|
|
|
|
|
|
|
|
|
|
cmd = cmd.substring(1); // strip prefix char
|
|
|
|
cmd = cmd.substring(1); // strip prefix char
|
|
|
|
|
|
|
|
|
|
|
|
String arg = ""; |
|
|
|
// String arg = "";
|
|
|
|
if (split.length > 1) |
|
|
|
List<String> args = new ArrayList<>(); |
|
|
|
arg = split[1]; |
|
|
|
Collections.addAll(args, split); |
|
|
|
|
|
|
|
args.remove(0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Invocation invocation = new Invocation(event, args); |
|
|
|
|
|
|
|
invocation.setRequestMessage(message); |
|
|
|
|
|
|
|
|
|
|
|
if (commands.containsKey(cmd)) |
|
|
|
if (commands.containsKey(cmd)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Command command = commands.get(cmd); |
|
|
|
Command command = commands.get(cmd); |
|
|
|
command.call(event, List.of(arg)); |
|
|
|
command.call(invocation); |
|
|
|
} else |
|
|
|
} else |
|
|
|
{ |
|
|
|
{ |
|
|
|
helpCommand.call(event, List.of(arg)); |
|
|
|
helpCommand.call(invocation); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception ex) |
|
|
|
} catch (Exception ex) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -251,8 +265,9 @@ public final class Main extends ListenerAdapter |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Song queueDownload(SongRequest request) |
|
|
|
public Song queueDownload(SongRequest request, Consumer<Song> destination) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// TODO: move this logic somewhere better
|
|
|
|
Song song; |
|
|
|
Song song; |
|
|
|
if (request.getUrl() != null) |
|
|
|
if (request.getUrl() != null) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -262,16 +277,16 @@ public final class Main extends ListenerAdapter |
|
|
|
// interpret search result
|
|
|
|
// interpret search result
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); |
|
|
|
throw new UnsupportedOperationException("Not supported yet."); |
|
|
|
} |
|
|
|
} |
|
|
|
if (request.getRequestMessage() != null) |
|
|
|
if (request.getInvocation().getRequestMessage() != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
song.setRequestedBy(request.getRequestMessage().getAuthor().getName()); |
|
|
|
song.setRequestedBy(request.getInvocation().getRequestMessage().getAuthor().getName()); |
|
|
|
song.setRequestedIn(request.getRequestMessage().getChannel().getId()); |
|
|
|
song.setRequestedIn(request.getInvocation().getRequestMessage().getChannel().getId()); |
|
|
|
} |
|
|
|
} |
|
|
|
song.setNumber(trackNumber); |
|
|
|
song.setNumber(trackNumber); |
|
|
|
trackNumber++; |
|
|
|
trackNumber++; |
|
|
|
request.setSong(song); |
|
|
|
request.setSong(song); |
|
|
|
downloader.accept(new Downloader.DownloadTask(request, queueManager)); |
|
|
|
downloader.accept(new Downloader.DownloadTask(request, destination)); |
|
|
|
request.respond("Request pending..."); |
|
|
|
request.getInvocation().respond("Request pending..."); |
|
|
|
return song; |
|
|
|
return song; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -371,6 +386,7 @@ public final class Main extends ListenerAdapter |
|
|
|
{ |
|
|
|
{ |
|
|
|
YamlMapping map = Yaml.createYamlInput(file).readYamlMapping(); |
|
|
|
YamlMapping map = Yaml.createYamlInput(file).readYamlMapping(); |
|
|
|
Playlist playlist = Playlist.fromYaml(map); |
|
|
|
Playlist playlist = Playlist.fromYaml(map); |
|
|
|
|
|
|
|
playlist.setBot(this); |
|
|
|
playlists.put(playlist.getName(), playlist); |
|
|
|
playlists.put(playlist.getName(), playlist); |
|
|
|
} catch (IOException ex) |
|
|
|
} catch (IOException ex) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -379,6 +395,26 @@ public final class Main extends ListenerAdapter |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void addPlaylist(Playlist playlist) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
playlists.put(playlist.getName(), playlist); |
|
|
|
|
|
|
|
playlist.setBot(this); |
|
|
|
|
|
|
|
savePlaylist(playlist); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void savePlaylist(Playlist playlist) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
File file = new File(playlistsDirectory, playlist.getName() + ".yaml"); |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Yaml.createYamlPrinter(new FileWriter(file)) |
|
|
|
|
|
|
|
.print(playlist.toYaml()); |
|
|
|
|
|
|
|
} catch (IOException ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public MusicHandler getMusicHandler() |
|
|
|
public MusicHandler getMusicHandler() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return musicHandler; |
|
|
|
return musicHandler; |
|
|
@ -413,5 +449,14 @@ public final class Main extends ListenerAdapter |
|
|
|
return trackNumber; |
|
|
|
return trackNumber; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public File getDataDirectory() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return dataDirectory; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, Playlist> getPlaylists() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return playlists; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|