From e7a43d24ee0d15663160d79caac841fb750096c4 Mon Sep 17 00:00:00 2001 From: Sage Date: Tue, 25 Nov 2025 11:36:30 -0500 Subject: [PATCH] Implemented annotation for permissions --- .../jda6/annotation/JDAPermission.java | 51 ++++++++++++++++ .../JDAPermissionBuilderModifier.java | 61 +++++++++++++++++++ .../example/commands/AnnotatedCommands.java | 2 + 3 files changed, 114 insertions(+) create mode 100644 cloud-jda6/src/main/java/org/incendo/cloud/discord/jda6/annotation/JDAPermission.java create mode 100644 cloud-jda6/src/main/java/org/incendo/cloud/discord/jda6/annotation/JDAPermissionBuilderModifier.java diff --git a/cloud-jda6/src/main/java/org/incendo/cloud/discord/jda6/annotation/JDAPermission.java b/cloud-jda6/src/main/java/org/incendo/cloud/discord/jda6/annotation/JDAPermission.java new file mode 100644 index 0000000..b098bbb --- /dev/null +++ b/cloud-jda6/src/main/java/org/incendo/cloud/discord/jda6/annotation/JDAPermission.java @@ -0,0 +1,51 @@ +// +// MIT License +// +// Copyright (c) 2024 Incendo +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +package org.incendo.cloud.discord.jda6.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import net.dv8tion.jda.api.Permission; +import org.apiguardian.api.API; + +/** + * Annotation equivalent of {@link net.dv8tion.jda.api.Permission}. + * + *

This requires the installation of {@link JDAPermissionBuilderModifier}.

+ * + * @since 1.0.0 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@API(status = API.Status.STABLE, since = "1.0.0") +public @interface JDAPermission { + + /** + * Corresponds to {@link net.dv8tion.jda.api.Permission} with annotation support. + * + * @return what permission the user needs to run the command + */ + Permission permission(); +} diff --git a/cloud-jda6/src/main/java/org/incendo/cloud/discord/jda6/annotation/JDAPermissionBuilderModifier.java b/cloud-jda6/src/main/java/org/incendo/cloud/discord/jda6/annotation/JDAPermissionBuilderModifier.java new file mode 100644 index 0000000..f5b9cee --- /dev/null +++ b/cloud-jda6/src/main/java/org/incendo/cloud/discord/jda6/annotation/JDAPermissionBuilderModifier.java @@ -0,0 +1,61 @@ +// +// MIT License +// +// Copyright (c) 2024 Incendo +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +package org.incendo.cloud.discord.jda6.annotation; + +import java.util.Objects; +import org.apiguardian.api.API; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.incendo.cloud.Command; +import org.incendo.cloud.annotations.AnnotationParser; +import org.incendo.cloud.annotations.BuilderModifier; +import org.incendo.cloud.discord.slash.DiscordPermission; + +/** + * Builder modifier that enables the use of {@link JDAPermission}. + * + * @param command sender type + * @since 1.0.0 + */ +@API(status = API.Status.STABLE, since = "1.0.0") +public final class JDAPermissionBuilderModifier implements BuilderModifier { + + /** + * Installs the builder modifier. + * + * @param command sender type + * @param annotationParser annotation parser + */ + public static void install(final @NonNull AnnotationParser annotationParser) { + Objects.requireNonNull(annotationParser, "annotationParser"); + annotationParser.registerBuilderModifier(JDAPermission.class, new JDAPermissionBuilderModifier<>()); + } + + @Override + public Command.@NonNull Builder modifyBuilder( + final @NonNull JDAPermission annotation, + final Command.@NonNull Builder builder + ) { + return builder.permission(DiscordPermission.of(annotation.permission().getRawValue())); + } +} diff --git a/examples/example-jda6/src/main/java/org/incendo/cloud/discord/jda6/example/commands/AnnotatedCommands.java b/examples/example-jda6/src/main/java/org/incendo/cloud/discord/jda6/example/commands/AnnotatedCommands.java index 135c2ad..02e6f13 100644 --- a/examples/example-jda6/src/main/java/org/incendo/cloud/discord/jda6/example/commands/AnnotatedCommands.java +++ b/examples/example-jda6/src/main/java/org/incendo/cloud/discord/jda6/example/commands/AnnotatedCommands.java @@ -41,6 +41,7 @@ import org.incendo.cloud.discord.immutables.ImmutableImpl; import org.incendo.cloud.discord.jda6.JDA6CommandManager; import org.incendo.cloud.discord.jda6.JDAInteraction; +import org.incendo.cloud.discord.jda6.annotation.JDAPermissionBuilderModifier; import org.incendo.cloud.discord.jda6.annotation.ReplySetting; import org.incendo.cloud.discord.jda6.annotation.ReplySettingBuilderModifier; import org.incendo.cloud.discord.jda6.example.Example; @@ -63,6 +64,7 @@ public void register(final @NonNull JDA6CommandManager commandMa // Adds support for the JDA-specific annotations. ReplySettingBuilderModifier.install(annotationParser); + JDAPermissionBuilderModifier.install(annotationParser); CommandScopeBuilderModifier.install(annotationParser); // Parses @Command, @Parser, @Suggestions & @ExceptionHandler...