From a451369c92e4f863aa98d62c17e7789b9b2324a2 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Mon, 20 Jun 2022 17:14:04 +0100 Subject: [PATCH] Add playlist commands. --- src/main/java/moe/nekojimi/chords/Main.java | 49 ++++++++++++-- .../moe/nekojimi/chords/MusicHandler.java | 2 + .../java/moe/nekojimi/chords/Playlist.java | 14 ++++ .../moe/nekojimi/chords/QueueManager.java | 2 + .../playlists/AddToPlaylistCommand.java | 66 +++++++++++++++++++ .../playlists/MakePlaylistCommand.java | 52 +++++++++++++++ .../commands/playlists/PlaylistCommand.java | 51 ++++++++++++++ .../playlists/StopPlaylistCommand.java | 48 ++++++++++++++ 8 files changed, 280 insertions(+), 4 deletions(-) create mode 100644 src/main/java/moe/nekojimi/chords/commands/playlists/AddToPlaylistCommand.java create mode 100644 src/main/java/moe/nekojimi/chords/commands/playlists/MakePlaylistCommand.java create mode 100644 src/main/java/moe/nekojimi/chords/commands/playlists/PlaylistCommand.java create mode 100644 src/main/java/moe/nekojimi/chords/commands/playlists/StopPlaylistCommand.java diff --git a/src/main/java/moe/nekojimi/chords/Main.java b/src/main/java/moe/nekojimi/chords/Main.java index 1c745ff..f16c0e5 100644 --- a/src/main/java/moe/nekojimi/chords/Main.java +++ b/src/main/java/moe/nekojimi/chords/Main.java @@ -8,6 +8,7 @@ package moe.nekojimi.chords; import com.amihaiemil.eoyaml.Yaml; import com.amihaiemil.eoyaml.YamlMapping; import java.io.File; +import java.io.FileWriter; import java.io.FilenameFilter; import java.io.IOException; import java.net.MalformedURLException; @@ -22,6 +23,10 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.security.auth.login.LoginException; 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.Searcher; import net.dv8tion.jda.api.JDA; @@ -165,6 +170,11 @@ public final class Main extends ListenerAdapter helpCommand = new HelpCommand(this); addCommand(helpCommand); + addCommand(new AddToPlaylistCommand(this)); + addCommand(new PlaylistCommand(this)); + addCommand(new MakePlaylistCommand(this)); + addCommand(new StopPlaylistCommand(this)); + // load playlists loadPlaylists(); @@ -216,7 +226,7 @@ public final class Main extends ListenerAdapter try { URL parseURL = new URL(content.trim()); - playCommand.call(null); + playCommand.call(new Invocation(event, List.of(parseURL.toExternalForm()))); } catch (MalformedURLException ex) { // not a URL, then @@ -255,8 +265,9 @@ public final class Main extends ListenerAdapter } } - public Song queueDownload(SongRequest request) + public Song queueDownload(SongRequest request, Consumer destination) { + // TODO: move this logic somewhere better Song song; if (request.getUrl() != null) { @@ -274,8 +285,8 @@ public final class Main extends ListenerAdapter song.setNumber(trackNumber); trackNumber++; request.setSong(song); - downloader.accept(new Downloader.DownloadTask(request, queueManager)); - request.respond("Request pending..."); + downloader.accept(new Downloader.DownloadTask(request, destination)); + request.getInvocation().respond("Request pending..."); return song; } @@ -375,6 +386,7 @@ public final class Main extends ListenerAdapter { YamlMapping map = Yaml.createYamlInput(file).readYamlMapping(); Playlist playlist = Playlist.fromYaml(map); + playlist.setBot(this); playlists.put(playlist.getName(), playlist); } catch (IOException ex) { @@ -383,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() { return musicHandler; @@ -417,5 +449,14 @@ public final class Main extends ListenerAdapter return trackNumber; } + public File getDataDirectory() + { + return dataDirectory; + } + + public Map getPlaylists() + { + return playlists; + } } diff --git a/src/main/java/moe/nekojimi/chords/MusicHandler.java b/src/main/java/moe/nekojimi/chords/MusicHandler.java index 127f6d6..a5bad7e 100644 --- a/src/main/java/moe/nekojimi/chords/MusicHandler.java +++ b/src/main/java/moe/nekojimi/chords/MusicHandler.java @@ -94,6 +94,8 @@ public class MusicHandler implements AudioSendHandler, Closeable if (!currentSong.isKept()) currentSong.delete(); currentSong = null; + player.close(); + player = null; } currentSong = queueManager.nextSongNeeded(); if (nowPlayingConsumer != null) diff --git a/src/main/java/moe/nekojimi/chords/Playlist.java b/src/main/java/moe/nekojimi/chords/Playlist.java index 2863d35..12b9e61 100644 --- a/src/main/java/moe/nekojimi/chords/Playlist.java +++ b/src/main/java/moe/nekojimi/chords/Playlist.java @@ -36,6 +36,7 @@ public class Playlist implements Consumer private static final int SHUFFLE_DONT_REPEAT_LAST = 3; private final String name; + private Main bot; private final List songs = new ArrayList<>(); private final LinkedList playHistory = new LinkedList<>(); @@ -77,6 +78,9 @@ public class Playlist implements Consumer { song.setKept(true); songs.add(song); + + if (bot != null) + bot.savePlaylist(this); } public String getName() @@ -119,4 +123,14 @@ public class Playlist implements Consumer addSong(t); } + public Main getBot() + { + return bot; + } + + public void setBot(Main bot) + { + this.bot = bot; + } + } diff --git a/src/main/java/moe/nekojimi/chords/QueueManager.java b/src/main/java/moe/nekojimi/chords/QueueManager.java index 2200392..48cb4be 100644 --- a/src/main/java/moe/nekojimi/chords/QueueManager.java +++ b/src/main/java/moe/nekojimi/chords/QueueManager.java @@ -114,6 +114,8 @@ public class QueueManager implements Consumer public void setPlaylist(Playlist playlist) { this.playlist = playlist; + if (playlist == null || !handler.isPlaying()) + handler.playNext(); } public boolean restartSong() diff --git a/src/main/java/moe/nekojimi/chords/commands/playlists/AddToPlaylistCommand.java b/src/main/java/moe/nekojimi/chords/commands/playlists/AddToPlaylistCommand.java new file mode 100644 index 0000000..6adf9c3 --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/playlists/AddToPlaylistCommand.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2022 jimj316 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package moe.nekojimi.chords.commands.playlists; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.logging.Level; +import java.util.logging.Logger; +import moe.nekojimi.chords.Main; +import moe.nekojimi.chords.Playlist; +import moe.nekojimi.chords.SongRequest; +import moe.nekojimi.chords.commands.Command; +import moe.nekojimi.chords.commands.Invocation; + +/** + * + * @author jimj316 + */ +public class AddToPlaylistCommand extends Command +{ + + public AddToPlaylistCommand(Main bot) + { + super(bot, "addlist"); + } + + @Override + public void call(Invocation invocation) + { + String playlistName = invocation.getArgs().get(0); + Playlist playlist = bot.getPlaylists().get(playlistName); + if (playlist == null) + { + invocation.respond("There's no playlist named \"" + playlistName + "\"!"); + return; + } + + try + { + URL url = new URL(invocation.getArgs().get(1)); + final SongRequest request = new SongRequest(); + request.setInvocation(invocation); + request.setUrl(url); + bot.queueDownload(request, playlist); + } catch (MalformedURLException ex) + { + invocation.respond("That's not a valid URL!"); + Logger.getLogger(AddToPlaylistCommand.class.getName()).log(Level.SEVERE, null, ex); + } + } + +} diff --git a/src/main/java/moe/nekojimi/chords/commands/playlists/MakePlaylistCommand.java b/src/main/java/moe/nekojimi/chords/commands/playlists/MakePlaylistCommand.java new file mode 100644 index 0000000..38417d1 --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/playlists/MakePlaylistCommand.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2022 jimj316 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package moe.nekojimi.chords.commands.playlists; + +import moe.nekojimi.chords.Main; +import moe.nekojimi.chords.Playlist; +import moe.nekojimi.chords.commands.Command; +import moe.nekojimi.chords.commands.Invocation; + +/** + * + * @author jimj316 + */ +public class MakePlaylistCommand extends Command +{ + + public MakePlaylistCommand(Main bot) + { + super(bot, "makelist"); + } + + @Override + public void call(Invocation invocation) + { + String name = invocation.getArgs().get(0); + if (bot.getPlaylists().containsKey(name)) + { + invocation.respond("There's already a playlist named \"" + name + "\"!"); + return; + } + + Playlist list = new Playlist(name); + bot.addPlaylist(list); + + invocation.respond("Playlist created!"); + } + +} diff --git a/src/main/java/moe/nekojimi/chords/commands/playlists/PlaylistCommand.java b/src/main/java/moe/nekojimi/chords/commands/playlists/PlaylistCommand.java new file mode 100644 index 0000000..5fd28f2 --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/playlists/PlaylistCommand.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2022 jimj316 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package moe.nekojimi.chords.commands.playlists; + +import moe.nekojimi.chords.Main; +import moe.nekojimi.chords.Playlist; +import moe.nekojimi.chords.commands.Command; +import moe.nekojimi.chords.commands.Invocation; + +/** + * + * @author jimj316 + */ +public class PlaylistCommand extends Command +{ + + public PlaylistCommand(Main bot) + { + super(bot, "playlist"); + } + + @Override + public void call(Invocation invocation) + { + String name = invocation.getArgs().get(0); + Playlist list = bot.getPlaylists().get(name); + if (list == null) + { + invocation.respond("There's no playlist named \"" + name + "\"!"); + return; + } + + bot.getQueueManager().setPlaylist(list); + invocation.respond("Playing from list \"" + name + "\"!"); + } + +} diff --git a/src/main/java/moe/nekojimi/chords/commands/playlists/StopPlaylistCommand.java b/src/main/java/moe/nekojimi/chords/commands/playlists/StopPlaylistCommand.java new file mode 100644 index 0000000..8683919 --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/playlists/StopPlaylistCommand.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2022 jimj316 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package moe.nekojimi.chords.commands.playlists; + +import moe.nekojimi.chords.Main; +import moe.nekojimi.chords.commands.Command; +import moe.nekojimi.chords.commands.Invocation; + +/** + * + * @author jimj316 + */ +public class StopPlaylistCommand extends Command +{ + + public StopPlaylistCommand(Main bot) + { + super(bot, "stoplist"); + } + + @Override + public void call(Invocation invocation) + { + if (bot.getQueueManager().getPlaylist() == null) + { + invocation.respond("There's no playlist running!"); + return; + } + + bot.getQueueManager().setPlaylist(null); + invocation.respond("Playlist stopped!"); + } + +}