From 0375911c6748371730e3e4f4ba2f27de2afd5702 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Fri, 16 Apr 2021 15:59:58 +0200 Subject: [PATCH] Introduced the reaction system Signed-off-by: Louis Vallat --- app/src/main/java/xyz/vallat/louis/App.java | 2 +- .../managers/discord/DiscordManager.java | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/xyz/vallat/louis/App.java b/app/src/main/java/xyz/vallat/louis/App.java index cc004e1..4bc6229 100644 --- a/app/src/main/java/xyz/vallat/louis/App.java +++ b/app/src/main/java/xyz/vallat/louis/App.java @@ -11,7 +11,7 @@ public class App { public static final String PREFIX = "+"; public static final String NAME = "PrésencEirb"; public static final String DESCRIPTION = "Tu as pensé à pointer ?"; - public static final String VERSION = "0.3.3"; + public static final String VERSION = "0.4.0"; private static final Logger logger = LoggerFactory.getLogger(App.class.getCanonicalName()); public static void main(String[] args) { 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 e9b47bf..ecef565 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 @@ -5,17 +5,18 @@ 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.event.domain.message.ReactionAddEvent; import discord4j.core.object.entity.User; import discord4j.core.object.presence.Activity; import discord4j.core.object.presence.Presence; +import discord4j.core.object.reaction.ReactionEmoji; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; import xyz.vallat.louis.commands.*; import xyz.vallat.louis.environment.EnvironmentVariables; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import static xyz.vallat.louis.App.PREFIX; @@ -23,6 +24,16 @@ public final class DiscordManager { private static final List commands = new ArrayList<>(); private static final Logger logger = LoggerFactory.getLogger(DiscordManager.class.getCanonicalName()); + private static final ReactionEmoji[] reactionAnswers = { + ReactionEmoji.unicode("\uD83E\uDD16"), // robot head + ReactionEmoji.unicode("\uD83D\uDE0B"), // smile with tongue out + ReactionEmoji.unicode("\uD83E\uDD70"), // smile with hearts + ReactionEmoji.unicode("\uD83E\uDD29"), // smile with stars in eyes + ReactionEmoji.unicode("\uD83D\uDE01"), // smile + ReactionEmoji.unicode("\uD83D\uDE07"), // innocent + ReactionEmoji.unicode("\uD83D\uDCAF"), // 1OO + ReactionEmoji.unicode("\uD83E\uDDBE") // robot arm + }; private static GatewayDiscordClient discordClient; static { @@ -50,6 +61,17 @@ public final class DiscordManager { .subscribe(); } + private static void registerReactionEvents() { + discordClient.getEventDispatcher().on(ReactionAddEvent.class) + .filter(event -> event.getMessage().block() != null + && Arrays.stream(reactionAnswers).noneMatch(r -> Objects.requireNonNull(event.getMessage().block()) + .getReactors(r).any(user -> user.equals(discordClient.getSelf().block())).block()) + && Objects.requireNonNull(event.getMessage().block()).getAuthor().isPresent() + && !Objects.requireNonNull(event.getMessage().block()).getAuthor().get().equals(discordClient.getSelf().block())) + .flatMap(event -> event.getMessage().flatMap(message -> message.addReaction(reactionAnswers[new Random().nextInt(reactionAnswers.length)]))) + .subscribe(); + } + public static void login() { discordClient = DiscordClientBuilder.create(System.getenv(EnvironmentVariables.DISCORD_TOKEN.getValue())) .build().login().retry(2) @@ -66,6 +88,7 @@ public final class DiscordManager { logger.info("Logged in on Discord as {}#{}.", self.getUsername(), self.getDiscriminator()); }); registerDiscordCommands(); + registerReactionEvents(); } public static Mono sendPrivateMessageToUser(String snowflake, String message) {