From f0e16f6b6eda3a6cb62068ce62b24e95de378b84 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Thu, 29 Oct 2020 15:09:30 +0100 Subject: [PATCH] Added help command Signed-off-by: Louis Vallat --- .../xyz/vallat/louis/commands/Command.java | 5 +++- .../xyz/vallat/louis/commands/Download.java | 2 +- .../java/xyz/vallat/louis/commands/Help.java | 29 +++++++++++++++++++ .../vallat/louis/commands/ListLangMovie.java | 2 +- .../vallat/louis/discord/DiscordManager.java | 27 ++++++++++------- 5 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 src/main/java/xyz/vallat/louis/commands/Help.java diff --git a/src/main/java/xyz/vallat/louis/commands/Command.java b/src/main/java/xyz/vallat/louis/commands/Command.java index 87fbdab..01a82d4 100644 --- a/src/main/java/xyz/vallat/louis/commands/Command.java +++ b/src/main/java/xyz/vallat/louis/commands/Command.java @@ -16,7 +16,6 @@ import xyz.vallat.louis.omdb.objects.Movie; import xyz.vallat.louis.subtitles.OpenSubtitles; import java.io.IOException; -import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -67,6 +66,10 @@ public abstract class Command { return maxArgs; } + public String getName() { + return name; + } + protected Mono notEnoughArguments(MessageCreateEvent event) { return event.getMessage().getChannel() .flatMap(channel -> channel.createMessage("Error, you're missing some arguments.\n" + getUsage())) diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java index ab7e1ca..e06aacd 100644 --- a/src/main/java/xyz/vallat/louis/commands/Download.java +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -81,7 +81,7 @@ public class Download extends Command { subs.getData().get(0).getContentAsString("cp1252")); SubtitleLineManager.importSubtitleLines(blocks, languageId, movie, event.getGuildId().isPresent() ? event.getGuildId().get() : null, - event.getMember().isPresent() ? event.getMessage().getId() : null); + event.getMember().isPresent() ? event.getMember().get().getId() : null); embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " + "Congratulations and thank you for your contribution!"); embed.addField("You imported", blocks.size() + " lines into my database", false); diff --git a/src/main/java/xyz/vallat/louis/commands/Help.java b/src/main/java/xyz/vallat/louis/commands/Help.java new file mode 100644 index 0000000..93c403f --- /dev/null +++ b/src/main/java/xyz/vallat/louis/commands/Help.java @@ -0,0 +1,29 @@ +package xyz.vallat.louis.commands; + +import discord4j.core.event.domain.message.MessageCreateEvent; +import discord4j.rest.util.Color; +import reactor.core.publisher.Mono; +import xyz.vallat.louis.discord.DiscordManager; + +import java.time.Instant; + +public class Help extends Command { + + public Help(String name) { + super(name, "I need help. You need help. We all need help at some point.", name, 0, 0); + } + + @Override + public Mono execute(MessageCreateEvent event) { + return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> { + embed.setColor(Color.MEDIUM_SEA_GREEN).setTimestamp(Instant.now()).setTitle("Help") + .setDescription("This is all the commands I know."); + for (Command command : DiscordManager.getCommands()) + embed.addField(command.getName(), + command.getDescription() + " Usage: " + command.getUsage() + ".", false); + } + )).then(); + } +} + + diff --git a/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java b/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java index 7f66903..db7d05f 100644 --- a/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java +++ b/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java @@ -14,7 +14,7 @@ public class ListLangMovie extends Command { private static final Logger logger = LoggerFactory.getLogger(ListLangMovie.class.getCanonicalName()); public ListLangMovie(String name) { - super(name, "List all languages attached to a film title or an IMDB Identifier.", + super(name, "List all languages attached to a movie title or an IMDB Identifier.", name + " imdb|title value", 2, 2); } diff --git a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java index 1ca8472..06f21d0 100644 --- a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java +++ b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java @@ -13,37 +13,42 @@ import reactor.core.publisher.Mono; import xyz.vallat.louis.commands.*; import xyz.vallat.louis.env.EnvironmentVariables; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import static xyz.vallat.louis.MovieQuoteBot.PREFIX; public final class DiscordManager { + private static final List commands = new ArrayList<>(); private static final Logger logger = LoggerFactory.getLogger(DiscordManager.class.getCanonicalName()); - private static final Map commands = new HashMap<>(); private static GatewayDiscordClient discordClient; static { - commands.put("ping", new Ping(PREFIX + "ping")); - commands.put("version", new Version(PREFIX + "version")); - commands.put("listLangMovie", new ListLangMovie(PREFIX + "listLangMovie")); - commands.put("download", new Download(PREFIX + "download")); - commands.put("quote", new Quote(PREFIX + "quote")); + commands.add(new ListLangMovie(PREFIX + "listLangMovie")); + commands.add(new Download(PREFIX + "download")); + commands.add(new Quote(PREFIX + "quote")); + commands.add(new Ping(PREFIX + "ping")); + commands.add(new Help(PREFIX + "help")); + commands.add(new Version(PREFIX + "version")); } private DiscordManager() { } + public static List getCommands() { + return new ArrayList<>(commands); + } + public static void logout() { discordClient.logout().block(); } private static void registerDiscordCommands() { - for (Map.Entry command : commands.entrySet()) + for (Command command : commands) discordClient.getEventDispatcher().on(MessageCreateEvent.class) .filter(event -> event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false)) - .filter(event -> event.getMessage().getContent().split(" ")[0].equals(PREFIX + command.getKey())) - .flatMap(event -> command.getValue().execute(event).then()) + .filter(event -> event.getMessage().getContent().split(" ")[0].equals(command.getName())) + .flatMap(event -> command.execute(event).then()) .subscribe(); }