Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guild settings getter in command events #65

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 <S> the type of the settings
* @return the settings, or {@code null} if either of the following conditions are met:
* <ul>
* <li>this command wasn't run in a guild</li>
* <li>the client's {@link GuildSettingsManager} is null</li>
* <li>the {@link GuildSettingsManager} returned null settings for the guild</li>
* </ul>
*/
@Nullable
public <S> S getGuildSettings()
{
try {
final GuildSettingsManager<S> 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 <S> the type of the settings
* @return the settings, or {@code null} if either of the following conditions are met:
* <ul>
* <li>this command wasn't run in a guild</li>
* <li>the client's {@link GuildSettingsManager} is null</li>
* <li>the {@link GuildSettingsManager} returned null settings for the guild</li>
* <li>the {@link GuildSettingsManager} returned settings that are not assignable to the {@code settingsClazz}</li>
* </ul>
*/
@Nullable
@SuppressWarnings("rawtypes")
public <S> S getGuildSettings(Class<? extends S> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <S> the type of the settings
* @return the settings, or {@code null} if either of the following conditions are met:
* <ul>
* <li>this interaction didn't happen in a guild</li>
* <li>the client's {@link GuildSettingsManager} is null</li>
* <li>the {@link GuildSettingsManager} returned null settings for the guild</li>
* </ul>
*/
@Nullable
public <S> S getGuildSettings()
{
if (!isFromGuild()) {
return null;
}
final GuildSettingsManager<S> 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 <S> the type of the settings
* @return the settings, or {@code null} if either of the following conditions are met:
* <ul>
* <li>this interaction didn't happen in a guild</li>
* <li>the client's {@link GuildSettingsManager} is null</li>
* <li>the {@link GuildSettingsManager} returned null settings for the guild</li>
* <li>the {@link GuildSettingsManager} returned settings that are not assignable to the {@code settingsClazz}</li>
* </ul>
*/
@Nullable
@SuppressWarnings("rawtypes")
public <S> S getGuildSettings(Class<? extends S> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <S> the type of the settings
* @return the settings, or {@code null} if either of the following conditions are met:
* <ul>
* <li>this interaction didn't happen in a guild</li>
* <li>the client's {@link GuildSettingsManager} is null</li>
* <li>the {@link GuildSettingsManager} returned null settings for the guild</li>
* </ul>
*/
@Nullable
public <S> S getGuildSettings()
{
if (!isFromGuild()) {
return null;
}
final GuildSettingsManager<S> 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 <S> the type of the settings
* @return the settings, or {@code null} if either of the following conditions are met:
* <ul>
* <li>this interaction didn't happen in a guild</li>
* <li>the client's {@link GuildSettingsManager} is null</li>
* <li>the {@link GuildSettingsManager} returned null settings for the guild</li>
* <li>the {@link GuildSettingsManager} returned settings that are not assignable to the {@code settingsClazz}</li>
* </ul>
*/
@Nullable
@SuppressWarnings("rawtypes")
public <S> S getGuildSettings(Class<? extends S> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <S> the type of the settings
* @return the settings, or {@code null} if either of the following conditions are met:
* <ul>
* <li>this interaction didn't happen in a guild</li>
* <li>the client's {@link GuildSettingsManager} is null</li>
* <li>the {@link GuildSettingsManager} returned null settings for the guild</li>
* </ul>
*/
@Nullable
public <S> S getGuildSettings()
{
if (!isFromGuild()) {
return null;
}
final GuildSettingsManager<S> 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 <S> the type of the settings
* @return the settings, or {@code null} if either of the following conditions are met:
* <ul>
* <li>this interaction didn't happen in a guild</li>
* <li>the client's {@link GuildSettingsManager} is null</li>
* <li>the {@link GuildSettingsManager} returned null settings for the guild</li>
* <li>the {@link GuildSettingsManager} returned settings that are not assignable to the {@code settingsClazz}</li>
* </ul>
*/
@Nullable
@SuppressWarnings("rawtypes")
public <S> S getGuildSettings(Class<? extends S> 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);
}
}