Added the link command

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-25 13:30:33 +01:00
parent 4ef0ee8e6b
commit 3db8134153
7 changed files with 188 additions and 6 deletions

View File

@ -39,6 +39,8 @@ dependencies {
implementation 'com.discord4j:discord4j-core:3.1.1' implementation 'com.discord4j:discord4j-core:3.1.1'
implementation 'ch.qos.logback:logback-classic:1.2.3' implementation 'ch.qos.logback:logback-classic:1.2.3'
implementation 'org.postgresql:postgresql:42.2.18.jre7' implementation 'org.postgresql:postgresql:42.2.18.jre7'
implementation 'org.apache.commons:commons-lang3:3.11'
implementation 'net.sf.biweekly:biweekly:0.6.5'
} }

View File

@ -1,8 +1,26 @@
package xyz.vallat.louis; package xyz.vallat.louis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.vallat.louis.managers.discord.DiscordManager;
public class App { public class App {
private static final Logger logger = LoggerFactory.getLogger(App.class.getCanonicalName());
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.1-SNAPSHOT";
public static void main(String[] args) { public static void main(String[] args) {
logger.info("Starting {} ver. {}.", NAME, VERSION);
DiscordManager.login();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
logger.info("Received shut down signal. Bye!");
DiscordManager.logout();
}));
DiscordManager.onDisconnect();
} }
} }

View File

@ -1,5 +1,57 @@
package xyz.vallat.louis.commands; package xyz.vallat.louis.commands;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.reaction.ReactionEmoji;
import discord4j.rest.util.Color;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import java.time.Instant;
public abstract class Command { public abstract class Command {
protected static final ReactionEmoji WAITING = ReactionEmoji.unicode("");
private static final Logger logger = LoggerFactory.getLogger(Command.class.getCanonicalName());
protected final String name;
protected final String description;
protected final String usage;
protected Command(String name, String description, String usage) {
this.name = name;
this.description = description;
this.usage = usage;
}
public abstract Mono<Void> execute(MessageCreateEvent event);
public String getUsage() {
return "``" + this.usage + "``";
}
public String getDescription() {
return this.description;
}
public String getName() {
return name;
}
protected Mono<Void> fatalError(MessageCreateEvent event, Throwable throwable) {
return event.getMessage().getChannel().flatMap(
channel -> channel.createEmbed(embed -> {
event.getMessage().removeSelfReaction(WAITING);
String errorCode = RandomStringUtils.random(8, true, true);
logger.error("KERNEL PANIC: {}", errorCode, throwable);
embed.setTitle("KERNEL PANIC").setColor(Color.RED)
.setDescription("A fatal and unhandled error has been encountered. " +
"Please transfer this message to my administrator.")
.addField("IDENTIFIER", errorCode, false).setTimestamp(Instant.now());
})).then();
}
protected String[] getArgArray(MessageCreateEvent event) {
return event.getMessage().getContent().substring(name.length()).split(" ");
}
} }

View File

@ -0,0 +1,20 @@
package xyz.vallat.louis.commands;
import discord4j.core.event.domain.message.MessageCreateEvent;
import reactor.core.publisher.Mono;
import xyz.vallat.louis.environment.EnvironmentVariables;
public class Lien extends Command{
public Lien(String name) {
super(name, "Donne le lien vers le moodle de présence.", name);
}
@Override
public Mono<Void> execute(MessageCreateEvent event) {
return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createMessage(
event.getMessage().getAuthor().isEmpty() ? "" :
("<@!" + event.getMessage().getAuthor().get().getId().asString() + "> ") +
System.getenv(EnvironmentVariables.MOODLE_PRESENCE_LINK.getValue()))).then();
}
}

View File

@ -0,0 +1,16 @@
package xyz.vallat.louis.commands;
import discord4j.core.event.domain.message.MessageCreateEvent;
import reactor.core.publisher.Mono;
public class Subscribe extends Command {
public Subscribe(String name) {
super(name, "S'inscrire aux rappels.", name + " <identifiant ressource>");
}
@Override
public Mono<Void> execute(MessageCreateEvent event) {
return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createMessage("Ponnnng!")).then();
}
}

View File

@ -1,17 +1,18 @@
package xyz.vallat.louis.environment; package xyz.vallat.louis.environment;
public enum EnvironmentVariables { public enum EnvironmentVariables {
DISCORD_TOKEN("DISCORD_TOKEN") DISCORD_TOKEN("DISCORD_TOKEN"),
MOODLE_PRESENCE_LINK("MOODLE_PRESENCE_LINK")
; ;
private final String name; private final String value;
EnvironmentVariables(String name) { EnvironmentVariables(String name) {
this.name = name; this.value = name;
} }
public String getName() { public String getValue() {
return name; return value;
} }
} }

View File

@ -1,12 +1,85 @@
package xyz.vallat.louis.managers.discord; package xyz.vallat.louis.managers.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.Command;
import xyz.vallat.louis.commands.Lien;
import xyz.vallat.louis.commands.Subscribe;
import xyz.vallat.louis.environment.EnvironmentVariables;
import java.util.ArrayList;
import java.util.List;
import static xyz.vallat.louis.App.PREFIX;
public class DiscordManager { public class DiscordManager {
private static final List<Command> commands = new ArrayList<>();
private static final Logger logger = LoggerFactory.getLogger(DiscordManager.class.getCanonicalName());
private static GatewayDiscordClient discordClient;
static {
commands.add(new Subscribe(PREFIX + "subscribe"));
commands.add(new Lien(PREFIX + "lien"));
}
private DiscordManager() { private DiscordManager() {
} }
public static void login() { public static void logout() {
discordClient.logout().block();
}
private static void registerDiscordCommands() {
for (Command command : commands)
discordClient.getEventDispatcher().on(MessageCreateEvent.class)
.filter(event -> event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false))
.filter(event -> event.getMessage().getContent().split(" ")[0].equals(command.getName()))
.flatMap(event -> command.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;
setOnline();
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();
}
public static List<Command> getCommands() {
return new ArrayList<>(commands);
}
public static void setOnline() {
if (discordClient != null)
discordClient.updatePresence(Presence.online(Activity.watching("vos emplois du temps"))).subscribe();
} }
} }