Skip to content

Commit

Permalink
Read requirement annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Apr 23, 2024
1 parent 95e411e commit 44e57a9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 51 deletions.
47 changes: 47 additions & 0 deletions src/main/java/net/dv8tion/jda/api/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -313,4 +316,48 @@ public static long getRaw(@Nonnull Collection<Permission> 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<Permission> fromEvents(@Nonnull Class<? extends GenericEvent>... 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<Permission> fromEvents(@Nonnull Collection<Class<? extends GenericEvent>> events)
{
Checks.noneNull(events, "Events");
EnumSet<Permission> flags = EnumSet.noneOf(Permission.class);
for (Class<? extends GenericEvent> event : events)
{
final RequiredPermissions requiredPermissions = event.getDeclaredAnnotation(RequiredPermissions.class);
if (requiredPermissions != null)
Collections.addAll(flags, requiredPermissions.always());
}
return flags;
}
}
57 changes: 6 additions & 51 deletions src/main/java/net/dv8tion/jda/api/requests/GatewayIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -405,44 +389,15 @@ public static EnumSet<GatewayIntent> fromEvents(@Nonnull Class<? extends Generic
@Nonnull
public static EnumSet<GatewayIntent> fromEvents(@Nonnull Collection<Class<? extends GenericEvent>> events)
{
Checks.noneNull(events, "Events");
EnumSet<GatewayIntent> intents = EnumSet.noneOf(GatewayIntent.class);
for (Class<? extends GenericEvent> 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;
}

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/cache/CacheFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -137,4 +143,48 @@ public static EnumSet<CacheFlag> 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<CacheFlag> fromEvents(@Nonnull Class<? extends GenericEvent>... 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<CacheFlag> fromEvents(@Nonnull Collection<Class<? extends GenericEvent>> events)
{
Checks.noneNull(events, "Events");
EnumSet<CacheFlag> flags = EnumSet.noneOf(CacheFlag.class);
for (Class<? extends GenericEvent> event : events)
{
final RequiredCacheFlags requiredCacheFlags = event.getDeclaredAnnotation(RequiredCacheFlags.class);
if (requiredCacheFlags != null)
Collections.addAll(flags, requiredCacheFlags.always());
}
return flags;
}
}

0 comments on commit 44e57a9

Please sign in to comment.