alias management

This commit is contained in:
2024-08-10 20:35:59 +02:00
parent d17d5bf55f
commit 1afa4cd2ed
3 changed files with 153 additions and 8 deletions
@@ -184,6 +184,24 @@ public class Main {
) )
.setContexts(InteractionContextType.BOT_DM) .setContexts(InteractionContextType.BOT_DM)
).queue(); ).queue();
jda.upsertCommand(Commands.slash("add-alias", "Add alias to a note")
.setIntegrationTypes(IntegrationType.USER_INSTALL)
.addOptions(
new OptionData(OptionType.STRING, "alias", "Existing alias of a note", true, true),
new OptionData(OptionType.STRING, "new-alias", "New alias for the note", true)
)
.setContexts(InteractionContextType.BOT_DM)
).queue();
jda.upsertCommand(Commands.slash("delete-alias", "Remove alias to a note")
.setIntegrationTypes(IntegrationType.USER_INSTALL)
.addOptions(
new OptionData(OptionType.STRING, "alias", "Existing alias of a note", true, true)
)
.setContexts(InteractionContextType.BOT_DM)
).queue();
} }
private static void loadProperties() { private static void loadProperties() {
@@ -37,7 +37,7 @@ public class Storage {
config.setJdbcUrl("jdbc:sqlite:data/data.db"); config.setJdbcUrl("jdbc:sqlite:data/data.db");
config.setConnectionTestQuery("SELECT 1"); config.setConnectionTestQuery("SELECT 1");
config.setMaxLifetime(60000); // 60 Sec config.setMaxLifetime(60000); // 60 Sec
config.setMaximumPoolSize(Math.min(1, Runtime.getRuntime().availableProcessors() / 4)); config.setMaximumPoolSize(Math.min(4, Runtime.getRuntime().availableProcessors() / 4));
dataSource = new HikariDataSource(config); dataSource = new HikariDataSource(config);
try (Connection connection = dataSource.getConnection()) { try (Connection connection = dataSource.getConnection()) {
@@ -174,11 +174,39 @@ public class Storage {
aliases.add(alias); aliases.add(alias);
return Status.SUCCESS; return Status.SUCCESS;
} catch (SQLException e) { } catch (SQLException e) {
logger.warning("Failed to add alias"); logger.warning(e.getMessage());
return Status.ALIAS_EXISTS; return Status.ALIAS_EXISTS;
} }
} }
public Status deleteAlias(@NotNull String alias) {
try (Connection connection = dataSource.getConnection()) {
Note note = getNote(alias);
if (note == null) {
return Status.ALIAS_NOT_FOUND;
}
PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM aliases WHERE alias = ?");
statement.setString(1, alias);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
if (resultSet.getInt(1) <= 1) {
// Only one alias left, don't allow deletion
return Status.ALIAS_IS_REQUIRED;
}
}
statement = connection.prepareStatement("DELETE FROM aliases WHERE alias = ?");
statement.setString(1, alias);
statement.executeUpdate();
aliases.remove(alias);
return Status.SUCCESS;
} catch (SQLException e) {
return Status.ERROR;
}
}
public Set<String> getAliases() { public Set<String> getAliases() {
try (Connection connection = dataSource.getConnection()) { try (Connection connection = dataSource.getConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT alias FROM aliases"); PreparedStatement statement = connection.prepareStatement("SELECT alias FROM aliases");
@@ -239,6 +267,7 @@ public class Storage {
SUCCESS, SUCCESS,
ALIAS_EXISTS, ALIAS_EXISTS,
ALIAS_NOT_FOUND, ALIAS_NOT_FOUND,
ALIAS_IS_REQUIRED,
ERROR ERROR
} }
@@ -3,6 +3,7 @@ package me.youhavetrouble.noted.listener;
import me.youhavetrouble.noted.Main; import me.youhavetrouble.noted.Main;
import me.youhavetrouble.noted.Storage; import me.youhavetrouble.noted.Storage;
import me.youhavetrouble.noted.note.Note; import me.youhavetrouble.noted.note.Note;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
@@ -73,8 +74,7 @@ public class SlashCommandListener extends ListenerAdapter {
switch (event.getName()) { switch (event.getName()) {
case "add-note" -> { case "add-note" -> {
Long adminId = Main.getAdminId(); if (!isAdmin(event.getUser())) {
if (adminId == null || !adminId.equals(event.getUser().getIdLong())) {
event.reply("You do not have permission to use this command.") event.reply("You do not have permission to use this command.")
.setEphemeral(true) .setEphemeral(true)
.queue(); .queue();
@@ -83,8 +83,7 @@ public class SlashCommandListener extends ListenerAdapter {
addNote(event); addNote(event);
} }
case "edit-note" -> { case "edit-note" -> {
Long adminId = Main.getAdminId(); if (!isAdmin(event.getUser())) {
if (adminId == null || !adminId.equals(event.getUser().getIdLong())) {
event.reply("You do not have permission to use this command.") event.reply("You do not have permission to use this command.")
.setEphemeral(true) .setEphemeral(true)
.queue(); .queue();
@@ -93,8 +92,7 @@ public class SlashCommandListener extends ListenerAdapter {
editNote(event); editNote(event);
} }
case "delete-note" -> { case "delete-note" -> {
Long adminId = Main.getAdminId(); if (!isAdmin(event.getUser())) {
if (adminId == null || !adminId.equals(event.getUser().getIdLong())) {
event.reply("You do not have permission to use this command.") event.reply("You do not have permission to use this command.")
.setEphemeral(true) .setEphemeral(true)
.queue(); .queue();
@@ -102,6 +100,24 @@ public class SlashCommandListener extends ListenerAdapter {
} }
deleteNote(event); deleteNote(event);
} }
case "add-alias" -> {
if (!isAdmin(event.getUser())) {
event.reply("You do not have permission to use this command.")
.setEphemeral(true)
.queue();
return;
}
addAlias(event);
}
case "delete-alias" -> {
if (!isAdmin(event.getUser())) {
event.reply("You do not have permission to use this command.")
.setEphemeral(true)
.queue();
return;
}
deleteAlias(event);
}
default -> event.reply("Unknown command.") default -> event.reply("Unknown command.")
.setEphemeral(true) .setEphemeral(true)
@@ -109,6 +125,11 @@ public class SlashCommandListener extends ListenerAdapter {
} }
} }
private boolean isAdmin(User user) {
Long adminId = Main.getAdminId();
return adminId != null && adminId.equals(user.getIdLong());
}
private void getNote(SlashCommandInteractionEvent event, String noteAlias, boolean ephemeral) { private void getNote(SlashCommandInteractionEvent event, String noteAlias, boolean ephemeral) {
Note note = Main.getStorage().getNote(noteAlias); Note note = Main.getStorage().getNote(noteAlias);
if (note == null) { if (note == null) {
@@ -341,4 +362,81 @@ public class SlashCommandListener extends ListenerAdapter {
} }
} }
private void addAlias(SlashCommandInteractionEvent event) {
OptionMapping noteAliasMapping = event.getOption("alias");
if (noteAliasMapping == null) {
event.reply("Please provide a note alias.")
.setEphemeral(true)
.queue();
return;
}
OptionMapping newAliasMapping = event.getOption("new-alias");
if (newAliasMapping == null) {
event.reply("Please provide a new alias.")
.setEphemeral(true)
.queue();
return;
}
String noteAlias = noteAliasMapping.getAsString();
Note note = Main.getStorage().getNote(noteAlias);
if (note == null) {
event.reply("Note with alias %s not found.".formatted(noteAlias))
.setEphemeral(true)
.queue();
return;
}
Storage.Status status = Main.getStorage().addAlias(newAliasMapping.getAsString(), note.id);
if (status == Storage.Status.SUCCESS) {
aliases.add(newAliasMapping.getAsString());
event.reply("Alias added.")
.setEphemeral(true)
.queue();
} else if (status == Storage.Status.ALIAS_EXISTS) {
event.reply("Alias already exists.")
.setEphemeral(true)
.queue();
} else {
event.reply("Failed to add alias.")
.setEphemeral(true)
.queue();
}
}
private void deleteAlias(SlashCommandInteractionEvent event) {
OptionMapping aliasMapping = event.getOption("alias");
if (aliasMapping == null) {
event.reply("Please provide a alias.")
.setEphemeral(true)
.queue();
return;
}
String alias = aliasMapping.getAsString();
Storage.Status status = Main.getStorage().deleteAlias(alias);
System.out.print(status);
switch (status) {
case SUCCESS -> {
aliases.remove(alias);
event.reply("Alias deleted.")
.setEphemeral(true)
.queue();
}
case ALIAS_NOT_FOUND -> event.reply("Provided alias does not exist.")
.setEphemeral(true)
.queue();
case ALIAS_IS_REQUIRED -> event.reply("At least one alias per note is required.")
.setEphemeral(true)
.queue();
case null, default -> event.reply("Failed to delete alias.")
.setEphemeral(true)
.queue();
}
}
} }