From a74b663e282e57880baf65acf9c8c9f6320574a5 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Wed, 28 Oct 2020 17:03:52 +0100 Subject: [PATCH] Refactored to move Discord logic in DiscordManager Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MovieQuoteBot.java | 60 +-------------- .../xyz/vallat/louis/commands/Version.java | 3 +- .../vallat/louis/discord/DiscordManager.java | 75 +++++++++++++++++++ 3 files changed, 81 insertions(+), 57 deletions(-) create mode 100644 src/main/java/xyz/vallat/louis/discord/DiscordManager.java diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index a9010e5..7773ef0 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -1,83 +1,31 @@ package xyz.vallat.louis; -import discord4j.core.DiscordClientBuilder; -import discord4j.core.GatewayDiscordClient; -import discord4j.core.event.domain.lifecycle.ReadyEvent; -import discord4j.core.event.domain.message.MessageCreateEvent; -import discord4j.core.object.entity.User; -import discord4j.core.object.presence.Activity; -import discord4j.core.object.presence.Presence; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import reactor.core.publisher.Mono; -import xyz.vallat.louis.commands.*; import xyz.vallat.louis.database.DBManager; -import xyz.vallat.louis.env.EnvironmentVariables; +import xyz.vallat.louis.discord.DiscordManager; import xyz.vallat.louis.subtitles.OpenSubtitles; -import java.util.HashMap; -import java.util.Map; - public class MovieQuoteBot { public static final String PREFIX = "!"; 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 VERSION = "0.1-SNAPSHOT"; - private static final Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName()); - private static final Map commands = new HashMap<>(); - private static GatewayDiscordClient discordClient; - - static { - commands.put("ping", new Ping(PREFIX + "ping")); - commands.put("version", new Version(PREFIX + "version")); - commands.put("listLang", new ListLang(PREFIX + "listLang")); - commands.put("download", new Download(PREFIX + "download")); - } public static void main(String[] args) { DBManager.testConnection(); DBManager.initDatabase(); OpenSubtitles.login(); - discordLogin(System.getenv(EnvironmentVariables.DISCORD_TOKEN.getValue())); - registerDiscordCommands(); + DiscordManager.login(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { logger.info("Received shut down signal. Bye!"); OpenSubtitles.logout(); - discordClient.logout().block(); + DiscordManager.logout(); })); - discordClient.onDisconnect().block(); - } - - private static void registerDiscordCommands() { - for (Map.Entry command : commands.entrySet()) - discordClient.getEventDispatcher().on(MessageCreateEvent.class) - .filter(event -> event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false)) - .filter(event -> event.getMessage().getContent().split(" ")[0].equals(PREFIX + command.getKey())) - .flatMap(event -> command.getValue().execute(event).then()) - .subscribe(); - } - - private static void discordLogin(String token) { - discordClient = DiscordClientBuilder.create(token).build().login().retry(2) - .onErrorMap(throwable -> { - logger.error("Cannot login to Discord right now. Reason: {}", throwable.getMessage()); - return throwable; - } - ).block(); - 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()); - }); - } - - public static Mono getGuilds() { - return discordClient.getGuilds().count(); + DiscordManager.onDisconnect(); } } diff --git a/src/main/java/xyz/vallat/louis/commands/Version.java b/src/main/java/xyz/vallat/louis/commands/Version.java index 7981e3b..e3f29ae 100644 --- a/src/main/java/xyz/vallat/louis/commands/Version.java +++ b/src/main/java/xyz/vallat/louis/commands/Version.java @@ -4,6 +4,7 @@ import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.rest.util.Color; import reactor.core.publisher.Mono; import xyz.vallat.louis.MovieQuoteBot; +import xyz.vallat.louis.discord.DiscordManager; public class Version extends Command { @@ -20,7 +21,7 @@ public class Version extends Command { .setTitle(MovieQuoteBot.NAME) .setDescription(MovieQuoteBot.DESCRIPTION) .addField("Version", MovieQuoteBot.VERSION, true) - .addField("Guilds", String.valueOf(MovieQuoteBot.getGuilds().block()), true) + .addField("Guilds", String.valueOf(DiscordManager.getGuilds().block()), true) ) ) .then(); diff --git a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java new file mode 100644 index 0000000..6a3f7b6 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java @@ -0,0 +1,75 @@ +package xyz.vallat.louis.discord; + +import discord4j.core.DiscordClientBuilder; +import discord4j.core.GatewayDiscordClient; +import discord4j.core.event.domain.lifecycle.ReadyEvent; +import discord4j.core.event.domain.message.MessageCreateEvent; +import discord4j.core.object.entity.User; +import discord4j.core.object.presence.Activity; +import discord4j.core.object.presence.Presence; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Mono; +import xyz.vallat.louis.commands.*; +import xyz.vallat.louis.env.EnvironmentVariables; + +import java.util.HashMap; +import java.util.Map; + +import static xyz.vallat.louis.MovieQuoteBot.PREFIX; + +public final class DiscordManager { + private static final Logger logger = LoggerFactory.getLogger(DiscordManager.class.getCanonicalName()); + private static final Map commands = new HashMap<>(); + private static GatewayDiscordClient discordClient; + + static { + commands.put("ping", new Ping(PREFIX + "ping")); + commands.put("version", new Version(PREFIX + "version")); + commands.put("listLang", new ListLang(PREFIX + "listLang")); + commands.put("download", new Download(PREFIX + "download")); + } + + private DiscordManager() { + } + + public static void logout() { + discordClient.logout().block(); + } + + private static void registerDiscordCommands() { + for (Map.Entry command : commands.entrySet()) + discordClient.getEventDispatcher().on(MessageCreateEvent.class) + .filter(event -> event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false)) + .filter(event -> event.getMessage().getContent().split(" ")[0].equals(PREFIX + command.getKey())) + .flatMap(event -> command.getValue().execute(event).then()) + .subscribe(); + } + + public static void login() { + discordClient = DiscordClientBuilder.create(System.getenv(EnvironmentVariables.DISCORD_TOKEN.getValue())) + .build().login().retry(2) + .onErrorMap(throwable -> { + logger.error("Cannot login to Discord right now. Reason: {}", throwable.getMessage()); + return throwable; + } + ).block(); + 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()); + }); + registerDiscordCommands(); + } + + public static void onDisconnect() { + discordClient.onDisconnect().block(); + } + + public static Mono getGuilds() { + return discordClient.getGuilds().count(); + } + +}