Skip to content
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
48 changes: 48 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,10 +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;

/**
Expand Down Expand Up @@ -257,4 +261,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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.events.annotations;

import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import java.lang.annotation.*;

/**
* Annotates the required cache flags for this event.
* <br>This is used by {@link CacheFlag#fromEvents(Class[])}
* to determine which cache flags are required and/or optional for a given event type.
*
* @see CacheFlag#fromEvents(Class[])
* @see JDABuilder#enableCache(CacheFlag, CacheFlag...)
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequiredCacheFlags
{
/**
* Cache flags required for the annotated event to fire.
*/
CacheFlag[] always() default {};

/**
* Cache flags which may be required for certain methods of the annotated event,
* or which may help fire the event under certain conditions.
*
* <p>The details should be documented on the annotated event.
*/
CacheFlag[] sometimes() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.events.annotations;

import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;

import java.lang.annotation.*;

/**
* Annotates the required intents for this event.
* <br>This is used by {@link GatewayIntent#fromEvents(Class[])}
* to determine which intents are required and/or optional for a given event type.
*
* @see GatewayIntent#fromEvents(Class[])
* @see JDABuilder#enableIntents(GatewayIntent, GatewayIntent...)
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequiredIntents
{
/**
* Gateway intents required for the annotated event to fire.
*/
GatewayIntent[] always() default {};

/**
* Gateway intents which may be required for certain methods of the annotated event,
* or which may help fire the event under certain conditions.
*
* <p>The details should be documented on the annotated event.
*/
GatewayIntent[] sometimes() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.events.annotations;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.IPermissionHolder;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;

import java.lang.annotation.*;

/**
* Annotates the required and/or optional permissions on this event.
*
* @see Permission#fromEvents(Class[])
* @see IPermissionHolder#hasPermission(GuildChannel, Permission...)
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequiredPermissions
{
/**
* Permissions required for the annotated event to fire.
*/
Permission[] always() default {};

/**
* Permissions which may be required for certain methods of the annotated event,
* or which may help fire the event under certain conditions.
*
* <p>The details should be documented on the annotated event.
*/
Permission[] sometimes() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.events.annotations;

import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;

import java.lang.annotation.*;

