Skip to content

Commit

Permalink
Add @Requirements and @RequiresCachedMember, use them for cache f…
Browse files Browse the repository at this point in the history
…lags
  • Loading branch information
freya022 committed Oct 10, 2023
1 parent 5f9847c commit e331895
Show file tree
Hide file tree
Showing 59 changed files with 292 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import java.lang.annotation.*;

//TODO docs
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Requirements
{
GatewayIntent[] intents() default {};

GatewayIntent[] optionalIntents() default {};

CacheFlag[] cache() default {};

CacheFlag[] optionalCache() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 java.lang.annotation.*;

//TODO docs, explain cache policy, chunking and user actions adding them to the cache
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequiresCachedMember
{
}
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.Requirements;
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.
*/
@Requirements(cache = CacheFlag.FORUM_TAGS)
public class ForumTagAddEvent extends GenericForumTagEvent
{
public ForumTagAddEvent(@Nonnull JDA api, long responseNumber, @Nonnull IPostContainer channel, @Nonnull ForumTag tag)
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.Requirements;
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.
*/
@Requirements(cache = CacheFlag.FORUM_TAGS)
public class ForumTagRemoveEvent extends GenericForumTagEvent
{
public ForumTagRemoveEvent(@Nonnull JDA api, long responseNumber, @Nonnull IPostContainer channel, @Nonnull ForumTag tag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
import java.util.Collection;
Expand All @@ -32,6 +34,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.
*/
@Requirements(cache = CacheFlag.FORUM_TAGS)
public abstract class GenericForumTagEvent extends Event
{
protected final IPostContainer channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import net.dv8tion.jda.api.entities.channel.attribute.IPostContainer;
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
import net.dv8tion.jda.api.entities.emoji.EmojiUnion;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -34,6 +36,7 @@
*
* <p>Identifier: {@code emoji}
*/
@Requirements(cache = CacheFlag.FORUM_TAGS)
public class ForumTagUpdateEmojiEvent extends GenericForumTagUpdateEvent<EmojiUnion>
{
public static final String IDENTIFIER = "emoji";
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.Requirements;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
import java.util.Collection;
Expand All @@ -32,6 +34,7 @@
*
* <p>Identifier: {@code moderated}
*/
@Requirements(cache = CacheFlag.FORUM_TAGS)
@SuppressWarnings("ConstantConditions")
public class ForumTagUpdateModeratedEvent extends GenericForumTagUpdateEvent<Boolean>
{
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.Requirements;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
import java.util.Collection;
Expand All @@ -32,6 +34,7 @@
*
* <p>Identifier: {@code name}
*/
@Requirements(cache = CacheFlag.FORUM_TAGS)
@SuppressWarnings("ConstantConditions")
public class ForumTagUpdateNameEvent extends GenericForumTagUpdateEvent<String>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import net.dv8tion.jda.api.entities.channel.attribute.IPostContainer;
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
import net.dv8tion.jda.api.events.UpdateEvent;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.events.channel.forum.GenericForumTagEvent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
import java.util.Collection;
Expand All @@ -35,6 +37,7 @@
* @param <T>
* The type of the updated field
*/
@Requirements(cache = CacheFlag.FORUM_TAGS)
public abstract class GenericForumTagUpdateEvent<T> extends GenericForumTagEvent implements UpdateEvent<ForumTag, T>
{
private final T previous;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;

Expand All @@ -31,6 +34,7 @@
*
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables that CacheFlag by default!
*/
@Requirements(intents = GatewayIntent.GUILD_EMOJIS_AND_STICKERS, cache = CacheFlag.EMOJI)
public class EmojiAddedEvent extends GenericEmojiEvent
{
public EmojiAddedEvent(@Nonnull JDA api, long responseNumber, @Nonnull RichCustomEmoji emoji)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;

Expand All @@ -31,6 +34,7 @@
*
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables that CacheFlag by default!
*/
@Requirements(intents = GatewayIntent.GUILD_EMOJIS_AND_STICKERS, cache = CacheFlag.EMOJI)
public class EmojiRemovedEvent extends GenericEmojiEvent
{
public EmojiRemovedEvent(@Nonnull JDA api, long responseNumber, @Nonnull RichCustomEmoji emoji)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;

Expand All @@ -33,6 +36,7 @@
*
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables that CacheFlag by default!
*/
@Requirements(intents = GatewayIntent.GUILD_EMOJIS_AND_STICKERS, cache = CacheFlag.EMOJI)
public abstract class GenericEmojiEvent extends Event
{
protected final RichCustomEmoji emoji;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;

Expand All @@ -35,6 +38,7 @@
*
* <p>Identifier: {@code name}
*/
@Requirements(intents = GatewayIntent.GUILD_EMOJIS_AND_STICKERS, cache = CacheFlag.EMOJI)
public class EmojiUpdateNameEvent extends GenericEmojiUpdateEvent<String>
{
public static final String IDENTIFIER = "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
import java.util.List;
Expand All @@ -37,6 +40,7 @@
*
* <p>Identifier: {@code roles}
*/
@Requirements(intents = GatewayIntent.GUILD_EMOJIS_AND_STICKERS, cache = CacheFlag.EMOJI)
public class EmojiUpdateRolesEvent extends GenericEmojiUpdateEvent<List<Role>>
{
public static final String IDENTIFIER = "roles";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.events.UpdateEvent;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.events.emoji.GenericEmojiEvent;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -34,6 +37,7 @@
*
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables that CacheFlag by default!
*/
@Requirements(intents = GatewayIntent.GUILD_EMOJIS_AND_STICKERS, cache = CacheFlag.EMOJI)
public abstract class GenericEmojiUpdateEvent<T> extends GenericEmojiEvent implements UpdateEvent<RichCustomEmoji, T>
{
protected final T previous;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.dv8tion.jda.api.entities.channel.attribute.IPermissionContainer;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.channel.unions.IPermissionContainerUnion;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.events.guild.GenericGuildEvent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

Expand All @@ -43,6 +44,7 @@
* unless the member is the {@link net.dv8tion.jda.api.entities.Guild#getSelfMember() self member}.
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables this by default!
*/
@Requirements(optionalCache = CacheFlag.MEMBER_OVERRIDES)
public class GenericPermissionOverrideEvent extends GenericGuildEvent
{
protected final IPermissionContainer channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.PermissionOverride;
import net.dv8tion.jda.api.entities.channel.attribute.IPermissionContainer;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
Expand All @@ -37,6 +38,7 @@
* unless the member is the {@link net.dv8tion.jda.api.entities.Guild#getSelfMember() self member}.
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables this by default!
*/
@Requirements(optionalCache = CacheFlag.MEMBER_OVERRIDES)
public class PermissionOverrideCreateEvent extends GenericPermissionOverrideEvent
{
public PermissionOverrideCreateEvent(@Nonnull JDA api, long responseNumber, @Nonnull IPermissionContainer channel, @Nonnull PermissionOverride override)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.PermissionOverride;
import net.dv8tion.jda.api.entities.channel.attribute.IPermissionContainer;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
Expand All @@ -37,6 +38,7 @@
* unless the member is the {@link net.dv8tion.jda.api.entities.Guild#getSelfMember() self member}.
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables this by default!
*/
@Requirements(optionalCache = CacheFlag.MEMBER_OVERRIDES)
public class PermissionOverrideDeleteEvent extends GenericPermissionOverrideEvent
{
public PermissionOverrideDeleteEvent(@Nonnull JDA api, long responseNumber, @Nonnull IPermissionContainer channel, @Nonnull PermissionOverride override)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.PermissionOverride;
import net.dv8tion.jda.api.entities.channel.attribute.IPermissionContainer;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
Expand All @@ -42,6 +43,7 @@
* unless the member is the {@link net.dv8tion.jda.api.entities.Guild#getSelfMember() self member}.
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables this by default!
*/
@Requirements(optionalCache = CacheFlag.MEMBER_OVERRIDES)
public class PermissionOverrideUpdateEvent extends GenericPermissionOverrideEvent
{
private final long oldAllow, oldDeny;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.ScheduledEvent;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.events.guild.GenericGuildEvent;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import javax.annotation.Nonnull;
Expand All @@ -38,6 +40,7 @@
* <p>This class may be used to check if a gateway event is related to a {@link ScheduledEvent}
* as all gateway events in the {@link net.dv8tion.jda.api.events.guild.scheduledevent} package extend this class.
*/
@Requirements(intents = GatewayIntent.SCHEDULED_EVENTS, cache = CacheFlag.SCHEDULED_EVENTS)
public abstract class GenericScheduledEventGatewayEvent extends GenericGuildEvent
{
protected final ScheduledEvent scheduledEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.ScheduledEvent;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.annotations.Requirements;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.CacheRestAction;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
Expand All @@ -38,6 +40,7 @@
* <p>This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#SCHEDULED_EVENTS SCHEDULED_EVENTS} intent and {@link CacheFlag#SCHEDULED_EVENTS} to be enabled.
* <br>{@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disables this by default!
*/
@Requirements(intents = GatewayIntent.SCHEDULED_EVENTS, cache = CacheFlag.SCHEDULED_EVENTS)
public abstract class GenericScheduledEventUserEvent extends GenericScheduledEventGatewayEvent
{
private final long userId;
Expand Down
Loading

0 comments on commit e331895

Please sign in to comment.