From c08b7c955f0b8d9ffb18c86e8d4ceacbc7fcf3cd Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Mon, 20 Jun 2022 17:12:47 +0100 Subject: [PATCH] Add new invocation system to command structure. --- src/main/java/moe/nekojimi/chords/Main.java | 30 ++++--- .../java/moe/nekojimi/chords/SongRequest.java | 60 ++++++------- .../moe/nekojimi/chords/commands/Command.java | 4 +- .../nekojimi/chords/commands/HelpCommand.java | 6 +- .../nekojimi/chords/commands/Invocation.java | 89 +++++++++++++++++++ .../nekojimi/chords/commands/JoinCommand.java | 5 +- .../chords/commands/LeaveCommand.java | 2 +- .../nekojimi/chords/commands/PlayCommand.java | 25 +++--- .../chords/commands/QueueCommand.java | 5 +- .../chords/commands/RemoveCommand.java | 14 ++- .../chords/commands/RestartCommand.java | 8 +- .../nekojimi/chords/commands/SkipCommand.java | 8 +- 12 files changed, 164 insertions(+), 92 deletions(-) create mode 100644 src/main/java/moe/nekojimi/chords/commands/Invocation.java diff --git a/src/main/java/moe/nekojimi/chords/Main.java b/src/main/java/moe/nekojimi/chords/Main.java index 5e9e33d..1c745ff 100644 --- a/src/main/java/moe/nekojimi/chords/Main.java +++ b/src/main/java/moe/nekojimi/chords/Main.java @@ -110,7 +110,7 @@ public final class Main extends ListenerAdapter if (ex == 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); } else { @@ -124,12 +124,12 @@ public final class Main extends ListenerAdapter String bitFmt = (bitrate <= 0 ? "??" : bitrate) + "k"; formatDetails = " (" + bitFmt + ", " + sizeFmt + ")"; } - request.respond("Now downloading " + song + formatDetails + " ..."); + request.getInvocation().respond("Now downloading " + song + formatDetails + " ..."); log("DOWN", "Downloading " + song + "..."); } 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()); } @@ -216,7 +216,7 @@ public final class Main extends ListenerAdapter try { URL parseURL = new URL(content.trim()); - playCommand.call(event, List.of(parseURL.toExternalForm())); + playCommand.call(null); } catch (MalformedURLException ex) { // not a URL, then @@ -224,7 +224,7 @@ public final class Main extends ListenerAdapter try { - String[] split = content.split("\\s+", 2); + String[] split = content.split("\\s+"); String cmd = split[0].toLowerCase(); if (!cmd.startsWith("!")) @@ -232,17 +232,21 @@ public final class Main extends ListenerAdapter cmd = cmd.substring(1); // strip prefix char - String arg = ""; - if (split.length > 1) - arg = split[1]; +// String arg = ""; + List args = new ArrayList<>(); + Collections.addAll(args, split); + args.remove(0); + + Invocation invocation = new Invocation(event, args); + invocation.setRequestMessage(message); if (commands.containsKey(cmd)) { Command command = commands.get(cmd); - command.call(event, List.of(arg)); + command.call(invocation); } else { - helpCommand.call(event, List.of(arg)); + helpCommand.call(invocation); } } catch (Exception ex) { @@ -262,10 +266,10 @@ public final class Main extends ListenerAdapter // interpret search result throw new UnsupportedOperationException("Not supported yet."); } - if (request.getRequestMessage() != null) + if (request.getInvocation().getRequestMessage() != null) { - song.setRequestedBy(request.getRequestMessage().getAuthor().getName()); - song.setRequestedIn(request.getRequestMessage().getChannel().getId()); + song.setRequestedBy(request.getInvocation().getRequestMessage().getAuthor().getName()); + song.setRequestedIn(request.getInvocation().getRequestMessage().getChannel().getId()); } song.setNumber(trackNumber); trackNumber++; diff --git a/src/main/java/moe/nekojimi/chords/SongRequest.java b/src/main/java/moe/nekojimi/chords/SongRequest.java index 6ae1fd1..bd4fa00 100644 --- a/src/main/java/moe/nekojimi/chords/SongRequest.java +++ b/src/main/java/moe/nekojimi/chords/SongRequest.java @@ -18,11 +18,8 @@ package moe.nekojimi.chords; import java.net.URL; import java.util.List; +import moe.nekojimi.chords.commands.Invocation; import moe.nekojimi.musicsearcher.Result; -import net.dv8tion.jda.api.MessageBuilder; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.requests.restaction.MessageAction; -import net.dv8tion.jda.internal.entities.DataMessage; /** * @@ -30,9 +27,7 @@ import net.dv8tion.jda.internal.entities.DataMessage; */ public class SongRequest { - - private Message requestMessage; - private Message responseMessage; + private Invocation invocation; private String query; private URL url; @@ -43,21 +38,6 @@ public class SongRequest private Song song; - @SuppressWarnings("null") - public void respond(String text) - { - MessageAction action = null; - - if (responseMessage == null) - { - action = requestMessage.reply(text); - } else - { - action = responseMessage.editMessage(text); - } - - responseMessage = action.complete(); - } public List getSearchResults() @@ -80,25 +60,35 @@ public class SongRequest this.result = result; } - public Message getRequestMessage() + public Invocation getInvocation() { - return requestMessage; + return invocation; } - public void setRequestMessage(Message requestMessage) + public void setInvocation(Invocation invocation) { - this.requestMessage = requestMessage; + this.invocation = invocation; } - public Message getResponseMessage() - { - return responseMessage; - } - - public void setResponseMessage(Message responseMessage) - { - this.responseMessage = responseMessage; - } +// public Message getRequestMessage() +// { +// return requestMessage; +// } +// +// public void setRequestMessage(Message requestMessage) +// { +// this.requestMessage = requestMessage; +// } +// +// public Message getResponseMessage() +// { +// return responseMessage; +// } +// +// public void setResponseMessage(Message responseMessage) +// { +// this.responseMessage = responseMessage; +// } public String getQuery() { diff --git a/src/main/java/moe/nekojimi/chords/commands/Command.java b/src/main/java/moe/nekojimi/chords/commands/Command.java index 77f145d..035a4b7 100644 --- a/src/main/java/moe/nekojimi/chords/commands/Command.java +++ b/src/main/java/moe/nekojimi/chords/commands/Command.java @@ -5,9 +5,7 @@ */ package moe.nekojimi.chords.commands; -import java.util.List; import moe.nekojimi.chords.Main; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; /** * @@ -26,7 +24,7 @@ public abstract class Command this.keyword = keyword; } - public abstract void call(GuildMessageReceivedEvent event, List arg); + public abstract void call(Invocation invocation); public String getKeyword() { diff --git a/src/main/java/moe/nekojimi/chords/commands/HelpCommand.java b/src/main/java/moe/nekojimi/chords/commands/HelpCommand.java index f0d3e6f..ae11bcc 100644 --- a/src/main/java/moe/nekojimi/chords/commands/HelpCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/HelpCommand.java @@ -16,9 +16,7 @@ */ package moe.nekojimi.chords.commands; -import java.util.List; import moe.nekojimi.chords.Main; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class HelpCommand extends Command { @@ -29,7 +27,7 @@ public class HelpCommand extends Command } @Override - public void call(GuildMessageReceivedEvent event, List arg) + public void call(Invocation invocation) { String help = "Commands available:\n" + "!join - Joins a voice channel\n" @@ -44,7 +42,7 @@ public class HelpCommand extends Command // { // help += "!" + key + ":" // } - event.getChannel().sendMessage(help).queue(); + invocation.respond(help); } } diff --git a/src/main/java/moe/nekojimi/chords/commands/Invocation.java b/src/main/java/moe/nekojimi/chords/commands/Invocation.java new file mode 100644 index 0000000..ad36c07 --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/Invocation.java @@ -0,0 +1,89 @@ +/* + * 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; + +import java.util.List; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import net.dv8tion.jda.api.requests.restaction.MessageAction; + +/** + * + * @author jimj316 + */ +public class Invocation +{ + + private Message requestMessage; + private Message responseMessage; + + private final GuildMessageReceivedEvent event; + private final List args; + + public Invocation(GuildMessageReceivedEvent event, List args) + { + this.event = event; + this.args = args; + } + + public GuildMessageReceivedEvent getEvent() + { + return event; + } + + public List getArgs() + { + return args; + } + + @SuppressWarnings("null") + public void respond(String text) + { + MessageAction action = null; + + if (responseMessage == null) + { + action = requestMessage.reply(text); + } else + { + action = responseMessage.editMessage(text); + } + + responseMessage = action.complete(); + } + + public Message getRequestMessage() + { + return requestMessage; + } + + public void setRequestMessage(Message requestMessage) + { + this.requestMessage = requestMessage; + } + + public Message getResponseMessage() + { + return responseMessage; + } + + public void setResponseMessage(Message responseMessage) + { + this.responseMessage = responseMessage; + } + +} diff --git a/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java b/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java index a6af543..8b4e846 100644 --- a/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java @@ -6,7 +6,6 @@ package moe.nekojimi.chords.commands; import java.util.List; -import java.util.Optional; import moe.nekojimi.chords.Main; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; @@ -20,8 +19,10 @@ public class JoinCommand extends Command } @Override - public void call(GuildMessageReceivedEvent event, List args) + public void call(Invocation invocation) { + GuildMessageReceivedEvent event = invocation.getEvent(); + List args = invocation.getArgs(); TextChannel textChannel = event.getChannel(); VoiceChannel channel = null; if (args.isEmpty() || args.get(0).isBlank()) diff --git a/src/main/java/moe/nekojimi/chords/commands/LeaveCommand.java b/src/main/java/moe/nekojimi/chords/commands/LeaveCommand.java index 76f0d4d..7d68cfe 100644 --- a/src/main/java/moe/nekojimi/chords/commands/LeaveCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/LeaveCommand.java @@ -18,7 +18,7 @@ public class LeaveCommand extends Command } @Override - public void call(GuildMessageReceivedEvent event, List arg) + public void call(Invocation invocation) { if (bot.getCurrentVoiceChannel() != null) { diff --git a/src/main/java/moe/nekojimi/chords/commands/PlayCommand.java b/src/main/java/moe/nekojimi/chords/commands/PlayCommand.java index 3e221e4..e5a6a49 100644 --- a/src/main/java/moe/nekojimi/chords/commands/PlayCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/PlayCommand.java @@ -25,7 +25,6 @@ import moe.nekojimi.chords.Main; import moe.nekojimi.chords.SongRequest; import moe.nekojimi.musicsearcher.Query; import moe.nekojimi.musicsearcher.Result; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; /** * @@ -44,13 +43,13 @@ public class PlayCommand extends Command } @Override - public void call(GuildMessageReceivedEvent event, List arg) + public void call(Invocation invocation) { SongRequest request = new SongRequest(); - request.setRequestMessage(event.getMessage()); + request.setInvocation(invocation); try { - final URL url = new URL(arg.get(0)); + final URL url = new URL(invocation.getArgs().get(0)); request.setUrl(url); bot.queueDownload(request); @@ -61,18 +60,18 @@ public class PlayCommand extends Command { try { - int index = Integer.parseInt(arg.get(0)); + int index = Integer.parseInt(invocation.getArgs().get(0)); int size = request.getSearchResults().size(); if (index >= 1 && index <= size) { Result result = request.getSearchResults().get(index - 1); request.setResult(result); - bot.queueDownload(request); + bot.queueDownload(request, bot.getQueueManager()); // event.getChannel().sendMessage("Song removed.").queue(); } else if (size > 1) - event.getChannel().sendMessage("That's not a number between 1 and " + size + "!").queue(); + invocation.respond("That's not a number between 1 and " + size + "!"); else if (size == 1) - event.getChannel().sendMessage("There's only one song and that's not one of them!").queue(); + invocation.respond("There's only one song and that's not one of them!"); return; } catch (NumberFormatException nfx) @@ -82,13 +81,13 @@ public class PlayCommand extends Command } // otherwise, try searching - CompletableFuture> search = bot.getSearcher().search(Query.fullText(arg.stream().reduce((t, u) -> t + " " + u).get())); - event.getChannel().sendMessage("Searching for \"" + arg + "\" ...").queue(); + CompletableFuture> search = bot.getSearcher().search(Query.fullText(invocation.getArgs().stream().reduce((t, u) -> t + " " + u).get())); + invocation.respond("Searching for \"" + invocation.getArgs() + "\" ..."); search.orTimeout(30, TimeUnit.SECONDS).whenCompleteAsync((List results, Throwable exec) -> { if (exec != null) { - event.getChannel().sendMessage("Failed to search! Reason: " + exec.getMessage()).queue(); + invocation.respond("Failed to search! Reason: " + exec.getMessage()); return; } @@ -97,7 +96,7 @@ public class PlayCommand extends Command if (results.isEmpty()) { - event.getChannel().sendMessage("Found nothing! :(").queue(); + invocation.respond("Found nothing! :("); return; } @@ -125,7 +124,7 @@ public class PlayCommand extends Command i++; } resultString += "Type eg. `!play 1` to select"; - event.getChannel().sendMessage(resultString).queue(); + invocation.respond(resultString); }); // event.getChannel().sendMessage("That's not a valid URL you idiot! " + ex.getMessage()).queue(); } diff --git a/src/main/java/moe/nekojimi/chords/commands/QueueCommand.java b/src/main/java/moe/nekojimi/chords/commands/QueueCommand.java index e20f8f8..c61bb6e 100644 --- a/src/main/java/moe/nekojimi/chords/commands/QueueCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/QueueCommand.java @@ -21,7 +21,6 @@ import java.util.Queue; import moe.nekojimi.chords.Downloader; import moe.nekojimi.chords.Main; import moe.nekojimi.chords.Song; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class QueueCommand extends Command { @@ -32,7 +31,7 @@ public class QueueCommand extends Command } @Override - public void call(GuildMessageReceivedEvent event, List arg) + public void call(Invocation invocation) { String message = ">>> "; int i = 1; @@ -68,7 +67,7 @@ public class QueueCommand extends Command if (downloadQueue.isEmpty() && songQueue.isEmpty()) message += ":mailbox_with_no_mail: The track queue is empty."; // :inbox_tray: - event.getChannel().sendMessage(message).queue(); + invocation.respond(message); } } diff --git a/src/main/java/moe/nekojimi/chords/commands/RemoveCommand.java b/src/main/java/moe/nekojimi/chords/commands/RemoveCommand.java index b68b682..8132c8c 100644 --- a/src/main/java/moe/nekojimi/chords/commands/RemoveCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/RemoveCommand.java @@ -16,9 +16,7 @@ */ package moe.nekojimi.chords.commands; -import java.util.List; import moe.nekojimi.chords.Main; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class RemoveCommand extends Command { @@ -29,23 +27,23 @@ public class RemoveCommand extends Command } @Override - public void call(GuildMessageReceivedEvent event, List arg) + public void call(Invocation invocation) { try { - int i = Integer.parseInt(arg.get(0)); + int i = Integer.parseInt(invocation.getArgs().get(0)); boolean removed = bot.getQueueManager().removeSong(i - 1); final int size = bot.getQueueManager().getJukeboxQueue().size(); if (removed) - event.getChannel().sendMessage("Song removed.").queue(); + invocation.respond("Song removed."); else if (size > 1) - event.getChannel().sendMessage("That's not a number between 1 and " + size + "!").queue(); + invocation.respond("That's not a number between 1 and " + size + "!"); else if (size == 1) - event.getChannel().sendMessage("There's only one song to remove and that's not one of them!").queue(); + invocation.respond("There's only one song to remove and that's not one of them!"); } catch (NumberFormatException ex) { - event.getChannel().sendMessage(arg + " isn't a number!").queue(); + invocation.respond(invocation.getArgs().get(0) + " isn't a number!"); } } diff --git a/src/main/java/moe/nekojimi/chords/commands/RestartCommand.java b/src/main/java/moe/nekojimi/chords/commands/RestartCommand.java index ee17125..7c065ad 100644 --- a/src/main/java/moe/nekojimi/chords/commands/RestartCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/RestartCommand.java @@ -16,9 +16,7 @@ */ package moe.nekojimi.chords.commands; -import java.util.List; import moe.nekojimi.chords.Main; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class RestartCommand extends Command { @@ -29,14 +27,14 @@ public class RestartCommand extends Command } @Override - public void call(GuildMessageReceivedEvent event, List arg) + public void call(Invocation invocation) { // TODO: this needs to clear the current data queue boolean ok = bot.getQueueManager().restartSong(); if (ok) - event.getChannel().sendMessage("Restarted current song!").queue(); + invocation.respond("Restarted current song!"); else - event.getChannel().sendMessage("Cannot restart!").queue(); + invocation.respond("Cannot restart!"); } } diff --git a/src/main/java/moe/nekojimi/chords/commands/SkipCommand.java b/src/main/java/moe/nekojimi/chords/commands/SkipCommand.java index 6cfe5fc..8a1b2ed 100644 --- a/src/main/java/moe/nekojimi/chords/commands/SkipCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/SkipCommand.java @@ -16,9 +16,7 @@ */ package moe.nekojimi.chords.commands; -import java.util.List; import moe.nekojimi.chords.Main; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class SkipCommand extends Command { @@ -29,13 +27,13 @@ public class SkipCommand extends Command } @Override - public void call(GuildMessageReceivedEvent event, List arg) + public void call(Invocation invocation) { boolean ok = bot.getMusicHandler().nextSong(true); if (ok) - event.getChannel().sendMessage("Skipped to next song!").queue(); + invocation.respond("Skipped to next song!"); else - event.getChannel().sendMessage("There's no more songs!").queue(); + invocation.respond("There's no more songs!"); } }