Refactored to move Discord logic in DiscordManager

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-28 17:03:52 +01:00
parent 920bc47f85
commit a74b663e28
3 changed files with 81 additions and 57 deletions

View File

@ -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<String, Command> 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<String, Command> 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<Long> getGuilds() {
return discordClient.getGuilds().count();
DiscordManager.onDisconnect();
}
}

View File

@ -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();

View File

@ -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<String, Command> 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<String, Command> 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<Long> getGuilds() {
return discordClient.getGuilds().count();
}
}