From ef7a3ff664aea480ce5c8de8417a55ce5d36110b Mon Sep 17 00:00:00 2001 From: Matyrobbrt Date: Wed, 20 Apr 2022 14:04:30 +0300 Subject: [PATCH 1/3] Add settings getter in command events --- .../jdautilities/command/CommandEvent.java | 54 +++++++++++++++++++ .../command/MessageContextMenuEvent.java | 50 +++++++++++++++++ .../command/SlashCommandEvent.java | 49 +++++++++++++++++ .../command/UserContextMenuEvent.java | 50 +++++++++++++++++ 4 files changed, 203 insertions(+) diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java b/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java index bff08aea..3dc77df7 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java @@ -25,6 +25,7 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.exceptions.PermissionException; import net.dv8tion.jda.internal.utils.Checks; +import org.jetbrains.annotations.Nullable; /** * A wrapper class for a {@link net.dv8tion.jda.api.events.message.MessageReceivedEvent MessageReceivedEvent}, @@ -1203,4 +1204,57 @@ public boolean isFromType(ChannelType channelType) { return event.isFromType(channelType); } + + /** + * Gets the settings of the guild in which this command was run. + * + * @param the type of the settings + * @return the settings, or {@code null} if either of the following conditions are met: + * + */ + @Nullable + public S getGuildSettings() + { + try { + getGuild(); + } catch (IllegalStateException ignored) { + return null; + } + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + return manager.getSettings(getGuild()); + } + + /** + * Gets the settings of the guild in which this command was run. + * + * @param settingsClazz the class of the settings + * @param the type of the settings + * @return the settings, or {@code null} if either of the following conditions are met: + *
    + *
  • this command wasn't run in a guild
  • + *
  • the client's {@link GuildSettingsManager} is null
  • + *
  • the {@link GuildSettingsManager} returned null settings for the guild
  • + *
  • the {@link GuildSettingsManager} returned settings that are not assignable to the {@code settingsClazz}
  • + *
+ */ + @Nullable + @SuppressWarnings("rawtypes") + public S getGuildSettings(Class settingsClazz) + { + try { + getGuild(); + } catch (IllegalStateException ignored) { + return null; + } + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + final Object settings = manager.getSettings(getGuild()); + if (!settingsClazz.isInstance(settings)) return null; + return settingsClazz.cast(settings); + } } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenuEvent.java b/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenuEvent.java index 87b5c55a..f5d5ed17 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenuEvent.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenuEvent.java @@ -25,6 +25,7 @@ import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import net.dv8tion.jda.api.utils.AttachmentOption; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; @@ -159,4 +160,53 @@ public boolean isOwner() return true; return false; } + + /** + * Gets the settings of the guild in which this context menu was used. + * + * @param the type of the settings + * @return the settings, or {@code null} if either of the following conditions are met: + *
    + *
  • this interaction didn't happen in a guild
  • + *
  • the client's {@link GuildSettingsManager} is null
  • + *
  • the {@link GuildSettingsManager} returned null settings for the guild
  • + *
+ */ + @Nullable + public S getGuildSettings() + { + if (!isFromGuild()) { + return null; + } + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + return manager.getSettings(getGuild()); + } + + /** + * Gets the settings of the guild in which this context menu was used. + * + * @param settingsClazz the class of the settings + * @param the type of the settings + * @return the settings, or {@code null} if either of the following conditions are met: + *
    + *
  • this interaction didn't happen in a guild
  • + *
  • the client's {@link GuildSettingsManager} is null
  • + *
  • the {@link GuildSettingsManager} returned null settings for the guild
  • + *
  • the {@link GuildSettingsManager} returned settings that are not assignable to the {@code settingsClazz}
  • + *
+ */ + @Nullable + @SuppressWarnings("rawtypes") + public S getGuildSettings(Class settingsClazz) + { + if (!isFromGuild()) { + return null; + } + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + final Object settings = manager.getSettings(getGuild()); + if (!settingsClazz.isInstance(settings)) return null; + return settingsClazz.cast(settings); + } } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java index 919a60a7..5136a683 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java @@ -353,4 +353,53 @@ public boolean isFromType(ChannelType channelType) { return getChannelType() == channelType; } + + /** + * Gets the settings of the guild in which this command was run. + * + * @param the type of the settings + * @return the settings, or {@code null} if either of the following conditions are met: + *
    + *
  • this interaction didn't happen in a guild
  • + *
  • the client's {@link GuildSettingsManager} is null
  • + *
  • the {@link GuildSettingsManager} returned null settings for the guild
  • + *
+ */ + @Nullable + public S getGuildSettings() + { + if (!isFromGuild()) { + return null; + } + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + return manager.getSettings(getGuild()); + } + + /** + * Gets the settings of the guild in which this command was run. + * + * @param settingsClazz the class of the settings + * @param the type of the settings + * @return the settings, or {@code null} if either of the following conditions are met: + *
    + *
  • this interaction didn't happen in a guild
  • + *
  • the client's {@link GuildSettingsManager} is null
  • + *
  • the {@link GuildSettingsManager} returned null settings for the guild
  • + *
  • the {@link GuildSettingsManager} returned settings that are not assignable to the {@code settingsClazz}
  • + *
+ */ + @Nullable + @SuppressWarnings("rawtypes") + public S getGuildSettings(Class settingsClazz) + { + if (!isFromGuild()) { + return null; + } + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + final Object settings = manager.getSettings(getGuild()); + if (!settingsClazz.isInstance(settings)) return null; + return settingsClazz.cast(settings); + } } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenuEvent.java b/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenuEvent.java index f6b4ea95..f6390330 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenuEvent.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenuEvent.java @@ -25,6 +25,7 @@ import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import net.dv8tion.jda.api.utils.AttachmentOption; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; @@ -122,4 +123,53 @@ public boolean isOwner() return true; return false; } + + /** + * Gets the settings of the guild in which this context menu was used. + * + * @param the type of the settings + * @return the settings, or {@code null} if either of the following conditions are met: + *
    + *
  • this interaction didn't happen in a guild
  • + *
  • the client's {@link GuildSettingsManager} is null
  • + *
  • the {@link GuildSettingsManager} returned null settings for the guild
  • + *
+ */ + @Nullable + public S getGuildSettings() + { + if (!isFromGuild()) { + return null; + } + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + return manager.getSettings(getGuild()); + } + + /** + * Gets the settings of the guild in which this context menu was used. + * + * @param settingsClazz the class of the settings + * @param the type of the settings + * @return the settings, or {@code null} if either of the following conditions are met: + *
    + *
  • this interaction didn't happen in a guild
  • + *
  • the client's {@link GuildSettingsManager} is null
  • + *
  • the {@link GuildSettingsManager} returned null settings for the guild
  • + *
  • the {@link GuildSettingsManager} returned settings that are not assignable to the {@code settingsClazz}
  • + *
+ */ + @Nullable + @SuppressWarnings("rawtypes") + public S getGuildSettings(Class settingsClazz) + { + if (!isFromGuild()) { + return null; + } + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + final Object settings = manager.getSettings(getGuild()); + if (!settingsClazz.isInstance(settings)) return null; + return settingsClazz.cast(settings); + } } From 252809f87422562223b7d1af571af75d819f5a65 Mon Sep 17 00:00:00 2001 From: Matyrobbrt Date: Wed, 20 Apr 2022 14:06:29 +0300 Subject: [PATCH 2/3] Update CommandEvent.java --- .../jdautilities/command/CommandEvent.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java b/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java index 3dc77df7..08cd85e2 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java @@ -1220,13 +1220,12 @@ public boolean isFromType(ChannelType channelType) public S getGuildSettings() { try { - getGuild(); + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + return manager.getSettings(getGuild()); } catch (IllegalStateException ignored) { return null; } - final GuildSettingsManager manager = getClient().getSettingsManager(); - if (manager == null) return null; - return manager.getSettings(getGuild()); } /** @@ -1248,13 +1247,13 @@ public S getGuildSettings(Class settingsClazz) { try { getGuild(); + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + final Object settings = manager.getSettings(getGuild()); + if (!settingsClazz.isInstance(settings)) return null; + return settingsClazz.cast(settings); } catch (IllegalStateException ignored) { return null; } - final GuildSettingsManager manager = getClient().getSettingsManager(); - if (manager == null) return null; - final Object settings = manager.getSettings(getGuild()); - if (!settingsClazz.isInstance(settings)) return null; - return settingsClazz.cast(settings); } } From 237a2f3c2e27f64eef6d2a79f35a97f250db2ea3 Mon Sep 17 00:00:00 2001 From: matyrobbrt Date: Sun, 7 Aug 2022 17:53:56 +0300 Subject: [PATCH 3/3] Update SlashCommandEvent.java --- .../java/com/jagrosh/jdautilities/command/SlashCommandEvent.java | 1 + 1 file changed, 1 insertion(+) diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java index d5fa2f94..223e26d6 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java @@ -402,6 +402,7 @@ public S getGuildSettings(Class settingsClazz) return settingsClazz.cast(settings); } + /** * Gets the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} that this CommandEvent * may have taken place on, or {@code null} if it didn't happen on a TextChannel. *