Now when user unsubscribe, their timers are also cancelled
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
57e248c79a
commit
187c46e1f2
@ -35,7 +35,6 @@ jar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
testImplementation 'junit:junit:4.13'
|
|
||||||
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'
|
||||||
|
@ -4,26 +4,22 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import xyz.vallat.louis.managers.database.DBManager;
|
import xyz.vallat.louis.managers.database.DBManager;
|
||||||
import xyz.vallat.louis.managers.discord.DiscordManager;
|
import xyz.vallat.louis.managers.discord.DiscordManager;
|
||||||
import xyz.vallat.louis.timertasks.AutoReimport;
|
import xyz.vallat.louis.timer.TimerManager;
|
||||||
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
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 PREFIX = "+";
|
||||||
public static final String NAME = "PrésencEirb";
|
public static final String NAME = "PrésencEirb";
|
||||||
public static final String DESCRIPTION = "Tu as pensé à pointer ?";
|
public static final String DESCRIPTION = "Tu as pensé à pointer ?";
|
||||||
public static final String VERSION = "0.1-SNAPSHOT";
|
public static final String VERSION = "0.1-SNAPSHOT";
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(App.class.getCanonicalName());
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
logger.info("Starting {} ver. {}.", NAME, VERSION);
|
logger.info("Starting {} ver. {}.", NAME, VERSION);
|
||||||
DBManager.testConnection();
|
DBManager.testConnection();
|
||||||
DBManager.initializeDatabase();
|
DBManager.initializeDatabase();
|
||||||
DiscordManager.login();
|
DiscordManager.login();
|
||||||
setTimers();
|
TimerManager.startAutoReimportTimer();
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
logger.info("Received shut down signal. Bye!");
|
logger.info("Received shut down signal. Bye!");
|
||||||
@ -32,15 +28,4 @@ public class App {
|
|||||||
|
|
||||||
DiscordManager.onDisconnect();
|
DiscordManager.onDisconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setTimers() {
|
|
||||||
Calendar midday = Calendar.getInstance();
|
|
||||||
midday.set(Calendar.HOUR_OF_DAY, 12);
|
|
||||||
midday.set(Calendar.MINUTE, 30);
|
|
||||||
midday.set(Calendar.SECOND, 0);
|
|
||||||
|
|
||||||
Timer reimportCron = new Timer();
|
|
||||||
reimportCron.schedule(new AutoReimport(), midday.getTime(),
|
|
||||||
TimeUnit.MILLISECONDS.convert(12, TimeUnit.HOURS));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package xyz.vallat.louis.commands;
|
|||||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import xyz.vallat.louis.managers.database.StudentManager;
|
import xyz.vallat.louis.managers.database.StudentManager;
|
||||||
|
import xyz.vallat.louis.timer.TimerManager;
|
||||||
|
|
||||||
public class Desinscription extends Command {
|
public class Desinscription extends Command {
|
||||||
|
|
||||||
@ -16,6 +17,7 @@ public class Desinscription extends Command {
|
|||||||
if (event.getMessage().getAuthor().isEmpty() ||
|
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("On dirait que je ne t'ai pas dans ma base de données !");
|
||||||
|
TimerManager.cancelStudentTimer(event.getMessage().getAuthor().get().getId().asString());
|
||||||
return channel.createMessage("C'est bon, tu n'es plus dans ma base de données ! :thumbs_up:");
|
return channel.createMessage("C'est bon, tu n'es plus dans ma base de données ! :thumbs_up:");
|
||||||
}).then().onErrorResume(t -> fatalError(event, t));
|
}).then().onErrorResume(t -> fatalError(event, t));
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ public final class CalendarManager {
|
|||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(CalendarManager.class.getCanonicalName());
|
private static final Logger logger = LoggerFactory.getLogger(CalendarManager.class.getCanonicalName());
|
||||||
public static final Calendar TZ_UTC = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
public static final Calendar TZ_UTC = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||||
|
public static final Calendar TZ = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"));
|
||||||
|
|
||||||
private CalendarManager() {
|
private CalendarManager() {
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package xyz.vallat.louis.managers.discord;
|
package xyz.vallat.louis.managers.discord;
|
||||||
|
|
||||||
|
import discord4j.common.util.Snowflake;
|
||||||
import discord4j.core.DiscordClientBuilder;
|
import discord4j.core.DiscordClientBuilder;
|
||||||
import discord4j.core.GatewayDiscordClient;
|
import discord4j.core.GatewayDiscordClient;
|
||||||
import discord4j.core.event.domain.lifecycle.ReadyEvent;
|
import discord4j.core.event.domain.lifecycle.ReadyEvent;
|
||||||
@ -26,7 +27,7 @@ public final class DiscordManager {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
commands.add(new Inscription(PREFIX + "inscription"));
|
commands.add(new Inscription(PREFIX + "inscription"));
|
||||||
commands.add(new Desinscription(PREFIX + "désincription"));
|
commands.add(new Desinscription(PREFIX + "désinscription"));
|
||||||
commands.add(new Lien(PREFIX + "lien"));
|
commands.add(new Lien(PREFIX + "lien"));
|
||||||
commands.add(new Version(PREFIX + "version"));
|
commands.add(new Version(PREFIX + "version"));
|
||||||
commands.add(new Aide(PREFIX + "aide"));
|
commands.add(new Aide(PREFIX + "aide"));
|
||||||
@ -66,6 +67,14 @@ public final class DiscordManager {
|
|||||||
registerDiscordCommands();
|
registerDiscordCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Mono<Void> sendPrivateMessageToUser(String snowflake, String message) {
|
||||||
|
if (discordClient != null) {
|
||||||
|
Snowflake u = Snowflake.of(snowflake);
|
||||||
|
return discordClient.getUserById(u)
|
||||||
|
.flatMap(user -> user.getPrivateChannel().flatMap(privateChannel -> privateChannel.createMessage(message))).then();
|
||||||
|
} else return Mono.empty();
|
||||||
|
}
|
||||||
|
|
||||||
public static Mono<String> getClientImage() {
|
public static Mono<String> getClientImage() {
|
||||||
if (discordClient == null) return Mono.empty();
|
if (discordClient == null) return Mono.empty();
|
||||||
return discordClient.getSelf().map(User::getAvatarUrl);
|
return discordClient.getSelf().map(User::getAvatarUrl);
|
||||||
|
39
app/src/main/java/xyz/vallat/louis/timer/TimerManager.java
Normal file
39
app/src/main/java/xyz/vallat/louis/timer/TimerManager.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package xyz.vallat.louis.timer;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import xyz.vallat.louis.timer.tasks.AutoReimport;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public final class TimerManager {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(TimerManager.class.getCanonicalName());
|
||||||
|
private static final Map<String, List<Timer>> studentsTimers = new HashMap<>();
|
||||||
|
|
||||||
|
private TimerManager() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void cancelStudentTimer(String snowflake) {
|
||||||
|
List<Timer> timers = studentsTimers.remove(snowflake);
|
||||||
|
for (Timer t : timers) {
|
||||||
|
try {
|
||||||
|
t.cancel();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warn("Got exception while cancelling student's timer:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void startAutoReimportTimer() {
|
||||||
|
Calendar midday = Calendar.getInstance();
|
||||||
|
midday.set(Calendar.HOUR, 12);
|
||||||
|
midday.set(Calendar.MINUTE, 30);
|
||||||
|
midday.set(Calendar.SECOND, 0);
|
||||||
|
|
||||||
|
Timer reimportCron = new Timer();
|
||||||
|
reimportCron.schedule(new AutoReimport(), midday.getTime(),
|
||||||
|
TimeUnit.MILLISECONDS.convert(12, TimeUnit.HOURS));
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package xyz.vallat.louis.timertasks;
|
package xyz.vallat.louis.timer.tasks;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
@ -0,0 +1,30 @@
|
|||||||
|
package xyz.vallat.louis.timer.tasks;
|
||||||
|
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import xyz.vallat.louis.environment.EnvironmentVariables;
|
||||||
|
import xyz.vallat.louis.managers.discord.DiscordManager;
|
||||||
|
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
public class MessagePresence extends TimerTask {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(MessagePresence.class.getCanonicalName());
|
||||||
|
|
||||||
|
private final String summary;
|
||||||
|
private final String snowflake;
|
||||||
|
|
||||||
|
public MessagePresence(String summary, String snowflake) {
|
||||||
|
this.summary = summary;
|
||||||
|
this.snowflake = snowflake;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
logger.debug("Warning user {} for course '{}'.", snowflake, summary);
|
||||||
|
DiscordManager.sendPrivateMessageToUser(snowflake,
|
||||||
|
"Salut ! Tu as pensé à émarger pour ton cours de " + summary + " ? Le lien vers moodle est ici : " +
|
||||||
|
System.getenv(EnvironmentVariables.MOODLE_PRESENCE_LINK.getValue())).subscribe();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user