diff --git a/src/main/java/net/dv8tion/jda/api/Permission.java b/src/main/java/net/dv8tion/jda/api/Permission.java index 1b15040d78..55312836a5 100644 --- a/src/main/java/net/dv8tion/jda/api/Permission.java +++ b/src/main/java/net/dv8tion/jda/api/Permission.java @@ -15,11 +15,14 @@ */ package net.dv8tion.jda.api; +import net.dv8tion.jda.api.events.GenericEvent; +import net.dv8tion.jda.api.events.annotations.RequiredPermissions; import net.dv8tion.jda.internal.utils.Checks; import javax.annotation.Nonnull; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.EnumSet; import java.util.stream.Collectors; @@ -313,4 +316,48 @@ public static long getRaw(@Nonnull Collection permissions) return getRaw(permissions.toArray(EMPTY_PERMISSIONS)); } + + /** + * Parse the required permissions from the provided {@link GenericEvent Event Types}. + * + * @param events + * The event types + * + * @throws IllegalArgumentException + * If provided with null + * + * @return {@link EnumSet} for the required permissions + */ + @Nonnull + @SafeVarargs + public static EnumSet fromEvents(@Nonnull Class... events) + { + Checks.noneNull(events, "Event"); + return fromEvents(Arrays.asList(events)); + } + + /** + * Parse the required permissions from the provided {@link GenericEvent Event Types}. + * + * @param events + * The event types + * + * @throws IllegalArgumentException + * If provided with null + * + * @return {@link EnumSet} for the required permissions + */ + @Nonnull + public static EnumSet fromEvents(@Nonnull Collection> events) + { + Checks.noneNull(events, "Events"); + EnumSet flags = EnumSet.noneOf(Permission.class); + for (Class event : events) + { + final RequiredPermissions requiredPermissions = event.getDeclaredAnnotation(RequiredPermissions.class); + if (requiredPermissions != null) + Collections.addAll(flags, requiredPermissions.always()); + } + return flags; + } } diff --git a/src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java b/src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java index 89814da397..5964f0477c 100644 --- a/src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java +++ b/src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java @@ -22,24 +22,8 @@ import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.GenericEvent; -import net.dv8tion.jda.api.events.automod.AutoModExecutionEvent; -import net.dv8tion.jda.api.events.automod.GenericAutoModRuleEvent; -import net.dv8tion.jda.api.events.emoji.GenericEmojiEvent; -import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent; -import net.dv8tion.jda.api.events.guild.GuildBanEvent; -import net.dv8tion.jda.api.events.guild.GuildUnbanEvent; -import net.dv8tion.jda.api.events.guild.invite.GenericGuildInviteEvent; -import net.dv8tion.jda.api.events.guild.member.GenericGuildMemberEvent; -import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; -import net.dv8tion.jda.api.events.guild.scheduledevent.update.GenericScheduledEventUpdateEvent; -import net.dv8tion.jda.api.events.guild.voice.GenericGuildVoiceEvent; +import net.dv8tion.jda.api.events.annotations.RequiredIntents; import net.dv8tion.jda.api.events.message.GenericMessageEvent; -import net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent; -import net.dv8tion.jda.api.events.message.react.GenericMessageReactionEvent; -import net.dv8tion.jda.api.events.sticker.GenericGuildStickerEvent; -import net.dv8tion.jda.api.events.user.UserTypingEvent; -import net.dv8tion.jda.api.events.user.update.GenericUserPresenceEvent; -import net.dv8tion.jda.api.events.user.update.GenericUserUpdateEvent; import net.dv8tion.jda.api.utils.cache.CacheFlag; import net.dv8tion.jda.internal.utils.Checks; @@ -405,44 +389,15 @@ public static EnumSet fromEvents(@Nonnull Class fromEvents(@Nonnull Collection> events) { + Checks.noneNull(events, "Events"); EnumSet intents = EnumSet.noneOf(GatewayIntent.class); for (Class event : events) { - Checks.notNull(event, "Event"); - - if (GenericUserPresenceEvent.class.isAssignableFrom(event)) - intents.add(GUILD_PRESENCES); - else if (GenericUserUpdateEvent.class.isAssignableFrom(event) || GenericGuildMemberEvent.class.isAssignableFrom(event) || GuildMemberRemoveEvent.class.isAssignableFrom(event)) - intents.add(GUILD_MEMBERS); - - else if (GuildBanEvent.class.isAssignableFrom(event) || GuildUnbanEvent.class.isAssignableFrom(event) || GuildAuditLogEntryCreateEvent.class.isAssignableFrom(event)) - intents.add(GUILD_MODERATION); - else if (GenericEmojiEvent.class.isAssignableFrom(event) || GenericGuildStickerEvent.class.isAssignableFrom(event)) - intents.add(GUILD_EMOJIS_AND_STICKERS); - else if (GenericScheduledEventUpdateEvent.class.isAssignableFrom(event)) - intents.add(SCHEDULED_EVENTS); - else if (GenericGuildInviteEvent.class.isAssignableFrom(event)) - intents.add(GUILD_INVITES); - else if (GenericGuildVoiceEvent.class.isAssignableFrom(event)) - intents.add(GUILD_VOICE_STATES); - - else if (MessageBulkDeleteEvent.class.isAssignableFrom(event)) - intents.add(GUILD_MESSAGES); - - else if (GenericMessageReactionEvent.class.isAssignableFrom(event)) - Collections.addAll(intents, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGE_REACTIONS); - - else if (GenericMessageEvent.class.isAssignableFrom(event)) - Collections.addAll(intents, GUILD_MESSAGES, DIRECT_MESSAGES); - - else if (UserTypingEvent.class.isAssignableFrom(event)) - Collections.addAll(intents, GUILD_MESSAGE_TYPING, DIRECT_MESSAGE_TYPING); - - else if (AutoModExecutionEvent.class.isAssignableFrom(event)) - intents.add(AUTO_MODERATION_EXECUTION); - else if (GenericAutoModRuleEvent.class.isAssignableFrom(event)) - intents.add(AUTO_MODERATION_CONFIGURATION); + final RequiredIntents requiredIntents = event.getDeclaredAnnotation(RequiredIntents.class); + if (requiredIntents != null) + Collections.addAll(intents, requiredIntents.always()); } + return intents; } diff --git a/src/main/java/net/dv8tion/jda/api/utils/cache/CacheFlag.java b/src/main/java/net/dv8tion/jda/api/utils/cache/CacheFlag.java index d00955ad78..f4272cc8eb 100644 --- a/src/main/java/net/dv8tion/jda/api/utils/cache/CacheFlag.java +++ b/src/main/java/net/dv8tion/jda/api/utils/cache/CacheFlag.java @@ -21,10 +21,16 @@ import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.channel.attribute.IPostContainer; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; +import net.dv8tion.jda.api.events.GenericEvent; +import net.dv8tion.jda.api.events.annotations.RequiredCacheFlags; import net.dv8tion.jda.api.requests.GatewayIntent; +import net.dv8tion.jda.internal.utils.Checks; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.EnumSet; /** @@ -137,4 +143,48 @@ public static EnumSet getPrivileged() { return EnumSet.copyOf(privileged); } + + /** + * Parse the required cache flags from the provided {@link GenericEvent Event Types}. + * + * @param events + * The event types + * + * @throws IllegalArgumentException + * If provided with null + * + * @return {@link EnumSet} for the required cache flags + */ + @Nonnull + @SafeVarargs + public static EnumSet fromEvents(@Nonnull Class... events) + { + Checks.noneNull(events, "Event"); + return fromEvents(Arrays.asList(events)); + } + + /** + * Parse the required cache flags from the provided {@link GenericEvent Event Types}. + * + * @param events + * The event types + * + * @throws IllegalArgumentException + * If provided with null + * + * @return {@link EnumSet} for the required cache flags + */ + @Nonnull + public static EnumSet fromEvents(@Nonnull Collection> events) + { + Checks.noneNull(events, "Events"); + EnumSet flags = EnumSet.noneOf(CacheFlag.class); + for (Class event : events) + { + final RequiredCacheFlags requiredCacheFlags = event.getDeclaredAnnotation(RequiredCacheFlags.class); + if (requiredCacheFlags != null) + Collections.addAll(flags, requiredCacheFlags.always()); + } + return flags; + } }