-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default command restrictions for restricting commands to be only …
…usable in a guild or in DMs.
- Loading branch information
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...src/main/java/net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
27 changes: 27 additions & 0 deletions
27
...net/stelitop/mad4j/commands/requirements/standard/DMCommandRequirementImplementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../main/java/net/stelitop/mad4j/commands/requirements/standard/GuildCommandRequirement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
21 changes: 21 additions & 0 deletions
21
.../stelitop/mad4j/commands/requirements/standard/GuildCommandRequirementImplementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters