Introduced the reaction system

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-04-16 15:59:58 +02:00
parent 306e72403c
commit 0375911c67
2 changed files with 26 additions and 3 deletions

View File

@ -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) {

View File

@ -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<Command> 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<Void> sendPrivateMessageToUser(String snowflake, String message) {