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:
parent
197437996a
commit
b27fad85d1
@ -4,7 +4,10 @@ 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.entity.Message;
|
||||
import discord4j.core.object.entity.channel.MessageChannel;
|
||||
import discord4j.core.object.reaction.ReactionEmoji;
|
||||
import discord4j.rest.util.Color;
|
||||
import org.apache.xmlrpc.XmlRpcException;
|
||||
import reactor.core.publisher.Mono;
|
||||
import xyz.vallat.louis.subtitles.OpenSubtitles;
|
||||
@ -12,7 +15,7 @@ import xyz.vallat.louis.subtitles.OpenSubtitles;
|
||||
public class Search extends Command {
|
||||
|
||||
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) {
|
||||
super(name, "A command to search some subtitles.", name + " <name>", 1, 1);
|
||||
@ -22,37 +25,45 @@ public class Search extends Command {
|
||||
public Mono<Void> execute(MessageCreateEvent event) {
|
||||
String[] args = event.getMessage().getContent().split(" ");
|
||||
if (args.length < minArgs)
|
||||
return event.getMessage().getChannel()
|
||||
.flatMap(channel -> channel.createMessage("Error.\n" + getUsage()))
|
||||
.then();
|
||||
return notEnoughArguments(event);
|
||||
return event.getMessage().getChannel()
|
||||
.flatMap(channel -> {
|
||||
if (OpenSubtitles.isLoggedIn()) {
|
||||
return event.getMessage().addReaction(WAITING)
|
||||
.then(event.getMessage().getChannel().flatMap(messageChannel -> {
|
||||
try {
|
||||
ListResponse<MovieInfo> response = OpenSubtitles.searchImdb(args[1]);
|
||||
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("It seems like OpenSubtitles had a stroke.");
|
||||
}
|
||||
}))
|
||||
.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 {
|
||||
ListResponse<MovieInfo> response = OpenSubtitles.searchImdb(arg);
|
||||
if (response.getStatus().getCode() == ResponseStatus.OK.getCode()) {
|
||||
return messageChannel.createEmbed(
|
||||
embed -> {
|
||||
embed.setTitle("Results")
|
||||
.setDescription("Found " + response.getData().size() + " results.")
|
||||
.setColor(Color.BISMARK);
|
||||
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("It seems like OpenSubtitles had a stroke.");
|
||||
}
|
||||
}
|
||||
|
||||
private Mono<Void> notEnoughArguments(MessageCreateEvent event) {
|
||||
return event.getMessage().getChannel()
|
||||
.flatMap(channel -> channel.createMessage("Error.\n" + getUsage()))
|
||||
.then();
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user