add completions for note aliases

This commit is contained in:
2024-08-07 22:20:30 +02:00
parent c0dff561ba
commit a8a38b2c04
3 changed files with 42 additions and 4 deletions
@@ -59,8 +59,8 @@ public class Main {
jda.upsertCommand(Commands.slash("note", "Get a note") jda.upsertCommand(Commands.slash("note", "Get a note")
.setIntegrationTypes(IntegrationType.GUILD_INSTALL, IntegrationType.USER_INSTALL) .setIntegrationTypes(IntegrationType.GUILD_INSTALL, IntegrationType.USER_INSTALL)
.addOptions( .addOptions(
new OptionData(OptionType.STRING, "note-id", "The ID of the note").setRequired(true), new OptionData(OptionType.STRING, "note-id", "The ID of the note", true, true),
new OptionData(OptionType.BOOLEAN, "ephermeal", "Whether the note should be ephermal").setRequired(false) new OptionData(OptionType.BOOLEAN, "ephermeal", "Whether the note should be ephermal", true)
) )
.setContexts(InteractionContextType.BOT_DM, InteractionContextType.GUILD, InteractionContextType.PRIVATE_CHANNEL) .setContexts(InteractionContextType.BOT_DM, InteractionContextType.GUILD, InteractionContextType.PRIVATE_CHANNEL)
).queue(); ).queue();
@@ -7,12 +7,14 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.awt.*; import java.awt.Color;
import java.io.File; import java.io.File;
import java.sql.Connection; 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.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -20,6 +22,8 @@ import java.util.logging.Logger;
public class Storage { public class Storage {
public final List<String> aliases = new ArrayList<>();
private final Logger logger = Logger.getLogger("Storage"); private final Logger logger = Logger.getLogger("Storage");
private final DataSource dataSource; private final DataSource dataSource;
@@ -47,7 +51,7 @@ public class Storage {
} }
createTables(); createTables();
aliases.addAll(getAliases());
} }
protected void shutdown() { protected void shutdown() {
@@ -113,6 +117,7 @@ public class Storage {
statement.setString(2, note.id.toString()); statement.setString(2, note.id.toString());
statement.executeUpdate(); statement.executeUpdate();
aliases.add(alias);
connection.commit(); connection.commit();
return Status.SUCCESS; return Status.SUCCESS;
} catch (SQLException e) { } catch (SQLException e) {
@@ -132,6 +137,7 @@ public class Storage {
statement.setString(1, alias); statement.setString(1, alias);
statement.setString(2, noteId.toString()); statement.setString(2, noteId.toString());
statement.executeUpdate(); statement.executeUpdate();
aliases.add(alias);
return Status.SUCCESS; return Status.SUCCESS;
} catch (SQLException e) { } catch (SQLException e) {
logger.warning("Failed to add alias"); logger.warning("Failed to add alias");
@@ -139,6 +145,21 @@ public class Storage {
} }
} }
public List<String> getAliases() {
try (Connection connection = dataSource.getConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT alias FROM aliases");
ResultSet resultSet = statement.executeQuery();
List<String> aliases = new ArrayList<>();
while (resultSet.next()) {
aliases.add(resultSet.getString("alias"));
}
return aliases;
} catch (SQLException e) {
logger.warning("Failed to get aliases");
return new ArrayList<>();
}
}
@Nullable @Nullable
public Note getNote(@NotNull String alias) { public Note getNote(@NotNull String alias) {
try (Connection connection = dataSource.getConnection()) { try (Connection connection = dataSource.getConnection()) {
@@ -3,12 +3,29 @@ 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.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;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import java.util.List;
import java.util.stream.Collectors;
public class SlashCommandListener extends ListenerAdapter { public class SlashCommandListener extends ListenerAdapter {
@Override
public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) {
if (event.getName().equals("note") && event.getFocusedOption().getName().equals("note-id")) {
List<Command.Choice> options = Main.getStorage().aliases.stream()
.filter(word -> word.startsWith(event.getFocusedOption().getValue()))
.map(word -> new Command.Choice(word, word))
.limit(25)
.collect(Collectors.toList());
event.replyChoices(options).queue();
}
}
@Override @Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
switch (event.getName()) { switch (event.getName()) {