add completions for note aliases
This commit is contained in:
@@ -59,8 +59,8 @@ public class Main {
|
||||
jda.upsertCommand(Commands.slash("note", "Get a note")
|
||||
.setIntegrationTypes(IntegrationType.GUILD_INSTALL, IntegrationType.USER_INSTALL)
|
||||
.addOptions(
|
||||
new OptionData(OptionType.STRING, "note-id", "The ID of the note").setRequired(true),
|
||||
new OptionData(OptionType.BOOLEAN, "ephermeal", "Whether the note should be ephermal").setRequired(false)
|
||||
new OptionData(OptionType.STRING, "note-id", "The ID of the note", true, true),
|
||||
new OptionData(OptionType.BOOLEAN, "ephermeal", "Whether the note should be ephermal", true)
|
||||
)
|
||||
.setContexts(InteractionContextType.BOT_DM, InteractionContextType.GUILD, InteractionContextType.PRIVATE_CHANNEL)
|
||||
).queue();
|
||||
|
||||
@@ -7,12 +7,14 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.awt.*;
|
||||
import java.awt.Color;
|
||||
import java.io.File;
|
||||
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.logging.Logger;
|
||||
|
||||
@@ -20,6 +22,8 @@ import java.util.logging.Logger;
|
||||
|
||||
public class Storage {
|
||||
|
||||
public final List<String> aliases = new ArrayList<>();
|
||||
|
||||
private final Logger logger = Logger.getLogger("Storage");
|
||||
|
||||
private final DataSource dataSource;
|
||||
@@ -47,7 +51,7 @@ public class Storage {
|
||||
}
|
||||
|
||||
createTables();
|
||||
|
||||
aliases.addAll(getAliases());
|
||||
}
|
||||
|
||||
protected void shutdown() {
|
||||
@@ -113,6 +117,7 @@ public class Storage {
|
||||
statement.setString(2, note.id.toString());
|
||||
statement.executeUpdate();
|
||||
|
||||
aliases.add(alias);
|
||||
connection.commit();
|
||||
return Status.SUCCESS;
|
||||
} catch (SQLException e) {
|
||||
@@ -132,6 +137,7 @@ public class Storage {
|
||||
statement.setString(1, alias);
|
||||
statement.setString(2, noteId.toString());
|
||||
statement.executeUpdate();
|
||||
aliases.add(alias);
|
||||
return Status.SUCCESS;
|
||||
} catch (SQLException e) {
|
||||
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
|
||||
public Note getNote(@NotNull String alias) {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
|
||||
@@ -3,12 +3,29 @@ package me.youhavetrouble.noted.listener;
|
||||
import me.youhavetrouble.noted.Main;
|
||||
import me.youhavetrouble.noted.Storage;
|
||||
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.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.interactions.commands.Command;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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
|
||||
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
|
||||
switch (event.getName()) {
|
||||
|
||||
Reference in New Issue
Block a user