From d2c28c1214d2a6a8ffed8264d08d20d895957c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Wed, 24 Aug 2022 16:15:58 +0200 Subject: [PATCH 1/7] Add GuildManager#setFeatures --- .../jda/api/managers/GuildManager.java | 9 ++++++++ .../internal/managers/GuildManagerImpl.java | 23 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java index 6a34aa6b62..54f519c73c 100644 --- a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java +++ b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java @@ -24,6 +24,7 @@ import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Collection; /** * Manager providing functionality to update one or more fields for a {@link net.dv8tion.jda.api.entities.Guild Guild}. @@ -73,6 +74,8 @@ public interface GuildManager extends Manager long COMMUNITY_UPDATES_CHANNEL = 1 << 13; /** Used to reset the premium progress bar enabled field */ long BOOST_PROGRESS_BAR_ENABLED = 1 << 14; + /** Used to add or remove modifiable features (such as {@code "INVITES_DISABLED"}) */ + long FEATURES = 1 << 15; /** * Resets the fields specified by the provided bit-flag pattern. @@ -94,6 +97,7 @@ public interface GuildManager extends Manager *
  • {@link #EXPLICIT_CONTENT_LEVEL}
  • *
  • {@link #VERIFICATION_LEVEL}
  • *
  • {@link #BOOST_PROGRESS_BAR_ENABLED}
  • + *
  • {@link #FEATURES}
  • * * * @param fields @@ -125,6 +129,7 @@ public interface GuildManager extends Manager *
  • {@link #EXPLICIT_CONTENT_LEVEL}
  • *
  • {@link #VERIFICATION_LEVEL}
  • *
  • {@link #BOOST_PROGRESS_BAR_ENABLED}
  • + *
  • {@link #FEATURES}
  • * * * @param fields @@ -372,4 +377,8 @@ public interface GuildManager extends Manager @Nonnull @CheckReturnValue GuildManager setBoostProgressBarEnabled(boolean boostProgressBarEnabled); + + @Nonnull + @CheckReturnValue + GuildManager setFeatures(@Nonnull Collection features); } diff --git a/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java b/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java index a8dba536de..d6dcef5b80 100644 --- a/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java @@ -16,7 +16,6 @@ package net.dv8tion.jda.internal.managers; -import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Icon; @@ -32,6 +31,9 @@ import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; public class GuildManagerImpl extends ManagerBase implements GuildManager { @@ -47,11 +49,11 @@ public class GuildManagerImpl extends ManagerBase implements Guild protected int explicitContentLevel; protected int verificationLevel; protected boolean boostProgressBarEnabled; + protected List features; public GuildManagerImpl(Guild guild) { super(guild.getJDA(), Route.Guilds.MODIFY_GUILD.compile(guild.getId())); - JDA api = guild.getJDA(); this.guild = guild; if (isPermissionChecksEnabled()) checkPermissions(); @@ -91,6 +93,8 @@ public GuildManagerImpl reset(long fields) this.description = null; if ((fields & BANNER) == BANNER) this.banner = null; + if ((fields & FEATURES) == FEATURES) + this.features = null; return this; } @@ -116,6 +120,7 @@ public GuildManagerImpl reset() this.banner = null; this.afkChannel = null; this.systemChannel = null; + this.features = null; return this; } @@ -284,6 +289,18 @@ public GuildManager setBoostProgressBarEnabled(boolean enabled) return this; } + @Nonnull + @Override + public GuildManager setFeatures(@Nonnull Collection features) + { + Checks.noneNull(features, "Features"); + this.features = features.stream() + .map(String::toUpperCase) + .collect(Collectors.toList()); + set |= FEATURES; + return this; + } + @Override protected RequestBody finalizeData() { @@ -318,6 +335,8 @@ protected RequestBody finalizeData() body.put("description", description); if (shouldUpdate(BOOST_PROGRESS_BAR_ENABLED)) body.put("premium_progress_bar_enabled", boostProgressBarEnabled); + if (shouldUpdate(FEATURES)) + body.put("features", features); reset(); //now that we've built our JSON object, reset the manager back to the non-modified state return getRequestBody(body); From f8a8b007c37fcd8d0da6e99e09656657a1a6e17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Tue, 30 Aug 2022 11:19:25 +0200 Subject: [PATCH 2/7] Add documentation --- .../net/dv8tion/jda/api/entities/Guild.java | 12 ++++++------ .../jda/api/managers/GuildManager.java | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/entities/Guild.java b/src/main/java/net/dv8tion/jda/api/entities/Guild.java index d94e7fc73c..0839421994 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Guild.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Guild.java @@ -57,6 +57,9 @@ import net.dv8tion.jda.internal.utils.Helpers; import net.dv8tion.jda.internal.utils.concurrent.task.GatewayTask; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.time.Duration; import java.time.temporal.TemporalAccessor; import java.util.*; @@ -65,10 +68,6 @@ import java.util.function.Consumer; import java.util.function.Predicate; -import javax.annotation.CheckReturnValue; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - /** * Represents a Discord {@link net.dv8tion.jda.api.entities.Guild Guild}. * This should contain all information provided from Discord about a Guild. @@ -535,11 +534,12 @@ default ImageProxy getIcon() /** * The Features of the {@link net.dv8tion.jda.api.entities.Guild Guild}. - *

    - * List of Features * + *

    Features can be udpated using {@link GuildManager#setFeatures(Collection)}. * * @return Never-null, unmodifiable Set containing all of the Guild's features. + * + * @see List of Features */ @Nonnull Set getFeatures(); diff --git a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java index 54f519c73c..37ee637218 100644 --- a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java +++ b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java @@ -378,6 +378,25 @@ public interface GuildManager extends Manager @CheckReturnValue GuildManager setBoostProgressBarEnabled(boolean boostProgressBarEnabled); + /** + * Configures the new {@link Guild#getFeatures() features} of the {@link Guild}. + *
    You can only remove or add a limited subset of features as described in the Guild Features Docs. + * + *

    Example + *

    {@code
    +     * List features = new ArrayList<>(guild.getFeatures());
    +     * features.add("INVITES_DISABLED");
    +     * guild.getManager().setFeatures(features).queue();
    +     * }
    + * + * @param features + * The new features to use + * + * @throws IllegalArgumentException + * If the provided list is null or empty + * + * @return GuildManager for chaining convenience + */ @Nonnull @CheckReturnValue GuildManager setFeatures(@Nonnull Collection features); From 31ac017ca034fbdc9c9d95023411e6df6b002dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Tue, 30 Aug 2022 11:23:31 +0200 Subject: [PATCH 3/7] Fix docs for exception --- src/main/java/net/dv8tion/jda/api/managers/GuildManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java index 37ee637218..826a4896d1 100644 --- a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java +++ b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java @@ -393,7 +393,7 @@ public interface GuildManager extends Manager * The new features to use * * @throws IllegalArgumentException - * If the provided list is null or empty + * If the provided list is null * * @return GuildManager for chaining convenience */ From 97ed7dd587718767c09a6daadf122b27082fba00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Mon, 26 Sep 2022 13:30:45 +0200 Subject: [PATCH 4/7] Add some more helper functions --- .../jda/api/managers/GuildManager.java | 99 ++++++++++++++++++- .../internal/managers/GuildManagerImpl.java | 34 ++++++- 2 files changed, 129 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java index 3b17be6b64..8ffbca4f1d 100644 --- a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java +++ b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java @@ -20,10 +20,12 @@ import net.dv8tion.jda.api.entities.Icon; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; +import net.dv8tion.jda.internal.utils.Checks; import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Arrays; import java.util.Collection; /** @@ -380,7 +382,8 @@ public interface GuildManager extends Manager /** * Configures the new {@link Guild#getFeatures() features} of the {@link Guild}. - *
    You can only remove or add a limited subset of features as described in the Guild Features Docs. + *
    The list of available features, including which ones can be configured is available in the + * Official Discord API Documentation. * *

    Example *

    {@code
    @@ -400,4 +403,98 @@ public interface GuildManager extends Manager
         @Nonnull
         @CheckReturnValue
         GuildManager setFeatures(@Nonnull Collection features);
    +
    +    /**
    +     * Adds a {@link Guild#getFeatures() Guild Feature} to the list of features.
    +     * 
    The list of available features, including which ones can be configured is available in the + * Official Discord API Documentation. + * + * @param features + * The features to add + * + * @throws IllegalArgumentException + * If any of the provided features is null + * + * @return GuildManager for chaining convenience + */ + @Nonnull + @CheckReturnValue + GuildManager addFeatures(@Nonnull Collection features); + + /** + * Adds a {@link Guild#getFeatures() Guild Feature} to the list of features. + *
    The list of available features, including which ones can be configured is available in the + * Official Discord API Documentation. + * + * @param features + * The features to add + * + * @throws IllegalArgumentException + * If any of the provided features is null + * + * @return GuildManager for chaining convenience + */ + @Nonnull + @CheckReturnValue + default GuildManager addFeatures(@Nonnull String... features) + { + Checks.noneNull(features, "Features"); + return addFeatures(Arrays.asList(features)); + } + + /** + * Removes a {@link Guild#getFeatures() Guild Feature} from the list of features. + *
    The list of available features, including which ones can be configured is available in the + * Official Discord API Documentation. + * + * @param features + * The features to remove + * + * @throws IllegalArgumentException + * If any of the provided features is null + * + * @return GuildManager for chaining convenience + */ + @Nonnull + @CheckReturnValue + GuildManager removeFeatures(@Nonnull Collection features); + + /** + * Removes a {@link Guild#getFeatures() Guild Feature} from the list of features. + *
    The list of available features, including which ones can be configured is available in the + * Official Discord API Documentation. + * + * @param features + * The features to remove + * + * @throws IllegalArgumentException + * If any of the provided features is null + * + * @return GuildManager for chaining convenience + */ + @Nonnull + @CheckReturnValue + default GuildManager removeFeatures(@Nonnull String... features) + { + Checks.noneNull(features, "Features"); + return removeFeatures(Arrays.asList(features)); + } + + /** + * Configures the {@code INVITES_DISABLED} feature flag of this guild. + *
    This is equivalent to adding or removing the feature {@code INVITES_DISABLED} via {@link #setFeatures(Collection)}. + * + * @param disabled + * True, to pause/disable all invites to the guild + * + * @return GuildManager for chaining convenience + */ + @Nonnull + @CheckReturnValue + default GuildManager setInvitesDisabled(boolean disabled) + { + if (disabled) + return addFeatures("INVITES_DISABLED"); + return removeFeatures("INVITES_DISABLED"); + } } diff --git a/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java b/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java index c4c301eefc..8eb9c07b83 100644 --- a/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java @@ -32,7 +32,9 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.Collection; -import java.util.List; +import java.util.HashSet; +import java.util.Set; +import java.util.function.Consumer; import java.util.stream.Collectors; public class GuildManagerImpl extends ManagerBase implements GuildManager @@ -49,7 +51,7 @@ public class GuildManagerImpl extends ManagerBase implements Guild protected int explicitContentLevel; protected int verificationLevel; protected boolean boostProgressBarEnabled; - protected List features; + protected Set features; public GuildManagerImpl(Guild guild) { @@ -296,7 +298,33 @@ public GuildManager setFeatures(@Nonnull Collection features) Checks.noneNull(features, "Features"); this.features = features.stream() .map(String::toUpperCase) - .collect(Collectors.toList()); + .collect(Collectors.toSet()); + set |= FEATURES; + return this; + } + + @Nonnull + @Override + public GuildManager addFeatures(@Nonnull Collection features) + { + return updateFeatures(features, this.features::add); + } + + @Nonnull + @Override + public GuildManager removeFeatures(@Nonnull Collection features) + { + return updateFeatures(features, this.features::remove); + } + + private GuildManager updateFeatures(Collection changed, Consumer op) + { + Checks.noneNull(changed, "Features"); + if (this.features == null) + this.features = new HashSet<>(getGuild().getFeatures()); + changed.stream() + .map(String::toUpperCase) + .forEach(op); set |= FEATURES; return this; } From 5fd4c5ae0d7795440d478be4076c42bc582d5ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Mon, 26 Sep 2022 13:33:08 +0200 Subject: [PATCH 5/7] Add isInvitesDisabled --- src/main/java/net/dv8tion/jda/api/entities/Guild.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/net/dv8tion/jda/api/entities/Guild.java b/src/main/java/net/dv8tion/jda/api/entities/Guild.java index ba5b542761..6c8fe99763 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Guild.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Guild.java @@ -554,6 +554,17 @@ default ImageProxy getIcon() @Nonnull Set getFeatures(); + /** + * Whether the invites for this guild are paused/disabled. + *
    This is equivalent to {@code getFeatures().contains("INVITES_DISABLED")}. + * + * @return True, if invites are paused/disabled + */ + default boolean isInvitesDisabled() + { + return getFeatures().contains("INVITES_DISABLED"); + } + /** * The Discord hash-id of the splash image for this Guild. A Splash image is an image displayed when viewing a * Discord Guild Invite on the web or in client just before accepting or declining the invite. From 37fcc80b5da20f36925a1bb740de1a7c049db064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Mon, 3 Oct 2022 23:52:42 +0200 Subject: [PATCH 6/7] Fix typo Co-authored-by: freya02 <41875020+freya022@users.noreply.github.com> --- src/main/java/net/dv8tion/jda/api/entities/Guild.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/dv8tion/jda/api/entities/Guild.java b/src/main/java/net/dv8tion/jda/api/entities/Guild.java index 6c8fe99763..81cfdc4984 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Guild.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Guild.java @@ -545,7 +545,7 @@ default ImageProxy getIcon() /** * The Features of the {@link net.dv8tion.jda.api.entities.Guild Guild}. * - *

    Features can be udpated using {@link GuildManager#setFeatures(Collection)}. + *

    Features can be updated using {@link GuildManager#setFeatures(Collection)}. * * @return Never-null, unmodifiable Set containing all of the Guild's features. * From b56b18cd96933ba85fdc501f5b672581e0096ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Tue, 18 Oct 2022 13:37:17 +0200 Subject: [PATCH 7/7] Fix some grammar issues --- .../net/dv8tion/jda/api/managers/GuildManager.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java index 8ffbca4f1d..3fc4dfe102 100644 --- a/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java +++ b/src/main/java/net/dv8tion/jda/api/managers/GuildManager.java @@ -382,7 +382,7 @@ public interface GuildManager extends Manager /** * Configures the new {@link Guild#getFeatures() features} of the {@link Guild}. - *
    The list of available features, including which ones can be configured is available in the + *
    The list of available features, including which ones can be configured, is available in the * Official Discord API Documentation. * *

    Example @@ -406,7 +406,7 @@ public interface GuildManager extends Manager /** * Adds a {@link Guild#getFeatures() Guild Feature} to the list of features. - *
    The list of available features, including which ones can be configured is available in the + *
    The list of available features, including which ones can be configured, is available in the * Official Discord API Documentation. * * @param features @@ -423,7 +423,7 @@ public interface GuildManager extends Manager /** * Adds a {@link Guild#getFeatures() Guild Feature} to the list of features. - *
    The list of available features, including which ones can be configured is available in the + *
    The list of available features, including which ones can be configured, is available in the * Official Discord API Documentation. * * @param features @@ -444,7 +444,7 @@ default GuildManager addFeatures(@Nonnull String... features) /** * Removes a {@link Guild#getFeatures() Guild Feature} from the list of features. - *
    The list of available features, including which ones can be configured is available in the + *
    The list of available features, including which ones can be configured, is available in the * Official Discord API Documentation. * * @param features @@ -461,7 +461,7 @@ default GuildManager addFeatures(@Nonnull String... features) /** * Removes a {@link Guild#getFeatures() Guild Feature} from the list of features. - *
    The list of available features, including which ones can be configured is available in the + *
    The list of available features, including which ones can be configured, is available in the * Official Discord API Documentation. * * @param features