Refactored Search and fixed an issue on a check in the Search command

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-26 17:11:24 +01:00
parent 197437996a
commit b27fad85d1
2 changed files with 36 additions and 26 deletions

View File

@ -4,7 +4,10 @@ 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 com.github.wtekiela.opensub4j.response.ResponseStatus;
import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.channel.MessageChannel;
import discord4j.core.object.reaction.ReactionEmoji; import discord4j.core.object.reaction.ReactionEmoji;
import discord4j.rest.util.Color;
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;
@ -12,7 +15,7 @@ import xyz.vallat.louis.subtitles.OpenSubtitles;
public class Search extends 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"; private static final String FORMAT = "%1$-7s - %2$s\n";
public Search(String name) { public Search(String name) {
super(name, "A command to search some subtitles.", name + " <name>", 1, 1); super(name, "A command to search some subtitles.", name + " <name>", 1, 1);
@ -22,24 +25,32 @@ public class Search extends Command {
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 < minArgs) if (args.length < minArgs)
return event.getMessage().getChannel() return notEnoughArguments(event);
.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 -> { .then(event.getMessage().getChannel().flatMap(messageChannel ->
searchAndShowFilms(args[1], channel, messageChannel)))
.then(event.getMessage().removeSelfReaction(WAITING))
.then();
} else return channel.createMessage("I cannot search for subtitles right now. Sorry.");
})
.then();
}
private Mono<? extends Message> searchAndShowFilms(String arg, MessageChannel channel, MessageChannel messageChannel) {
try { try {
ListResponse<MovieInfo> response = OpenSubtitles.searchImdb(args[1]); ListResponse<MovieInfo> response = OpenSubtitles.searchImdb(arg);
if (response.getStatus() == ResponseStatus.OK) { if (response.getStatus().getCode() == ResponseStatus.OK.getCode()) {
return messageChannel.createEmbed( return messageChannel.createEmbed(
embed -> { embed -> {
embed.setTitle("Results") embed.setTitle("Results")
.setDescription("Found " + response.getData().size() + " results."); .setDescription("Found " + response.getData().size() + " results.")
.setColor(Color.BISMARK);
StringBuilder s = new StringBuilder("```txt\n"); StringBuilder s = new StringBuilder("```txt\n");
for (MovieInfo info : response.getData().subList(0, 10)) for (MovieInfo info : response.getData().subList(0, 10))
s.append(String.format(format, info.getId(), info.getTitle())); s.append(String.format(FORMAT, info.getId(), info.getTitle()));
s.append("```"); s.append("```");
embed.addField("First 10 results:", s.toString(), false); embed.addField("First 10 results:", s.toString(), false);
}); });
@ -48,11 +59,11 @@ public class Search extends Command {
} catch (XmlRpcException e) { } catch (XmlRpcException e) {
return channel.createMessage("It seems like OpenSubtitles had a stroke."); return channel.createMessage("It seems like OpenSubtitles had a stroke.");
} }
})) }
.then(event.getMessage().removeSelfReaction(WAITING))
.then(); private Mono<Void> notEnoughArguments(MessageCreateEvent event) {
} else return channel.createMessage("I cannot search for subtitles right now. Sorry."); return event.getMessage().getChannel()
}) .flatMap(channel -> channel.createMessage("Error.\n" + getUsage()))
.then(); .then();
} }
} }

View File

@ -11,7 +11,6 @@ import org.slf4j.LoggerFactory;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Map;
public final class OpenSubtitles { public final class OpenSubtitles {
@ -49,7 +48,7 @@ public final class OpenSubtitles {
} }
} }
public static ListResponse<MovieInfo> searchImdb (String name) throws XmlRpcException { public static ListResponse<MovieInfo> searchImdb(String name) throws XmlRpcException {
return client.searchMoviesOnImdb(name); return client.searchMoviesOnImdb(name);
} }