Added unsubscribe command

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-25 22:32:02 +01:00
parent 578b4c5011
commit 8acf7c4ff2
4 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,22 @@
package xyz.vallat.louis.commands;
import discord4j.core.event.domain.message.MessageCreateEvent;
import reactor.core.publisher.Mono;
import xyz.vallat.louis.managers.database.StudentManager;
public class Desinscription extends Command {
public Desinscription(String name) {
super(name, "Se désinscrire des rappels d'émargement.", name);
}
@Override
public Mono<Void> execute(MessageCreateEvent event) {
return event.getMessage().getChannel().flatMap(channel -> {
if (event.getMessage().getAuthor().isEmpty() ||
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));
}
}

View File

@ -27,8 +27,9 @@ public class Inscription extends Command {
@Override
public Mono<Void> execute(MessageCreateEvent event) {
String[] args = event.getMessage().getContent().split(" ");
if (args.length < 2 || !StringUtils.isNumeric(args[1]))
if (args.length < 2 || !StringUtils.isNumeric(args[1]) || event.getMessage().getAuthor().isEmpty())
return event.getMessage().getChannel().flatMap(channel -> channel.createMessage("Error: " + getUsage())).then();
logger.info("Subscribing '{}'.", args[1]);
Student studentFromDatabase = StudentManager.getStudentFromDatabase(Integer.parseInt(args[1]));
if (studentFromDatabase != null) return alreadyImported(event, studentFromDatabase);
return event.getMessage().getChannel().flatMap(messageChannel -> {

View File

@ -61,4 +61,18 @@ public final class StudentManager {
}
return null;
}
public static boolean deleteFromSnowflake(String snowflake) {
try (Connection connection = DBManager.getConnection()) {
String sql = "DELETE FROM students WHERE snowflake = ?;";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, snowflake);
stmt.executeUpdate();
return true;
}
} catch (SQLException e) {
logger.error("SQLError:", e);
}
return false;
}
}

View File

@ -10,6 +10,7 @@ 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 xyz.vallat.louis.environment.EnvironmentVariables;
@ -27,6 +28,7 @@ public final class DiscordManager {
static {
commands.add(new Inscription(PREFIX + "inscription"));
commands.add(new Desinscription(PREFIX + "désincription"));
commands.add(new Lien(PREFIX + "lien"));
}