Add playlist commands.

playlists
Nekojimi 3 years ago
parent c08b7c955f
commit a451369c92
  1. 49
      src/main/java/moe/nekojimi/chords/Main.java
  2. 2
      src/main/java/moe/nekojimi/chords/MusicHandler.java
  3. 14
      src/main/java/moe/nekojimi/chords/Playlist.java
  4. 2
      src/main/java/moe/nekojimi/chords/QueueManager.java
  5. 66
      src/main/java/moe/nekojimi/chords/commands/playlists/AddToPlaylistCommand.java
  6. 52
      src/main/java/moe/nekojimi/chords/commands/playlists/MakePlaylistCommand.java
  7. 51
      src/main/java/moe/nekojimi/chords/commands/playlists/PlaylistCommand.java
  8. 48
      src/main/java/moe/nekojimi/chords/commands/playlists/StopPlaylistCommand.java

@ -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;
@ -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(null); playCommand.call(new Invocation(event, List.of(parseURL.toExternalForm())));
} catch (MalformedURLException ex) } catch (MalformedURLException ex)
{ {
// not a URL, then // 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<Song> destination)
{ {
// TODO: move this logic somewhere better
Song song; Song song;
if (request.getUrl() != null) if (request.getUrl() != null)
{ {
@ -274,8 +285,8 @@ public final class Main extends ListenerAdapter
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;
} }
@ -375,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)
{ {
@ -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() public MusicHandler getMusicHandler()
{ {
return musicHandler; return musicHandler;
@ -417,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;
}
} }

@ -94,6 +94,8 @@ public class MusicHandler implements AudioSendHandler, Closeable
if (!currentSong.isKept()) if (!currentSong.isKept())
currentSong.delete(); currentSong.delete();
currentSong = null; currentSong = null;
player.close();
player = null;
} }
currentSong = queueManager.nextSongNeeded(); currentSong = queueManager.nextSongNeeded();
if (nowPlayingConsumer != null) if (nowPlayingConsumer != null)

@ -36,6 +36,7 @@ public class Playlist implements Consumer<Song>
private static final int SHUFFLE_DONT_REPEAT_LAST = 3; private static final int SHUFFLE_DONT_REPEAT_LAST = 3;
private final String name; private final String name;
private Main bot;
private final List<Song> songs = new ArrayList<>(); private final List<Song> songs = new ArrayList<>();
private final LinkedList<Song> playHistory = new LinkedList<>(); private final LinkedList<Song> playHistory = new LinkedList<>();
@ -77,6 +78,9 @@ public class Playlist implements Consumer<Song>
{ {
song.setKept(true); song.setKept(true);
songs.add(song); songs.add(song);
if (bot != null)
bot.savePlaylist(this);
} }
public String getName() public String getName()
@ -119,4 +123,14 @@ public class Playlist implements Consumer<Song>
addSong(t); addSong(t);
} }
public Main getBot()
{
return bot;
}
public void setBot(Main bot)
{
this.bot = bot;
}
} }

@ -114,6 +114,8 @@ public class QueueManager implements Consumer<Song>
public void setPlaylist(Playlist playlist) public void setPlaylist(Playlist playlist)
{ {
this.playlist = playlist; this.playlist = playlist;
if (playlist == null || !handler.isPlaying())
handler.playNext();
} }
public boolean restartSong() public boolean restartSong()

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}
}

@ -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 <http://www.gnu.org/licenses/>.
*/
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!");
}
}

@ -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 <http://www.gnu.org/licenses/>.
*/
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 + "\"!");
}
}

@ -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 <http://www.gnu.org/licenses/>.
*/
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!");
}
}
Loading…
Cancel
Save