deleting notes

This commit is contained in:
2024-08-09 23:13:40 +02:00
parent 2655638c41
commit d17d5bf55f
3 changed files with 82 additions and 11 deletions
@@ -176,6 +176,14 @@ public class Main {
)
.setContexts(InteractionContextType.BOT_DM)
).queue();
jda.upsertCommand(Commands.slash("delete-note", "Delete a note")
.setIntegrationTypes(IntegrationType.USER_INSTALL)
.addOptions(
new OptionData(OptionType.STRING, "alias", "The ID of the note", true, true)
)
.setContexts(InteractionContextType.BOT_DM)
).queue();
}
private static void loadProperties() {
@@ -13,9 +13,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.logging.Logger;
public class Storage {
@@ -150,6 +148,20 @@ public class Storage {
}
}
public Status deleteNote(@NotNull UUID noteId) {
try (Connection connection = dataSource.getConnection()) {
PreparedStatement statement = connection.prepareStatement("DELETE FROM notes WHERE id = ?");
statement.setString(1, noteId.toString());
statement.executeUpdate();
statement = connection.prepareStatement("DELETE FROM aliases WHERE note_id = ?");
statement.setString(1, noteId.toString());
statement.executeUpdate();
return Status.SUCCESS;
} catch (SQLException e) {
return Status.ERROR;
}
}
public Status addAlias(@NotNull String alias, @NotNull UUID noteId) throws RuntimeException {
try (Connection connection = dataSource.getConnection()) {
PreparedStatement statement = connection.prepareStatement("""
@@ -167,18 +179,18 @@ public class Storage {
}
}
public List<String> getAliases() {
public Set<String> getAliases() {
try (Connection connection = dataSource.getConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT alias FROM aliases");
ResultSet resultSet = statement.executeQuery();
List<String> aliases = new ArrayList<>();
Set<String> aliases = new HashSet<>();
while (resultSet.next()) {
aliases.add(resultSet.getString("alias"));
}
return aliases;
} catch (SQLException e) {
logger.warning("Failed to get aliases");
return new ArrayList<>();
return new HashSet<>();
}
}
@@ -194,6 +206,9 @@ public class Storage {
""");
statement.setString(1, alias);
ResultSet resultSet = statement.executeQuery();
if (!resultSet.next()) {
return null;
}
Color color = null;
try {
@@ -201,7 +216,6 @@ public class Storage {
} catch (SQLException ignored) {
}
return new Note(
UUID.fromString(resultSet.getString("id")),
resultSet.getString("title"),
@@ -11,6 +11,7 @@ import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import java.awt.Color;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class SlashCommandListener extends ListenerAdapter {
@@ -28,11 +29,16 @@ public class SlashCommandListener extends ListenerAdapter {
"footer-url"
);
private final Set<String> aliases;
public SlashCommandListener() {
aliases = Main.getStorage().getAliases();
}
@Override
public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) {
if (!event.getName().equals("note") || event.getName().equals("edit-note")) return;
if (!event.getFocusedOption().getName().equals("alias")) return;
List<Command.Choice> options = Main.getStorage().aliases.stream()
List<Command.Choice> options = aliases.stream()
.filter(word -> word.startsWith(event.getFocusedOption().getValue()))
.map(word -> new Command.Choice(word, word))
.limit(25)
@@ -86,6 +92,16 @@ public class SlashCommandListener extends ListenerAdapter {
}
editNote(event);
}
case "delete-note" -> {
Long adminId = Main.getAdminId();
if (adminId == null || !adminId.equals(event.getUser().getIdLong())) {
event.reply("You do not have permission to use this command.")
.setEphemeral(true)
.queue();
return;
}
deleteNote(event);
}
default -> event.reply("Unknown command.")
.setEphemeral(true)
@@ -96,7 +112,7 @@ public class SlashCommandListener extends ListenerAdapter {
private void getNote(SlashCommandInteractionEvent event, String noteAlias, boolean ephemeral) {
Note note = Main.getStorage().getNote(noteAlias);
if (note == null) {
event.reply("Note with ID %s not found.".formatted(noteAlias))
event.reply("Note with alias %s not found.".formatted(noteAlias))
.setEphemeral(true)
.queue();
return;
@@ -168,9 +184,11 @@ public class SlashCommandListener extends ListenerAdapter {
note = note.withFooterUrl(footerUrlOption.getAsString());
}
Storage.Status status = Main.getStorage().addNote(note, aliasOption.getAsString());
String alias = aliasOption.getAsString();
Storage.Status status = Main.getStorage().addNote(note, alias);
if (status == Storage.Status.SUCCESS) {
aliases.add(alias);
event.reply("Note added.")
.setEphemeral(true)
.queue();
@@ -292,4 +310,35 @@ public class SlashCommandListener extends ListenerAdapter {
}
private void deleteNote(SlashCommandInteractionEvent event) {
OptionMapping noteAliasMapping = event.getOption("alias");
if (noteAliasMapping == null) {
event.reply("Please provide a note 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().deleteNote(note.id);
if (status == Storage.Status.SUCCESS) {
aliases.remove(noteAlias);
event.reply("Note deleted.")
.setEphemeral(true)
.queue();
} else {
event.reply("Failed to delete note.")
.setEphemeral(true)
.queue();
}
}
}