Skip to content

Commit

Permalink
Add default command restrictions for restricting commands to be only …
Browse files Browse the repository at this point in the history
…usable in a guild or in DMs.
  • Loading branch information
stelitop committed Aug 24, 2024
1 parent 9fb8faf commit c44b035
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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<Void> 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!");
}
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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<Void> 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!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +18,7 @@ public List<Class<?>> resultTypes() {

@Override
public List<Class<? extends Event>> eventTypes() {
return List.of(ChatInputInteractionEvent.class);
return List.of(InteractionCreateEvent.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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!";
}
}

0 comments on commit c44b035

Please sign in to comment.