From c90c28dd60accbf6bee86b2cf22a9bb8799e16c7 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Wed, 25 Nov 2020 22:56:41 +0100 Subject: [PATCH] Added version and help commands Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/commands/Aide.java | 30 +++++++++++++++++++ .../vallat/louis/commands/Desinscription.java | 2 +- .../xyz/vallat/louis/commands/Version.java | 30 +++++++++++++++++++ .../managers/database/StudentManager.java | 1 + .../managers/discord/DiscordManager.java | 13 +++++--- 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/xyz/vallat/louis/commands/Aide.java create mode 100644 app/src/main/java/xyz/vallat/louis/commands/Version.java diff --git a/app/src/main/java/xyz/vallat/louis/commands/Aide.java b/app/src/main/java/xyz/vallat/louis/commands/Aide.java new file mode 100644 index 0000000..bbd1254 --- /dev/null +++ b/app/src/main/java/xyz/vallat/louis/commands/Aide.java @@ -0,0 +1,30 @@ +package xyz.vallat.louis.commands; + +import discord4j.core.event.domain.message.MessageCreateEvent; +import discord4j.rest.util.Color; +import reactor.core.publisher.Mono; +import xyz.vallat.louis.App; +import xyz.vallat.louis.managers.discord.DiscordManager; + +import java.time.Instant; + +public class Aide extends Command { + + public Aide(String name) { + super(name, "Un chouette menu d'aide.", name); + } + + @Override + public Mono execute(MessageCreateEvent event) { + return event.getMessage().getChannel().flatMap(channel -> channel.createEmbed(embed -> { + DiscordManager.getCommands().forEach(c -> + embed.addField(c.getName(), c.getDescription() + " Usage: " + c.getUsage(), false)); + embed.setColor(Color.GREEN) + .setTitle(App.NAME) + .setDescription("Besoin d'aide ?") + .setUrl("https://gitlab.com/lovallat/reminder-bot") + .setFooter("Menu d'aide", null) + .setTimestamp(Instant.now()); + })).then().onErrorResume(t -> fatalError(event, t)); + } +} diff --git a/app/src/main/java/xyz/vallat/louis/commands/Desinscription.java b/app/src/main/java/xyz/vallat/louis/commands/Desinscription.java index ec7af19..7b47a0a 100644 --- a/app/src/main/java/xyz/vallat/louis/commands/Desinscription.java +++ b/app/src/main/java/xyz/vallat/louis/commands/Desinscription.java @@ -14,7 +14,7 @@ public class Desinscription extends Command { public Mono execute(MessageCreateEvent event) { return event.getMessage().getChannel().flatMap(channel -> { if (event.getMessage().getAuthor().isEmpty() || - StudentManager.deleteFromSnowflake(event.getMessage().getAuthor().get().getId().asString())) + !StudentManager.deleteFromSnowflake(event.getMessage().getAuthor().get().getId().asString())) return channel.createMessage("On dirait que je ne t'ai pas dans ma base de données !"); return channel.createMessage("C'est bon, tu n'es plus dans ma base de données ! :thumbs_up:"); }).then().onErrorResume(t -> fatalError(event, t)); diff --git a/app/src/main/java/xyz/vallat/louis/commands/Version.java b/app/src/main/java/xyz/vallat/louis/commands/Version.java new file mode 100644 index 0000000..0eb53bb --- /dev/null +++ b/app/src/main/java/xyz/vallat/louis/commands/Version.java @@ -0,0 +1,30 @@ +package xyz.vallat.louis.commands; + +import discord4j.core.event.domain.message.MessageCreateEvent; +import discord4j.rest.util.Color; +import reactor.core.publisher.Mono; +import xyz.vallat.louis.App; +import xyz.vallat.louis.managers.discord.DiscordManager; + +import java.time.Instant; + +public class Version extends Command { + + public Version(String name) { + super(name, "Donne des informations sur la version de ce bot.", name); + } + + @Override + public Mono execute(MessageCreateEvent event) { + return event.getMessage().getChannel().flatMap(channel -> channel.createEmbed(embed -> { + embed.setColor(Color.GREEN) + .setTitle(App.NAME) + .setDescription(App.DESCRIPTION) + .setUrl("https://gitlab.com/lovallat/reminder-bot") + .setTimestamp(Instant.now()) + .addField("Version", App.VERSION, false); + String imageURL = DiscordManager.getClientImage().block(); + if (imageURL != null) embed.setThumbnail(imageURL); + })).then().onErrorResume(t -> fatalError(event, t)); + } +} diff --git a/app/src/main/java/xyz/vallat/louis/managers/database/StudentManager.java b/app/src/main/java/xyz/vallat/louis/managers/database/StudentManager.java index 516ced5..af9f012 100644 --- a/app/src/main/java/xyz/vallat/louis/managers/database/StudentManager.java +++ b/app/src/main/java/xyz/vallat/louis/managers/database/StudentManager.java @@ -63,6 +63,7 @@ public final class StudentManager { } public static boolean deleteFromSnowflake(String snowflake) { + logger.debug("Deleting student {}.", snowflake); try (Connection connection = DBManager.getConnection()) { String sql = "DELETE FROM students WHERE snowflake = ?;"; try (PreparedStatement stmt = connection.prepareStatement(sql)) { diff --git a/app/src/main/java/xyz/vallat/louis/managers/discord/DiscordManager.java b/app/src/main/java/xyz/vallat/louis/managers/discord/DiscordManager.java index fb64148..be0adaf 100644 --- a/app/src/main/java/xyz/vallat/louis/managers/discord/DiscordManager.java +++ b/app/src/main/java/xyz/vallat/louis/managers/discord/DiscordManager.java @@ -9,10 +9,8 @@ import discord4j.core.object.presence.Activity; import discord4j.core.object.presence.Presence; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import xyz.vallat.louis.commands.Command; -import xyz.vallat.louis.commands.Desinscription; -import xyz.vallat.louis.commands.Inscription; -import xyz.vallat.louis.commands.Lien; +import reactor.core.publisher.Mono; +import xyz.vallat.louis.commands.*; import xyz.vallat.louis.environment.EnvironmentVariables; import java.util.ArrayList; @@ -30,6 +28,8 @@ public final class DiscordManager { commands.add(new Inscription(PREFIX + "inscription")); commands.add(new Desinscription(PREFIX + "désincription")); commands.add(new Lien(PREFIX + "lien")); + commands.add(new Version(PREFIX + "version")); + commands.add(new Aide(PREFIX + "aide")); } private DiscordManager() { @@ -66,6 +66,11 @@ public final class DiscordManager { registerDiscordCommands(); } + public static Mono getClientImage() { + if (discordClient == null) return Mono.empty(); + return discordClient.getSelf().map(User::getAvatarUrl); + } + public static void onDisconnect() { discordClient.onDisconnect().block(); }