/**
* Annotates this event as requiring a cached member to fire,
* or a method requiring a cached member to return appropriate results.
*
* <p>For a member/user to be cached, {@link GatewayIntent#GUILD_MEMBERS GatewayIntent.GUILD_MEMBERS} needs to be enabled,
* and the {@link MemberCachePolicy} configured to allow some, if not all, members to be cached.
* <br>Assuming the cache policy allows a member to be cached, the member will be loaded in the cache when either:
* <ul>
* <li>JDA loads it on startup, if a {@link ChunkingFilter} is configured</li>
* <li>It is loaded explicitly, for example, using {@link Guild#retrieveMemberById(long)}</li>
* <li>An event containing a member is received, such as {@link SlashCommandInteractionEvent}</li>
* </ul>
*
* @see MemberCachePolicy
* @see ChunkingFilter
* @see JDABuilder#setMemberCachePolicy(MemberCachePolicy)
* @see JDABuilder#setChunkingFilter(ChunkingFilter)
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequiresCachedMember
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package net.dv8tion.jda.api.events.automod;

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.automod.AutoModExecution;
import net.dv8tion.jda.api.entities.automod.AutoModResponse;
import net.dv8tion.jda.api.entities.automod.AutoModRule;
import net.dv8tion.jda.api.entities.automod.AutoModTriggerType;
import net.dv8tion.jda.api.entities.channel.unions.GuildMessageChannelUnion;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.annotations.RequiredIntents;
import net.dv8tion.jda.api.events.annotations.RequiredPermissions;
import net.dv8tion.jda.api.requests.GatewayIntent;

import javax.annotation.Nonnull;
Expand All @@ -37,6 +40,8 @@
* <br>This event will only fire for guilds where the bot has the {@link net.dv8tion.jda.api.Permission#MANAGE_SERVER MANAGE_SERVER} permission.
* Additionally, access to {@link #getContent()} and {@link #getMatchedContent()} requires the {@link GatewayIntent#MESSAGE_CONTENT MESSAGE_CONTENT} intent to be enabled.
*/
@RequiredIntents(always = GatewayIntent.AUTO_MODERATION_EXECUTION, sometimes = GatewayIntent.MESSAGE_CONTENT)
@RequiredPermissions(always = Permission.MANAGE_SERVER)
public class AutoModExecutionEvent extends Event implements AutoModExecution
{
private final AutoModExecution execution;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package net.dv8tion.jda.api.events.automod;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.automod.AutoModRule;
import net.dv8tion.jda.api.events.annotations.RequiredIntents;
import net.dv8tion.jda.api.events.annotations.RequiredPermissions;
import net.dv8tion.jda.api.requests.GatewayIntent;

import javax.annotation.Nonnull;

Expand All @@ -29,6 +33,8 @@
* <p>These events require the {@link net.dv8tion.jda.api.requests.GatewayIntent#AUTO_MODERATION_CONFIGURATION AUTO_MODERATION_CONFIGURATION} intent to be enabled.
* <br>This event will only fire for guilds where the bot has the {@link net.dv8tion.jda.api.Permission#MANAGE_SERVER MANAGE_SERVER} permission.
*/
@RequiredIntents(always = GatewayIntent.AUTO_MODERATION_CONFIGURATION)
@RequiredPermissions(always = Permission.MANAGE_SERVER)
public class AutoModRuleCreateEvent extends GenericAutoModRuleEvent
{
public AutoModRuleCreateEvent(@Nonnull JDA api, long responseNumber, @Nonnull AutoModRule rule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package net.dv8tion.jda.api.events.automod;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.automod.AutoModRule;
import net.dv8tion.jda.api.events.annotations.RequiredIntents;
import net.dv8tion.jda.api.events.annotations.RequiredPermissions;
import net.dv8tion.jda.api.requests.GatewayIntent;

import javax.annotation.Nonnull;

Expand All @@ -29,6 +33,8 @@
* <p>These events require the {@link net.dv8tion.jda.api.requests.GatewayIntent#AUTO_MODERATION_CONFIGURATION AUTO_MODERATION_CONFIGURATION} intent to be enabled.
* <br>This event will only fire for guilds where the bot has the {@link net.dv8tion.jda.api.Permission#MANAGE_SERVER MANAGE_SERVER} permission.
*/
@RequiredIntents(always = GatewayIntent.AUTO_MODERATION_CONFIGURATION)
@RequiredPermissions(always = Permission.MANAGE_SERVER)
public class AutoModRuleDeleteEvent extends GenericAutoModRuleEvent
{
public AutoModRuleDeleteEvent(@Nonnull JDA api, long responseNumber, @Nonnull AutoModRule rule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package net.dv8tion.jda.api.events.automod;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.automod.AutoModRule;
import net.dv8tion.jda.api.events.annotations.RequiredIntents;
import net.dv8tion.jda.api.events.annotations.RequiredPermissions;
import net.dv8tion.jda.api.requests.GatewayIntent;

import javax.annotation.Nonnull;

Expand All @@ -29,6 +33,8 @@
* <p>These events require the {@link net.dv8tion.jda.api.requests.GatewayIntent#AUTO_MODERATION_CONFIGURATION AUTO_MODERATION_CONFIGURATION} intent to be enabled.
* <br>This event will only fire for guilds where the bot has the {@link net.dv8tion.jda.api.Permission#MANAGE_SERVER MANAGE_SERVER} permission.
*/
@RequiredIntents(always = GatewayIntent.AUTO_MODERATION_CONFIGURATION)
@RequiredPermissions(always = Permission.MANAGE_SERVER)
public class AutoModRuleUpdateEvent extends GenericAutoModRuleEvent
{
public AutoModRuleUpdateEvent(@Nonnull JDA api, long responseNumber, @Nonnull AutoModRule rule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
package net.dv8tion.jda.api.events.automod;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.automod.AutoModRule;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.annotations.RequiredIntents;
import net.dv8tion.jda.api.events.annotations.RequiredPermissions;
import net.dv8tion.jda.api.requests.GatewayIntent;

import javax.annotation.Nonnull;

Expand All @@ -30,6 +34,8 @@
* <p>These events require the {@link net.dv8tion.jda.api.requests.GatewayIntent#AUTO_MODERATION_CONFIGURATION AUTO_MODERATION_CONFIGURATION} intent to be enabled.
* <br>These events will only fire for guilds where the bot has the {@link net.dv8tion.jda.api.Permission#MANAGE_SERVER MANAGE_SERVER} permission.
*/
@RequiredIntents(always = GatewayIntent.AUTO_MODERATION_CONFIGURATION)
@RequiredPermissions(always = Permission.MANAGE_SERVER)
public class GenericAutoModRuleEvent extends Event
{
private final AutoModRule rule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.attribute.IPostContainer;
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
import net.dv8tion.jda.api.events.annotations.RequiredCacheFlags;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
import java.util.Collection;
Expand All @@ -33,6 +35,7 @@
* This requires {@link net.dv8tion.jda.api.utils.cache.CacheFlag#FORUM_TAGS CacheFlag.FORUM_TAGS} to be enabled.
* {@link net.dv8tion.jda.api.JDABuilder#createLight(String, Collection) JDABuilder.createLight(...)} disables this by default.
*/
@RequiredCacheFlags(always = CacheFlag.FORUM_TAGS)
public class ForumTagAddEvent extends GenericForumTagEvent
{
public ForumTagAddEvent(@Nonnull JDA api, long responseNumber, @Nonnull IPostContainer channel, @Nonnull ForumTag tag)
Expand Down
Loading