Changed the command interface to an abstract method and added better error handling
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
e9be061576
commit
197437996a
@ -35,9 +35,9 @@ public class MovieQuoteBot {
|
|||||||
private static GatewayDiscordClient discordClient;
|
private static GatewayDiscordClient discordClient;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
commands.put("ping", new Ping());
|
commands.put("ping", new Ping(PREFIX + "ping"));
|
||||||
commands.put("version", new Version());
|
commands.put("version", new Version(PREFIX + "version"));
|
||||||
commands.put("search", new Search());
|
commands.put("search", new Search(PREFIX + "search"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -3,7 +3,37 @@ package xyz.vallat.louis.commands;
|
|||||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
public interface Command {
|
public abstract class Command {
|
||||||
|
|
||||||
Mono<Void> 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<Void> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,11 @@ package xyz.vallat.louis.commands;
|
|||||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||||
import reactor.core.publisher.Mono;
|
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
|
@Override
|
||||||
public Mono<Void> execute(MessageCreateEvent event) {
|
public Mono<Void> execute(MessageCreateEvent event) {
|
||||||
|
@ -2,34 +2,51 @@ package xyz.vallat.louis.commands;
|
|||||||
|
|
||||||
import com.github.wtekiela.opensub4j.response.ListResponse;
|
import com.github.wtekiela.opensub4j.response.ListResponse;
|
||||||
import com.github.wtekiela.opensub4j.response.MovieInfo;
|
import com.github.wtekiela.opensub4j.response.MovieInfo;
|
||||||
|
import com.github.wtekiela.opensub4j.response.ResponseStatus;
|
||||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||||
import discord4j.core.object.reaction.ReactionEmoji;
|
import discord4j.core.object.reaction.ReactionEmoji;
|
||||||
import org.apache.xmlrpc.XmlRpcException;
|
import org.apache.xmlrpc.XmlRpcException;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import xyz.vallat.louis.subtitles.OpenSubtitles;
|
import xyz.vallat.louis.subtitles.OpenSubtitles;
|
||||||
|
|
||||||
public class Search implements Command {
|
public class Search extends Command {
|
||||||
|
|
||||||
private static final ReactionEmoji WAITING = ReactionEmoji.unicode("⌛");
|
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 + " <name>", 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> execute(MessageCreateEvent event) {
|
public Mono<Void> execute(MessageCreateEvent event) {
|
||||||
String[] args = event.getMessage().getContent().split(" ");
|
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()
|
return event.getMessage().getChannel()
|
||||||
.flatMap(channel -> {
|
.flatMap(channel -> {
|
||||||
if (OpenSubtitles.isLoggedIn()) {
|
if (OpenSubtitles.isLoggedIn()) {
|
||||||
return event.getMessage().addReaction(WAITING)
|
return event.getMessage().addReaction(WAITING)
|
||||||
.then(event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createMessage(args[1])))
|
|
||||||
.then(event.getMessage().getChannel().flatMap(messageChannel -> {
|
.then(event.getMessage().getChannel().flatMap(messageChannel -> {
|
||||||
try {
|
try {
|
||||||
ListResponse<MovieInfo> response = OpenSubtitles.searchImdb(args[1]);
|
ListResponse<MovieInfo> response = OpenSubtitles.searchImdb(args[1]);
|
||||||
StringBuilder b = new StringBuilder();
|
if (response.getStatus() == ResponseStatus.OK) {
|
||||||
for (MovieInfo m : response.getData())
|
return messageChannel.createEmbed(
|
||||||
b.append(m.getId()).append(" > ").append(m.getTitle()).append("\n");
|
embed -> {
|
||||||
return messageChannel.createMessage(String.valueOf(b));
|
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) {
|
} catch (XmlRpcException e) {
|
||||||
return channel.createMessage("Error");
|
return channel.createMessage("It seems like OpenSubtitles had a stroke.");
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.then(event.getMessage().removeSelfReaction(WAITING))
|
.then(event.getMessage().removeSelfReaction(WAITING))
|
||||||
|
@ -5,7 +5,11 @@ import discord4j.rest.util.Color;
|
|||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import xyz.vallat.louis.MovieQuoteBot;
|
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
|
@Override
|
||||||
public Mono<Void> execute(MessageCreateEvent event) {
|
public Mono<Void> execute(MessageCreateEvent event) {
|
||||||
|
Loading…
Reference in New Issue
Block a user