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 9ef38918..4d6e678e 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/CommandEvent.java @@ -26,6 +26,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}, @@ -1204,4 +1205,56 @@ 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 { + final GuildSettingsManager manager = getClient().getSettingsManager(); + if (manager == null) return null; + return manager.getSettings(getGuild()); + } catch (IllegalStateException ignored) { + return null; + } + } + + /** + * 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(); + 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; + } + } } 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 86b2b1ca..223e26d6 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommandEvent.java @@ -353,6 +353,55 @@ 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); + } + /** * 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. 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); + } }