Add playlist commands.
This commit is contained in:
parent
c08b7c955f
commit
a451369c92
|
@ -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<Song> 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<String, Playlist> getPlaylists()
|
||||
{
|
||||
return playlists;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -36,6 +36,7 @@ public class Playlist implements Consumer<Song>
|
|||
private static final int SHUFFLE_DONT_REPEAT_LAST = 3;
|
||||
|
||||
private final String name;
|
||||
private Main bot;
|
||||
private final List<Song> songs = new ArrayList<>();
|
||||
private final LinkedList<Song> playHistory = new LinkedList<>();
|
||||
|
||||
|
@ -77,6 +78,9 @@ public class Playlist implements Consumer<Song>
|
|||
{
|
||||
song.setKept(true);
|
||||
songs.add(song);
|
||||
|
||||
if (bot != null)
|
||||
bot.savePlaylist(this);
|
||||
}
|
||||
|
||||
public String getName()
|
||||
|
@ -119,4 +123,14 @@ public class Playlist implements Consumer<Song>
|
|||
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)
|
||||
{
|
||||
this.playlist = playlist;
|
||||
if (playlist == null || !handler.isPlaying())
|
||||
handler.playNext();
|
||||
}
|
||||
|
||||
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…
Reference in New Issue