From 53608d9a9383b3380570d42228ab9c9919831043 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Mon, 31 Jul 2017 12:54:23 +0200 Subject: [PATCH] Externalized many strings, converted playlist view to tables, and made the playlist show command more pretty. --- .../commands/music/PlaylistCommand.java | 17 ++++-- .../command/commands/music/QueueCommand.java | 3 +- .../YoutubePlayListener.java | 6 +- .../YoutubePlaylistAddListener.java | 6 +- .../lavaplayer/playlist/Playlist.java | 40 ++++++++++--- .../lavaplayer/playlist/UnloadedTrack.java | 4 ++ .../java/handiebot/utils/YoutubeSearch.java | 8 ++- src/main/java/handiebot/view/BotWindow.java | 19 +++++-- .../{ => listeners}/CommandLineListener.java | 3 +- .../PlaylistSelectionListener.java | 8 +-- .../view/listeners/SongRenameListener.java | 56 +++++++++++++++++++ .../view/tableModels/PlaylistTableModel.java | 25 ++++++--- .../view/tableModels/SongsTableModel.java | 6 ++ src/main/resources/Strings.properties | 12 ++++ src/main/resources/Strings_nl.properties | 10 +++- 15 files changed, 183 insertions(+), 40 deletions(-) rename src/main/java/handiebot/view/{ => listeners}/CommandLineListener.java (97%) rename src/main/java/handiebot/view/{ => listeners}/PlaylistSelectionListener.java (88%) create mode 100644 src/main/java/handiebot/view/listeners/SongRenameListener.java diff --git a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java index af53b0c..bce518b 100644 --- a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java +++ b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java @@ -14,7 +14,9 @@ import handiebot.utils.YoutubeSearch; import handiebot.view.BotLog; import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IMessage; +import sx.blah.discord.util.EmbedBuilder; +import java.awt.*; import java.io.File; import java.text.MessageFormat; import java.util.ArrayList; @@ -148,14 +150,20 @@ public class PlaylistCommand extends ContextCommand { return; Playlist playlist = new Playlist(context.getArgs()[1]); playlist.load(); - sendMessage(playlist.toString(), context.getChannel()); + sendMessage(playlist.getEmbed(), context.getChannel()); } else { List playlists = Playlist.getAvailablePlaylists(); - StringBuilder sb = new StringBuilder("**Playlists:**\n"); + EmbedBuilder eb = new EmbedBuilder(); + eb.withTitle("Playlists:"); + eb.withColor(Color.red); + StringBuilder sb = new StringBuilder(); for (String playlist : playlists) { - sb.append(playlist).append('\n'); + Playlist p = new Playlist(playlist); + p.load(); + sb.append(p.getName()).append(", ").append(p.getTrackCount()).append(" tracks.\n"); } - sendMessage(sb.toString(), context.getChannel()); + eb.withDescription(sb.toString()); + sendMessage(eb.build(), context.getChannel()); } } @@ -176,7 +184,6 @@ public class PlaylistCommand extends ContextCommand { sendMessage(MessageFormat.format(resourceBundle.getString("commands.command.playlist.add.message"), playlist.getName()), context.getChannel()); } playlist.save(); - sendMessage(playlist.toString(), context.getChannel()); log.log(BotLog.TYPE.INFO, MessageFormat.format(resourceBundle.getString("commands.command.playlist.add.log"), playlist.getName())); } else { //This is a youtube search query. diff --git a/src/main/java/handiebot/command/commands/music/QueueCommand.java b/src/main/java/handiebot/command/commands/music/QueueCommand.java index 1ec2ab0..d535460 100644 --- a/src/main/java/handiebot/command/commands/music/QueueCommand.java +++ b/src/main/java/handiebot/command/commands/music/QueueCommand.java @@ -43,13 +43,14 @@ public class QueueCommand extends ContextCommand { } break; case ("save"): - //TODO: add some error messages so users know how to use this. if (context.getArgs().length == 2 && Commands.hasPermission(context, 8)) { Playlist p = HandieBot.musicPlayer.getAllSongsInQueue(context.getGuild()); p.setName(context.getArgs()[1]); p.save(); sendMessage(MessageFormat.format(resourceBundle.getString("commands.command.queue.save.message"), p.getTrackCount(), p.getName()), context.getChannel()); log.log(BotLog.TYPE.INFO, MessageFormat.format(resourceBundle.getString("commands.command.queue.save.log"), p.getName())); + } else { + sendMessage(resourceBundle.getString("commands.command.queue.error.save"), context.getChannel()); } break; } diff --git a/src/main/java/handiebot/command/reactionListeners/YoutubePlayListener.java b/src/main/java/handiebot/command/reactionListeners/YoutubePlayListener.java index 4adb29c..e32a676 100644 --- a/src/main/java/handiebot/command/reactionListeners/YoutubePlayListener.java +++ b/src/main/java/handiebot/command/reactionListeners/YoutubePlayListener.java @@ -6,16 +6,18 @@ import handiebot.view.BotLog; import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IUser; +import java.text.MessageFormat; import java.util.List; import static handiebot.HandieBot.log; +import static handiebot.HandieBot.resourceBundle; /** * @author Andrew Lalis * Specific Listener for choices in the Play command, where songs chosen are added to the active queue. */ public class YoutubePlayListener extends YoutubeChoiceListener { -//TODO: Externalize strings + public YoutubePlayListener(IMessage message, IUser user, List urls) { super(message, user, urls); } @@ -24,7 +26,7 @@ public class YoutubePlayListener extends YoutubeChoiceListener { protected void onChoice(int choice) { try { HandieBot.musicPlayer.addToQueue(message.getGuild(), new UnloadedTrack(urls.get(choice)), this.user); - log.log(BotLog.TYPE.MUSIC, message.getGuild(), this.user.getName()+" chose item "+(choice+1)+" from the Youtube query."); + log.log(BotLog.TYPE.MUSIC, message.getGuild(), MessageFormat.format(resourceBundle.getString("commands.youtube.choiceMade.log"), this.user.getName(), choice+1)); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/handiebot/command/reactionListeners/YoutubePlaylistAddListener.java b/src/main/java/handiebot/command/reactionListeners/YoutubePlaylistAddListener.java index d12febf..d5c390a 100644 --- a/src/main/java/handiebot/command/reactionListeners/YoutubePlaylistAddListener.java +++ b/src/main/java/handiebot/command/reactionListeners/YoutubePlaylistAddListener.java @@ -4,8 +4,10 @@ import handiebot.lavaplayer.playlist.Playlist; import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IUser; +import java.text.MessageFormat; import java.util.List; +import static handiebot.HandieBot.resourceBundle; import static handiebot.utils.MessageUtils.sendMessage; /** @@ -13,7 +15,7 @@ import static handiebot.utils.MessageUtils.sendMessage; * Specific Listener for adding songs to a playlist that must be saved. */ public class YoutubePlaylistAddListener extends YoutubeChoiceListener { - //TODO: externalize strings + private Playlist playlist; public YoutubePlaylistAddListener(IMessage message, IUser user, List urls, Playlist playlist) { @@ -25,6 +27,6 @@ public class YoutubePlaylistAddListener extends YoutubeChoiceListener { protected void onChoice(int choice) { this.playlist.loadTrack(this.urls.get(choice)); this.playlist.save(); - sendMessage("Added song to *"+this.playlist.getName()+"*.", message.getChannel()); + sendMessage(MessageFormat.format(resourceBundle.getString("commands.command.playlist.add.message"), this.playlist.getName()), message.getChannel()); } } diff --git a/src/main/java/handiebot/lavaplayer/playlist/Playlist.java b/src/main/java/handiebot/lavaplayer/playlist/Playlist.java index 7e91c9e..4e2561d 100644 --- a/src/main/java/handiebot/lavaplayer/playlist/Playlist.java +++ b/src/main/java/handiebot/lavaplayer/playlist/Playlist.java @@ -1,11 +1,16 @@ package handiebot.lavaplayer.playlist; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; +import handiebot.utils.Pastebin; import handiebot.view.BotLog; +import sx.blah.discord.api.internal.json.objects.EmbedObject; +import sx.blah.discord.util.EmbedBuilder; +import java.awt.*; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -22,7 +27,7 @@ import static handiebot.HandieBot.resourceBundle; * on the playlist. */ public class Playlist { -//TODO: Externalize strings. + private String name; private List tracks; @@ -104,9 +109,9 @@ public class Playlist { try { UnloadedTrack track = new UnloadedTrack(url); this.tracks.add(track); - log.log(BotLog.TYPE.MUSIC, "Added "+track.getTitle()+" to playlist ["+this.name+"]."); + log.log(BotLog.TYPE.MUSIC, MessageFormat.format(resourceBundle.getString("playlist.loadTrack.log"), track.getTitle(), this.name)); } catch (Exception e) { - log.log(BotLog.TYPE.ERROR, "Unable to add "+url+" to the playlist ["+this.name+"]."); + log.log(BotLog.TYPE.ERROR, MessageFormat.format(resourceBundle.getString("playlist.loadTrack.error"), url, this.name)); e.printStackTrace(); } } @@ -135,12 +140,11 @@ public class Playlist { File playlistDir = new File(homeDir+"/.handiebot/playlist"); if (!playlistDir.exists()){ if (!playlistDir.mkdirs()){ - log.log(BotLog.TYPE.ERROR, "Unable to make directory: "+playlistDir.getPath()); + log.log(BotLog.TYPE.ERROR, MessageFormat.format(resourceBundle.getString("playlist.save.error.directory"), playlistDir.getPath())); return; } } File playlistFile = new File(playlistDir.getPath()+"/"+this.name.replace(" ", "_")+".txt"); - log.log(BotLog.TYPE.INFO, "Saving playlist to: "+playlistFile.getAbsolutePath()); try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(playlistFile)))){ writer.write(Integer.toString(this.tracks.size())+'\n'); for (UnloadedTrack track : this.tracks){ @@ -148,7 +152,7 @@ public class Playlist { writer.write('\n'); } } catch (FileNotFoundException e) { - log.log(BotLog.TYPE.ERROR, "Unable to find file to write playlist: "+this.name); + log.log(BotLog.TYPE.ERROR, MessageFormat.format(resourceBundle.getString("playlist.save.error.fileNotFound"), this.name)); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); @@ -171,11 +175,11 @@ public class Playlist { this.tracks.add(new UnloadedTrack(words[0], words[1], Long.parseLong(words[2]))); } } catch (IOException e) { - log.log(BotLog.TYPE.ERROR, "IOException while loading playlist ["+this.name+"]. "+e.getMessage()); + log.log(BotLog.TYPE.ERROR, MessageFormat.format(resourceBundle.getString("playlist.load.error.IOException"), this.name, e.getMessage())); e.printStackTrace(); } } else { - log.log(BotLog.TYPE.ERROR, "The playlist ["+this.name+"] does not exist."); + log.log(BotLog.TYPE.ERROR, MessageFormat.format(resourceBundle.getString("playlist.load.error.exists"), this.name)); } } @@ -223,4 +227,24 @@ public class Playlist { return sb.toString(); } + public EmbedObject getEmbed(){ + EmbedBuilder eb = new EmbedBuilder(); + eb.withTitle(this.getName()); + eb.withFooterText(this.getTrackCount()+" tracks."); + eb.withColor(Color.red); + StringBuilder sb = new StringBuilder(); + boolean needsPastebin = this.getTrackCount() > 20; + for (int i = 0; i < this.getTrackCount(); i++){ + sb.append(i+1).append(". ").append(this.tracks.get(i).getTitle()).append(' ').append(this.tracks.get(i).getFormattedDuration()).append('\n'); + } + if (needsPastebin){ + String result = Pastebin.paste(this.getName(), sb.toString()); + eb.withUrl(result); + eb.withDescription(resourceBundle.getString("playlist.embedTooLarge")); + } else { + eb.withDescription(sb.toString()); + } + return eb.build(); + } + } diff --git a/src/main/java/handiebot/lavaplayer/playlist/UnloadedTrack.java b/src/main/java/handiebot/lavaplayer/playlist/UnloadedTrack.java index e2315c1..247a6a6 100644 --- a/src/main/java/handiebot/lavaplayer/playlist/UnloadedTrack.java +++ b/src/main/java/handiebot/lavaplayer/playlist/UnloadedTrack.java @@ -92,6 +92,10 @@ public class UnloadedTrack implements Cloneable { return this.title; } + public void setTitle(String newTitle){ + this.title = newTitle; + } + public String getURL(){ return this.url; } diff --git a/src/main/java/handiebot/utils/YoutubeSearch.java b/src/main/java/handiebot/utils/YoutubeSearch.java index ef435af..182e59b 100644 --- a/src/main/java/handiebot/utils/YoutubeSearch.java +++ b/src/main/java/handiebot/utils/YoutubeSearch.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.security.GeneralSecurityException; +import java.text.MessageFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Arrays; @@ -36,6 +37,7 @@ import java.util.List; import java.util.Locale; import static handiebot.HandieBot.APPLICATION_NAME; +import static handiebot.HandieBot.resourceBundle; import static handiebot.utils.MessageUtils.addReaction; import static handiebot.utils.MessageUtils.sendMessage; @@ -44,7 +46,7 @@ import static handiebot.utils.MessageUtils.sendMessage; * Class to query Youtube Data API for results to searches, and return these in a nice list. */ public class YoutubeSearch { -//TODO: Externalize Strings + private static final String KEY = "AIzaSyAjYuxCYBCuZCNvW4w573LQ-jw5UKL64G8"; private static final int NUMBER_OF_VIDEOS_RETURNED = 5; @@ -153,7 +155,7 @@ public class YoutubeSearch { public static EmbedObject createEmbed(List