From c44b035012e8df068c46d5277190c59a911b8909 Mon Sep 17 00:00:00 2001 From: Stiliyan Nanovski Date: Sat, 24 Aug 2024 18:50:33 +0300 Subject: [PATCH] Add default command restrictions for restricting commands to be only usable in a guild or in DMs. --- .../standard/DMCommandRequirement.java | 15 +++++++++++ .../DMCommandRequirementImplementation.java | 27 +++++++++++++++++++ .../standard/GuildCommandRequirement.java | 15 +++++++++++ ...GuildCommandRequirementImplementation.java | 21 +++++++++++++++ .../SlashCommandEventResponseEventResult.java | 3 ++- .../commands/slash/BasicCommands.java | 20 ++++++++++++++ 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirement.java create mode 100644 library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirementImplementation.java create mode 100644 library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirement.java create mode 100644 library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirementImplementation.java diff --git a/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirement.java b/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirement.java new file mode 100644 index 0000000..bcd7259 --- /dev/null +++ b/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirement.java @@ -0,0 +1,15 @@ +package net.stelitop.mad4j.commands.requirements.standard; + +import net.stelitop.mad4j.commands.requirements.CommandRequirement; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@CommandRequirement(implementation = DMCommandRequirementImplementation.class) +public @interface DMCommandRequirement { +} diff --git a/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirementImplementation.java b/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirementImplementation.java new file mode 100644 index 0000000..7585959 --- /dev/null +++ b/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirementImplementation.java @@ -0,0 +1,27 @@ +package net.stelitop.mad4j.commands.requirements.standard; + +import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; +import discord4j.core.object.entity.channel.Channel; +import discord4j.core.object.entity.channel.MessageChannel; +import net.stelitop.mad4j.DiscordEventsComponent; +import net.stelitop.mad4j.commands.requirements.CommandRequirement; +import net.stelitop.mad4j.commands.requirements.CommandRequirementExecutor; +import net.stelitop.mad4j.utils.ActionResult; +import org.springframework.stereotype.Component; + +@DiscordEventsComponent +public class DMCommandRequirementImplementation implements CommandRequirementExecutor { + @Override + public ActionResult verify(ChatInputInteractionEvent event) { + MessageChannel channel = event.getInteraction().getChannel().block(); + if (channel == null) { + throw new NullPointerException("Could not get the channel of an interaction!"); + } + boolean inPrivate = channel.getType().equals(Channel.Type.DM); + if (inPrivate) { + return ActionResult.success(); + } else { + return ActionResult.fail("This command only works in DMs!"); + } + } +} diff --git a/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirement.java b/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirement.java new file mode 100644 index 0000000..a6edcda --- /dev/null +++ b/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirement.java @@ -0,0 +1,15 @@ +package net.stelitop.mad4j.commands.requirements.standard; + +import net.stelitop.mad4j.commands.requirements.CommandRequirement; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@CommandRequirement(implementation = GuildCommandRequirementImplementation.class) +public @interface GuildCommandRequirement { +} diff --git a/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirementImplementation.java b/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirementImplementation.java new file mode 100644 index 0000000..d2768a9 --- /dev/null +++ b/library/src/main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirementImplementation.java @@ -0,0 +1,21 @@ +package net.stelitop.mad4j.commands.requirements.standard; + +import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; +import net.stelitop.mad4j.DiscordEventsComponent; +import net.stelitop.mad4j.commands.requirements.CommandRequirement; +import net.stelitop.mad4j.commands.requirements.CommandRequirementExecutor; +import net.stelitop.mad4j.utils.ActionResult; +import org.springframework.stereotype.Component; + +@DiscordEventsComponent +public class GuildCommandRequirementImplementation implements CommandRequirementExecutor { + @Override + public ActionResult verify(ChatInputInteractionEvent event) { + boolean inGuild = event.getInteraction().getGuildId().isPresent(); + if (inGuild) { + return ActionResult.success(); + } else { + return ActionResult.fail("This command only works in a server!"); + } + } +} diff --git a/library/src/main/java/net/stelitop/mad4j/events/SlashCommandEventResponseEventResult.java b/library/src/main/java/net/stelitop/mad4j/events/SlashCommandEventResponseEventResult.java index 35742ce..ff65033 100644 --- a/library/src/main/java/net/stelitop/mad4j/events/SlashCommandEventResponseEventResult.java +++ b/library/src/main/java/net/stelitop/mad4j/events/SlashCommandEventResponseEventResult.java @@ -2,6 +2,7 @@ import discord4j.core.event.domain.Event; import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; +import discord4j.core.event.domain.interaction.InteractionCreateEvent; import net.stelitop.mad4j.interactions.EventResponse; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; @@ -17,7 +18,7 @@ public List> resultTypes() { @Override public List> eventTypes() { - return List.of(ChatInputInteractionEvent.class); + return List.of(InteractionCreateEvent.class); } @Override diff --git a/test-bots/general-bot/src/main/java/net/stelitop/generalbot/commands/slash/BasicCommands.java b/test-bots/general-bot/src/main/java/net/stelitop/generalbot/commands/slash/BasicCommands.java index fd3aca9..725b59a 100644 --- a/test-bots/general-bot/src/main/java/net/stelitop/generalbot/commands/slash/BasicCommands.java +++ b/test-bots/general-bot/src/main/java/net/stelitop/generalbot/commands/slash/BasicCommands.java @@ -10,6 +10,8 @@ import net.stelitop.mad4j.commands.CommandParam; import net.stelitop.mad4j.commands.DefaultValue; import net.stelitop.mad4j.commands.SlashCommand; +import net.stelitop.mad4j.commands.requirements.standard.DMCommandRequirement; +import net.stelitop.mad4j.commands.requirements.standard.GuildCommandRequirement; import org.springframework.beans.factory.annotation.Autowired; import reactor.core.publisher.Mono; @@ -121,4 +123,22 @@ public EmbedCreateSpec embedResponse() { .color(Color.BLUE) .build(); } + + @GuildCommandRequirement + @SlashCommand( + name = "basic guildonly", + description = "Only usable in guilds." + ) + public String guildOnlyCommand() { + return "Hello guild!"; + } + + @DMCommandRequirement + @SlashCommand( + name = "basic dmonly", + description = "Only usable in guilds." + ) + public String dmOnlyCommand() { + return "Hello dms!"; + } }