deleting notes
This commit is contained in:
@@ -176,6 +176,14 @@ public class Main {
|
|||||||
)
|
)
|
||||||
.setContexts(InteractionContextType.BOT_DM)
|
.setContexts(InteractionContextType.BOT_DM)
|
||||||
).queue();
|
).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() {
|
private static void loadProperties() {
|
||||||
|
|||||||
@@ -13,9 +13,7 @@ import java.sql.Connection;
|
|||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class Storage {
|
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 {
|
public Status addAlias(@NotNull String alias, @NotNull UUID noteId) throws RuntimeException {
|
||||||
try (Connection connection = dataSource.getConnection()) {
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
PreparedStatement statement = connection.prepareStatement("""
|
PreparedStatement statement = connection.prepareStatement("""
|
||||||
@@ -167,18 +179,18 @@ public class Storage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<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");
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
List<String> aliases = new ArrayList<>();
|
Set<String> aliases = new HashSet<>();
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
aliases.add(resultSet.getString("alias"));
|
aliases.add(resultSet.getString("alias"));
|
||||||
}
|
}
|
||||||
return aliases;
|
return aliases;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
logger.warning("Failed to get aliases");
|
logger.warning("Failed to get aliases");
|
||||||
return new ArrayList<>();
|
return new HashSet<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,6 +206,9 @@ public class Storage {
|
|||||||
""");
|
""");
|
||||||
statement.setString(1, alias);
|
statement.setString(1, alias);
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
if (!resultSet.next()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
Color color = null;
|
Color color = null;
|
||||||
try {
|
try {
|
||||||
@@ -201,7 +216,6 @@ public class Storage {
|
|||||||
} catch (SQLException ignored) {
|
} catch (SQLException ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new Note(
|
return new Note(
|
||||||
UUID.fromString(resultSet.getString("id")),
|
UUID.fromString(resultSet.getString("id")),
|
||||||
resultSet.getString("title"),
|
resultSet.getString("title"),
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
|||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class SlashCommandListener extends ListenerAdapter {
|
public class SlashCommandListener extends ListenerAdapter {
|
||||||
@@ -28,11 +29,16 @@ public class SlashCommandListener extends ListenerAdapter {
|
|||||||
"footer-url"
|
"footer-url"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
private final Set<String> aliases;
|
||||||
|
|
||||||
|
public SlashCommandListener() {
|
||||||
|
aliases = Main.getStorage().getAliases();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) {
|
public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) {
|
||||||
if (!event.getName().equals("note") || event.getName().equals("edit-note")) return;
|
|
||||||
if (!event.getFocusedOption().getName().equals("alias")) 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()))
|
.filter(word -> word.startsWith(event.getFocusedOption().getValue()))
|
||||||
.map(word -> new Command.Choice(word, word))
|
.map(word -> new Command.Choice(word, word))
|
||||||
.limit(25)
|
.limit(25)
|
||||||
@@ -86,6 +92,16 @@ public class SlashCommandListener extends ListenerAdapter {
|
|||||||
}
|
}
|
||||||
editNote(event);
|
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.")
|
default -> event.reply("Unknown command.")
|
||||||
.setEphemeral(true)
|
.setEphemeral(true)
|
||||||
@@ -96,7 +112,7 @@ public class SlashCommandListener extends ListenerAdapter {
|
|||||||
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) {
|
||||||
event.reply("Note with ID %s not found.".formatted(noteAlias))
|
event.reply("Note with alias %s not found.".formatted(noteAlias))
|
||||||
.setEphemeral(true)
|
.setEphemeral(true)
|
||||||
.queue();
|
.queue();
|
||||||
return;
|
return;
|
||||||
@@ -168,9 +184,11 @@ public class SlashCommandListener extends ListenerAdapter {
|
|||||||
note = note.withFooterUrl(footerUrlOption.getAsString());
|
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) {
|
if (status == Storage.Status.SUCCESS) {
|
||||||
|
aliases.add(alias);
|
||||||
event.reply("Note added.")
|
event.reply("Note added.")
|
||||||
.setEphemeral(true)
|
.setEphemeral(true)
|
||||||
.queue();
|
.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user