From 197437996a3285b0c78e64745640b241780c48ae Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Mon, 26 Oct 2020 16:22:54 +0100 Subject: [PATCH] Changed the command interface to an abstract method and added better error handling Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MovieQuoteBot.java | 6 ++-- .../xyz/vallat/louis/commands/Command.java | 34 +++++++++++++++++-- .../java/xyz/vallat/louis/commands/Ping.java | 6 +++- .../xyz/vallat/louis/commands/Search.java | 33 +++++++++++++----- .../xyz/vallat/louis/commands/Version.java | 6 +++- 5 files changed, 70 insertions(+), 15 deletions(-) diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index 596f5cf..5b383fc 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -35,9 +35,9 @@ public class MovieQuoteBot { private static GatewayDiscordClient discordClient; static { - commands.put("ping", new Ping()); - commands.put("version", new Version()); - commands.put("search", new Search()); + commands.put("ping", new Ping(PREFIX + "ping")); + commands.put("version", new Version(PREFIX + "version")); + commands.put("search", new Search(PREFIX + "search")); } public static void main(String[] args) { diff --git a/src/main/java/xyz/vallat/louis/commands/Command.java b/src/main/java/xyz/vallat/louis/commands/Command.java index 1bba91f..ed7227a 100644 --- a/src/main/java/xyz/vallat/louis/commands/Command.java +++ b/src/main/java/xyz/vallat/louis/commands/Command.java @@ -3,7 +3,37 @@ package xyz.vallat.louis.commands; import discord4j.core.event.domain.message.MessageCreateEvent; import reactor.core.publisher.Mono; -public interface Command { +public abstract class Command { - Mono execute(MessageCreateEvent event); + protected final String name; + protected final String description; + protected final String usage; + protected final int minArgs; + protected final int maxArgs; + + Command(String name, String description, String usage, int minArgs, int maxArgs) { + this.name = name; + this.description = description; + this.usage = usage; + this.minArgs = minArgs; + this.maxArgs = maxArgs; + } + + public abstract Mono execute(MessageCreateEvent event); + + public String getUsage() { + return this.usage; + } + + public String getDescription() { + return this.description; + } + + public int getMinArgs() { + return minArgs; + } + + public int getMaxArgs() { + return maxArgs; + } } diff --git a/src/main/java/xyz/vallat/louis/commands/Ping.java b/src/main/java/xyz/vallat/louis/commands/Ping.java index ad38690..f827f3e 100644 --- a/src/main/java/xyz/vallat/louis/commands/Ping.java +++ b/src/main/java/xyz/vallat/louis/commands/Ping.java @@ -3,7 +3,11 @@ package xyz.vallat.louis.commands; import discord4j.core.event.domain.message.MessageCreateEvent; import reactor.core.publisher.Mono; -public class Ping implements Command { +public class Ping extends Command { + + public Ping(String name) { + super(name, "Replies as soon as possible to check the bot's health.", name, 0, 0); + } @Override public Mono execute(MessageCreateEvent event) { diff --git a/src/main/java/xyz/vallat/louis/commands/Search.java b/src/main/java/xyz/vallat/louis/commands/Search.java index fef97c2..8d86c4b 100644 --- a/src/main/java/xyz/vallat/louis/commands/Search.java +++ b/src/main/java/xyz/vallat/louis/commands/Search.java @@ -2,34 +2,51 @@ package xyz.vallat.louis.commands; import com.github.wtekiela.opensub4j.response.ListResponse; import com.github.wtekiela.opensub4j.response.MovieInfo; +import com.github.wtekiela.opensub4j.response.ResponseStatus; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.reaction.ReactionEmoji; import org.apache.xmlrpc.XmlRpcException; import reactor.core.publisher.Mono; import xyz.vallat.louis.subtitles.OpenSubtitles; -public class Search implements Command { +public class Search extends Command { private static final ReactionEmoji WAITING = ReactionEmoji.unicode("⌛"); + String format = "%1$-7s - %2$s\n"; + + public Search(String name) { + super(name, "A command to search some subtitles.", name + " ", 1, 1); + } @Override public Mono execute(MessageCreateEvent event) { String[] args = event.getMessage().getContent().split(" "); - if (args.length != 1) return event.getMessage().getChannel().flatMap(channel -> channel.createMessage("Misuse")).then(); + if (args.length < minArgs) + return event.getMessage().getChannel() + .flatMap(channel -> channel.createMessage("Error.\n" + getUsage())) + .then(); return event.getMessage().getChannel() .flatMap(channel -> { if (OpenSubtitles.isLoggedIn()) { return event.getMessage().addReaction(WAITING) - .then(event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createMessage(args[1]))) .then(event.getMessage().getChannel().flatMap(messageChannel -> { try { ListResponse response = OpenSubtitles.searchImdb(args[1]); - StringBuilder b = new StringBuilder(); - for (MovieInfo m : response.getData()) - b.append(m.getId()).append(" > ").append(m.getTitle()).append("\n"); - return messageChannel.createMessage(String.valueOf(b)); + if (response.getStatus() == ResponseStatus.OK) { + return messageChannel.createEmbed( + embed -> { + embed.setTitle("Results") + .setDescription("Found " + response.getData().size() + " results."); + StringBuilder s = new StringBuilder("```txt\n"); + for (MovieInfo info : response.getData().subList(0, 10)) + s.append(String.format(format, info.getId(), info.getTitle())); + s.append("```"); + embed.addField("First 10 results:", s.toString(), false); + }); + } else + return messageChannel.createMessage("An error happened while fetching the results. Try again later, or contact my administrator."); } catch (XmlRpcException e) { - return channel.createMessage("Error"); + return channel.createMessage("It seems like OpenSubtitles had a stroke."); } })) .then(event.getMessage().removeSelfReaction(WAITING)) diff --git a/src/main/java/xyz/vallat/louis/commands/Version.java b/src/main/java/xyz/vallat/louis/commands/Version.java index 5a52558..fde8c97 100644 --- a/src/main/java/xyz/vallat/louis/commands/Version.java +++ b/src/main/java/xyz/vallat/louis/commands/Version.java @@ -5,7 +5,11 @@ import discord4j.rest.util.Color; import reactor.core.publisher.Mono; import xyz.vallat.louis.MovieQuoteBot; -public class Version implements Command { +public class Version extends Command { + + public Version(String name) { + super(name, "Give some information about the bot.", name, 0, 0); + } @Override public Mono execute(MessageCreateEvent event) {