From ee694c088fa36b57c696d978f812b3ccbd6412cb Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Fri, 9 Aug 2024 21:50:00 +0200 Subject: [PATCH] make getting note command configurable --- .../java/me/youhavetrouble/noted/Main.java | 12 ++++- .../noted/listener/SlashCommandListener.java | 44 ++++++++++--------- src/main/resources/noted.properites | 1 + 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/main/java/me/youhavetrouble/noted/Main.java b/src/main/java/me/youhavetrouble/noted/Main.java index dd8a8ba..5e412ef 100644 --- a/src/main/java/me/youhavetrouble/noted/Main.java +++ b/src/main/java/me/youhavetrouble/noted/Main.java @@ -3,6 +3,7 @@ package me.youhavetrouble.noted; import me.youhavetrouble.noted.listener.SlashCommandListener; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.interactions.IntegrationType; import net.dv8tion.jda.api.interactions.InteractionContextType; @@ -25,6 +26,7 @@ public class Main { private static String version = "Unknown version"; private static Storage storage; private static Long adminId; + public static String command; public static JDA jda; public static void main(String[] args) throws InterruptedException { @@ -51,12 +53,20 @@ public class Main { jda = JDABuilder.createLight(properties.getProperty("DISCORD_TOKEN"), Collections.emptyList()) .setCallbackPool(Executors.newVirtualThreadPerTaskExecutor()) .setActivity(Activity.customStatus("Notekeeping...")) + .setStatus(OnlineStatus.ONLINE) .addEventListeners(new SlashCommandListener()) .build(); jda.awaitReady(); - jda.upsertCommand(Commands.slash("note", "Get a note") + command = properties.getProperty("COMMAND", "note"); + if (command.equals("add-note") || command.equals("edit-note")) { + logger.warning("The command name " + command + " is reserved. Please change the command name in noted.properties."); + logger.warning("The command name has been changed to 'note'."); + command = "note"; + } + + jda.upsertCommand(Commands.slash(command, "Get a note") .setIntegrationTypes(IntegrationType.GUILD_INSTALL, IntegrationType.USER_INSTALL) .addOptions( new OptionData(OptionType.STRING, "alias", "The ID of the note", true, true), diff --git a/src/main/java/me/youhavetrouble/noted/listener/SlashCommandListener.java b/src/main/java/me/youhavetrouble/noted/listener/SlashCommandListener.java index dfb6745..69ded2e 100644 --- a/src/main/java/me/youhavetrouble/noted/listener/SlashCommandListener.java +++ b/src/main/java/me/youhavetrouble/noted/listener/SlashCommandListener.java @@ -42,28 +42,30 @@ public class SlashCommandListener extends ListenerAdapter { @Override public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { - switch (event.getName()) { - case "note" -> { - OptionMapping noteIdOption = event.getOption("alias"); - OptionMapping ephemeralOption = event.getOption("ephemeral"); - if (noteIdOption == null) { - event.reply("Please provide a note alias.") - .setEphemeral(true) - .queue(); - return; - } - boolean ephemeral = false; - try { - ephemeral = ephemeralOption != null && ephemeralOption.getAsBoolean(); - } catch (IllegalArgumentException e) { - event.reply("Invalid value for ephemeral.") - .setEphemeral(true) - .queue(); - return; - } - String noteId = noteIdOption.getAsString(); - getNote(event, noteId, ephemeral); + if (Main.command.equals(event.getName())) { + OptionMapping noteIdOption = event.getOption("alias"); + OptionMapping ephemeralOption = event.getOption("ephemeral"); + if (noteIdOption == null) { + event.reply("Please provide a note alias.") + .setEphemeral(true) + .queue(); + return; } + boolean ephemeral = false; + try { + ephemeral = ephemeralOption != null && ephemeralOption.getAsBoolean(); + } catch (IllegalArgumentException e) { + event.reply("Invalid value for ephemeral.") + .setEphemeral(true) + .queue(); + return; + } + String noteId = noteIdOption.getAsString(); + getNote(event, noteId, ephemeral); + return; + } + + switch (event.getName()) { case "add-note" -> { Long adminId = Main.getAdminId(); if (adminId == null || !adminId.equals(event.getUser().getIdLong())) { diff --git a/src/main/resources/noted.properites b/src/main/resources/noted.properites index b9cf246..ed449b7 100644 --- a/src/main/resources/noted.properites +++ b/src/main/resources/noted.properites @@ -1,2 +1,3 @@ DISCORD_TOKEN=your_bot_token_here ADMIN_USER_ID=your_user_id_here +COMMAND=note