Added OpenSubtitles capability
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
e22e4fec02
commit
83279129bf
@ -32,5 +32,6 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.discord4j:discord4j-core:3.1.1'
|
implementation 'com.discord4j:discord4j-core:3.1.1'
|
||||||
|
implementation 'com.github.wtekiela:opensub4j:0.3.0'
|
||||||
implementation 'ch.qos.logback:logback-classic:1.2.3'
|
implementation 'ch.qos.logback:logback-classic:1.2.3'
|
||||||
}
|
}
|
@ -1,5 +1,8 @@
|
|||||||
package xyz.vallat.louis;
|
package xyz.vallat.louis;
|
||||||
|
|
||||||
|
import com.github.wtekiela.opensub4j.api.OpenSubtitlesClient;
|
||||||
|
import com.github.wtekiela.opensub4j.impl.OpenSubtitlesClientImpl;
|
||||||
|
import com.github.wtekiela.opensub4j.response.ResponseStatus;
|
||||||
import discord4j.core.DiscordClientBuilder;
|
import discord4j.core.DiscordClientBuilder;
|
||||||
import discord4j.core.GatewayDiscordClient;
|
import discord4j.core.GatewayDiscordClient;
|
||||||
import discord4j.core.event.domain.lifecycle.ReadyEvent;
|
import discord4j.core.event.domain.lifecycle.ReadyEvent;
|
||||||
@ -7,21 +10,27 @@ import discord4j.core.event.domain.message.MessageCreateEvent;
|
|||||||
import discord4j.core.object.entity.User;
|
import discord4j.core.object.entity.User;
|
||||||
import discord4j.core.object.presence.Activity;
|
import discord4j.core.object.presence.Activity;
|
||||||
import discord4j.core.object.presence.Presence;
|
import discord4j.core.object.presence.Presence;
|
||||||
import discord4j.discordjson.json.gateway.StatusUpdate;
|
import org.apache.xmlrpc.XmlRpcException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import xyz.vallat.louis.commands.Command;
|
import xyz.vallat.louis.commands.Command;
|
||||||
import xyz.vallat.louis.commands.Ping;
|
import xyz.vallat.louis.commands.Ping;
|
||||||
|
import xyz.vallat.louis.commands.Search;
|
||||||
import xyz.vallat.louis.commands.Version;
|
import xyz.vallat.louis.commands.Version;
|
||||||
|
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class MovieQuoteBot {
|
public class MovieQuoteBot {
|
||||||
|
|
||||||
public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN";
|
public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN";
|
||||||
|
public static final String OS_USERNAME_ENVIRONMENT = "OPEN_SUBTITLES_USERNAME";
|
||||||
|
public static final String OS_PASSWORD_ENVIRONMENT = "OPEN_SUBTITLES_PASSWORD";
|
||||||
|
public static final String OS_UA_ENVIRONMENT = "OPEN_SUBTITLES_USER_AGENT";
|
||||||
public static final String PREFIX = "!";
|
public static final String PREFIX = "!";
|
||||||
public static final String NAME = "Movies Quote Bot";
|
public static final String NAME = "Movies Quote Bot";
|
||||||
public static final String DESCRIPTION = "I may know some quotes from some movies.";
|
public static final String DESCRIPTION = "I may know some quotes from some movies.";
|
||||||
@ -29,48 +38,98 @@ public class MovieQuoteBot {
|
|||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName());
|
private static final Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName());
|
||||||
private static final Map<String, Command> commands = new HashMap<>();
|
private static final Map<String, Command> commands = new HashMap<>();
|
||||||
private static GatewayDiscordClient client;
|
private static GatewayDiscordClient discordClient;
|
||||||
|
private static OpenSubtitlesClient openSubtitlesClient;
|
||||||
|
private static String discordToken;
|
||||||
|
private static String osUsername;
|
||||||
|
private static String osPassword;
|
||||||
|
private static String osUa;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
commands.put("ping", new Ping());
|
commands.put("ping", new Ping());
|
||||||
commands.put("version", new Version());
|
commands.put("version", new Version());
|
||||||
|
commands.put("search", new Search());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
if (System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null && args.length == 0) {
|
if ((System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null ||
|
||||||
logger.error("Missing token. Exiting.");
|
System.getenv(OS_USERNAME_ENVIRONMENT) == null ||
|
||||||
|
System.getenv(OS_PASSWORD_ENVIRONMENT) == null ||
|
||||||
|
System.getenv(OS_UA_ENVIRONMENT) == null
|
||||||
|
) && args.length < 4) {
|
||||||
|
logger.error("Missing tokens. Exiting.");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
client = DiscordClientBuilder.create(
|
setVariables(args);
|
||||||
System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null ? args[0] : System.getenv(DISCORD_TOKEN_ENVIRONMENT)
|
osLogin();
|
||||||
).build().login().block();
|
discordLogin();
|
||||||
|
registerDiscordCommands();
|
||||||
|
|
||||||
assert client != null;
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
client.updatePresence(Presence.online(Activity.watching("some more films"))).subscribe();
|
logger.info("Received shut down signal. Bye!");
|
||||||
client.getEventDispatcher().on(ReadyEvent.class)
|
discordClient.logout().block();
|
||||||
.subscribe(event -> {
|
}));
|
||||||
User self = event.getSelf();
|
|
||||||
logger.info("Logged in as {}#{}.", self.getUsername(), self.getDiscriminator());
|
|
||||||
});
|
|
||||||
|
|
||||||
client.getEventDispatcher().on(MessageCreateEvent.class)
|
discordClient.onDisconnect().block();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void registerDiscordCommands() {
|
||||||
|
discordClient.getEventDispatcher().on(MessageCreateEvent.class)
|
||||||
.flatMap(event -> Mono.just(event.getMessage().getContent())
|
.flatMap(event -> Mono.just(event.getMessage().getContent())
|
||||||
.flatMap(content -> Flux.fromIterable(commands.entrySet())
|
.flatMap(content -> Flux.fromIterable(commands.entrySet())
|
||||||
.filter(entry -> content.split(" ")[0].equals(PREFIX + entry.getKey()))
|
.filter(entry -> content.split(" ")[0].equals(PREFIX + entry.getKey()))
|
||||||
.flatMap(entry -> entry.getValue().execute(event))
|
.flatMap(entry -> entry.getValue().execute(event))
|
||||||
.next()))
|
.next()))
|
||||||
.subscribe();
|
.subscribe();
|
||||||
|
}
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
private static void discordLogin() {
|
||||||
logger.info("Received shut down signal. Bye!");
|
discordClient = DiscordClientBuilder.create(discordToken).build().login().block();
|
||||||
client.logout();
|
assert discordClient != null;
|
||||||
}));
|
discordClient.updatePresence(Presence.online(Activity.watching("some more films"))).subscribe();
|
||||||
|
discordClient.getEventDispatcher().on(ReadyEvent.class)
|
||||||
|
.subscribe(event -> {
|
||||||
|
User self = event.getSelf();
|
||||||
|
logger.info("Logged in on Discord as {}#{}.", self.getUsername(), self.getDiscriminator());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
client.onDisconnect().block();
|
private static void setVariables(String[] args) {
|
||||||
|
discordToken = System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null ?
|
||||||
|
args[0] :
|
||||||
|
System.getenv(DISCORD_TOKEN_ENVIRONMENT);
|
||||||
|
osUsername = System.getenv(OS_USERNAME_ENVIRONMENT) == null ?
|
||||||
|
args[1] :
|
||||||
|
System.getenv(OS_USERNAME_ENVIRONMENT);
|
||||||
|
osPassword = System.getenv(OS_PASSWORD_ENVIRONMENT) == null ?
|
||||||
|
args[2] :
|
||||||
|
System.getenv(OS_PASSWORD_ENVIRONMENT);
|
||||||
|
osUa = System.getenv(OS_UA_ENVIRONMENT) == null ?
|
||||||
|
args[3] :
|
||||||
|
System.getenv(OS_UA_ENVIRONMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void osLogin() {
|
||||||
|
try {
|
||||||
|
URL serverUrl = new URL("https", "api.opensubtitles.org", 443, "/xml-rpc");
|
||||||
|
openSubtitlesClient = new OpenSubtitlesClientImpl(serverUrl);
|
||||||
|
ResponseStatus status = openSubtitlesClient.login(osUsername, osPassword, "en", osUa).getStatus();
|
||||||
|
if (!openSubtitlesClient.isLoggedIn()) {
|
||||||
|
logger.error("Cannot login on OpenSubtitles.org.");
|
||||||
|
logger.error("Message: [{}] - {}", status.getCode(), status.getMessage());
|
||||||
|
} else logger.info("Logged in on OpenSubtitles.org.");
|
||||||
|
} catch (MalformedURLException | XmlRpcException e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
System.exit(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isOsLoggedIn() {
|
||||||
|
return openSubtitlesClient != null && openSubtitlesClient.isLoggedIn();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Mono<Long> getGuilds() {
|
public static Mono<Long> getGuilds() {
|
||||||
return client.getGuilds().count();
|
return discordClient.getGuilds().count();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user