Added help command

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-29 15:09:30 +01:00
parent f056308923
commit f0e16f6b6e
5 changed files with 51 additions and 14 deletions

View File

@ -16,7 +16,6 @@ import xyz.vallat.louis.omdb.objects.Movie;
import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.OpenSubtitles;
import java.io.IOException; import java.io.IOException;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -67,6 +66,10 @@ public abstract class Command {
return maxArgs; return maxArgs;
} }
public String getName() {
return name;
}
protected Mono<Void> notEnoughArguments(MessageCreateEvent event) { protected Mono<Void> notEnoughArguments(MessageCreateEvent event) {
return event.getMessage().getChannel() return event.getMessage().getChannel()
.flatMap(channel -> channel.createMessage("Error, you're missing some arguments.\n" + getUsage())) .flatMap(channel -> channel.createMessage("Error, you're missing some arguments.\n" + getUsage()))

View File

@ -81,7 +81,7 @@ public class Download extends Command {
subs.getData().get(0).getContentAsString("cp1252")); subs.getData().get(0).getContentAsString("cp1252"));
SubtitleLineManager.importSubtitleLines(blocks, languageId, movie, SubtitleLineManager.importSubtitleLines(blocks, languageId, movie,
event.getGuildId().isPresent() ? event.getGuildId().get() : null, 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. " + embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " +
"Congratulations and thank you for your contribution!"); "Congratulations and thank you for your contribution!");
embed.addField("You imported", blocks.size() + " lines into my database", false); embed.addField("You imported", blocks.size() + " lines into my database", false);

View File

@ -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<Void> 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();
}
}

View File

@ -14,7 +14,7 @@ public class ListLangMovie extends Command {
private static final Logger logger = LoggerFactory.getLogger(ListLangMovie.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(ListLangMovie.class.getCanonicalName());
public ListLangMovie(String name) { 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); name + " imdb|title value", 2, 2);
} }

View File

@ -13,37 +13,42 @@ import reactor.core.publisher.Mono;
import xyz.vallat.louis.commands.*; import xyz.vallat.louis.commands.*;
import xyz.vallat.louis.env.EnvironmentVariables; import xyz.vallat.louis.env.EnvironmentVariables;
import java.util.HashMap; import java.util.ArrayList;
import java.util.Map; import java.util.List;
import static xyz.vallat.louis.MovieQuoteBot.PREFIX; import static xyz.vallat.louis.MovieQuoteBot.PREFIX;
public final class DiscordManager { public final class DiscordManager {
private static final List<Command> commands = new ArrayList<>();
private static final Logger logger = LoggerFactory.getLogger(DiscordManager.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(DiscordManager.class.getCanonicalName());
private static final Map<String, Command> commands = new HashMap<>();
private static GatewayDiscordClient discordClient; private static GatewayDiscordClient discordClient;
static { static {
commands.put("ping", new Ping(PREFIX + "ping")); commands.add(new ListLangMovie(PREFIX + "listLangMovie"));
commands.put("version", new Version(PREFIX + "version")); commands.add(new Download(PREFIX + "download"));
commands.put("listLangMovie", new ListLangMovie(PREFIX + "listLangMovie")); commands.add(new Quote(PREFIX + "quote"));
commands.put("download", new Download(PREFIX + "download")); commands.add(new Ping(PREFIX + "ping"));
commands.put("quote", new Quote(PREFIX + "quote")); commands.add(new Help(PREFIX + "help"));
commands.add(new Version(PREFIX + "version"));
} }
private DiscordManager() { private DiscordManager() {
} }
public static List<Command> getCommands() {
return new ArrayList<>(commands);
}
public static void logout() { public static void logout() {
discordClient.logout().block(); discordClient.logout().block();
} }
private static void registerDiscordCommands() { private static void registerDiscordCommands() {
for (Map.Entry<String, Command> command : commands.entrySet()) for (Command command : commands)
discordClient.getEventDispatcher().on(MessageCreateEvent.class) discordClient.getEventDispatcher().on(MessageCreateEvent.class)
.filter(event -> event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false)) .filter(event -> event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false))
.filter(event -> event.getMessage().getContent().split(" ")[0].equals(PREFIX + command.getKey())) .filter(event -> event.getMessage().getContent().split(" ")[0].equals(command.getName()))
.flatMap(event -> command.getValue().execute(event).then()) .flatMap(event -> command.execute(event).then())
.subscribe(); .subscribe();
} }