-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ First pass on slash commands checker & responses
- Loading branch information
Showing
9 changed files
with
476 additions
and
59 deletions.
There are no files selected for viewing
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
85 changes: 85 additions & 0 deletions
85
src/main/java/com/greazi/discordbotfoundation/command/checkers/ChannelsChecker.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,85 @@ | ||
package com.greazi.discordbotfoundation.command.checkers; | ||
|
||
import com.greazi.discordbotfoundation.command.SimpleCommand; | ||
import com.greazi.discordbotfoundation.command.checkers.response.SlashCommandResponse; | ||
import net.dv8tion.jda.api.entities.Role; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
import net.dv8tion.jda.internal.utils.Checks; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Check if the channel is allowed/disallowed to execute the command. | ||
*/ | ||
public class ChannelsChecker implements SlashCommandChecker { | ||
|
||
//region Public API | ||
|
||
/** | ||
* Create a new channels checker that allows only the specified channels to execute the command. | ||
* @param allowedChannelsIds The list of roles ids that are allowed to execute the command. | ||
* @return The roles checker instance. | ||
*/ | ||
public static ChannelsChecker createAllowed(@NotNull List<String> allowedChannelsIds) { | ||
return create(allowedChannelsIds, new ArrayList<>()); | ||
} | ||
|
||
/** | ||
* Create a new channels checker that disallows the specified channels to execute the command. | ||
* @param disallowedRolesIds The list of roles ids that are disallowed to execute the command. | ||
* @return The roles checker instance. | ||
*/ | ||
public static ChannelsChecker createDisallowed(@NotNull List<String> disallowedRolesIds) { | ||
return create(new ArrayList<>(), disallowedRolesIds); | ||
} | ||
|
||
/** | ||
* Create a new channels checker that allows and disallows the specified channels to execute the command. | ||
* @param allowedChannelsIds The list of roles ids that are allowed to execute the command. | ||
* @param disallowedRolesIds The list of roles ids that are disallowed to execute the command. | ||
* @return The roles checker instance. | ||
*/ | ||
public static ChannelsChecker create(@NotNull List<String> allowedChannelsIds, | ||
@NotNull List<String> disallowedRolesIds) { | ||
Checks.notEmpty(allowedChannelsIds, "allowedChannelsIds"); | ||
Checks.noneNull(allowedChannelsIds, "allowedChannelsIds"); | ||
Checks.notEmpty(disallowedRolesIds, "disallowedRolesIds"); | ||
Checks.noneNull(disallowedRolesIds, "disallowedRolesIds"); | ||
|
||
return new ChannelsChecker(allowedChannelsIds, disallowedRolesIds); | ||
} | ||
|
||
//endregion | ||
|
||
private final List<String> allowedChannelsIds; | ||
private final List<String> disallowedRolesIds; | ||
private ChannelsChecker(List<String> allowedChannelsIds, List<String> disallowedRolesIds) { | ||
this.allowedChannelsIds = allowedChannelsIds; | ||
this.disallowedRolesIds = disallowedRolesIds; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public SlashCommandResponse check(@NotNull SimpleCommand command, | ||
@NotNull SlashCommandInteractionEvent context) { | ||
final var provider = command.getResponseProvider(); | ||
if (context.getMember() == null) { | ||
return provider.error("You must be in a guild to use this command."); | ||
} | ||
|
||
if (allowedChannelsIds.isEmpty() && disallowedRolesIds.isEmpty()) | ||
return provider.success(); | ||
|
||
final var channelId = context.getChannel().getId(); | ||
if (!allowedChannelsIds.isEmpty() && !allowedChannelsIds.contains(channelId)) | ||
return provider.error("You are not in the allowed channel to use this command."); | ||
|
||
if (!disallowedRolesIds.isEmpty() && disallowedRolesIds.contains(channelId)) | ||
return provider.error("You are in a disallowed channel to use this command."); | ||
|
||
return provider.success(); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/greazi/discordbotfoundation/command/checkers/RolesChecker.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,88 @@ | ||
package com.greazi.discordbotfoundation.command.checkers; | ||
|
||
import com.greazi.discordbotfoundation.command.SimpleCommand; | ||
import com.greazi.discordbotfoundation.command.checkers.response.SlashCommandResponse; | ||
import net.dv8tion.jda.api.entities.Role; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
import net.dv8tion.jda.internal.utils.Checks; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Check if the member has the required roles to execute the command. | ||
*/ | ||
public class RolesChecker implements SlashCommandChecker { | ||
|
||
//region Public API | ||
|
||
/** | ||
* Create a new roles checker that allows only the specified roles to execute the command. | ||
* @param allowedRolesIds The list of roles ids that are allowed to execute the command. | ||
* @return The roles checker instance. | ||
*/ | ||
public static RolesChecker createAllowed(@NotNull List<String> allowedRolesIds) { | ||
return create(allowedRolesIds, new ArrayList<>()); | ||
} | ||
|
||
/** | ||
* Create a new roles checker that disallows the specified roles to execute the command. | ||
* @param disallowedRolesIds The list of roles ids that are disallowed to execute the command. | ||
* @return The roles checker instance. | ||
*/ | ||
public static RolesChecker createDisallowed(@NotNull List<String> disallowedRolesIds) { | ||
return create(new ArrayList<>(), disallowedRolesIds); | ||
} | ||
|
||
/** | ||
* Create a new roles checker that allows and disallows the specified roles to execute the command. | ||
* @param allowedRolesIds The list of roles ids that are allowed to execute the command. | ||
* @param disallowedRolesIds The list of roles ids that are disallowed to execute the command. | ||
* @return The roles checker instance. | ||
*/ | ||
public static RolesChecker create(@NotNull List<String> allowedRolesIds, | ||
@NotNull List<String> disallowedRolesIds) { | ||
Checks.notEmpty(allowedRolesIds, "allowedRolesIds"); | ||
Checks.noneNull(allowedRolesIds, "allowedRolesIds"); | ||
Checks.notEmpty(disallowedRolesIds, "disallowedRolesIds"); | ||
Checks.noneNull(disallowedRolesIds, "disallowedRolesIds"); | ||
|
||
return new RolesChecker(allowedRolesIds, disallowedRolesIds); | ||
} | ||
|
||
//endregion | ||
|
||
private final List<String> allowedRolesIds; | ||
private final List<String> disallowedRolesIds; | ||
private RolesChecker(List<String> allowedRolesIds, List<String> disallowedRolesIds) { | ||
this.allowedRolesIds = allowedRolesIds; | ||
this.disallowedRolesIds = disallowedRolesIds; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public SlashCommandResponse check(@NotNull SimpleCommand command, | ||
@NotNull SlashCommandInteractionEvent context) { | ||
final var provider = command.getResponseProvider(); | ||
if (context.getMember() == null) { | ||
return provider.error("You must be in a guild to use this command."); | ||
} | ||
|
||
final var memberRoles = context.getMember().getRoles(); | ||
for (Role role : memberRoles) { | ||
if (disallowedRolesIds.contains(role.getId())) { | ||
return provider.error("You have a disallowed role to use this command."); | ||
} | ||
} | ||
|
||
for (String roleId : allowedRolesIds) { | ||
if (memberRoles.stream().noneMatch(role -> role.getId().equals(roleId))) { | ||
return provider.error("You don't have the required roles to use this command."); | ||
} | ||
} | ||
|
||
return provider.success(); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/greazi/discordbotfoundation/command/checkers/SlashCommandChecker.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,17 @@ | ||
package com.greazi.discordbotfoundation.command.checkers; | ||
|
||
import com.greazi.discordbotfoundation.command.SimpleCommand; | ||
import com.greazi.discordbotfoundation.command.checkers.response.SlashCommandResponse; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Interface for checking if a slash command context is valid to execute the actual command's code. | ||
*/ | ||
public interface SlashCommandChecker { | ||
|
||
@NotNull | ||
SlashCommandResponse check(@NotNull SimpleCommand command, | ||
@NotNull SlashCommandInteractionEvent context); | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...ava/com/greazi/discordbotfoundation/command/checkers/response/DefaultCommandProvider.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,30 @@ | ||
package com.greazi.discordbotfoundation.command.checkers.response; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Default {@link SlashCommandResponseProvider command provider} implementation, | ||
* using {@link com.greazi.discordbotfoundation.command.checkers.response.EmbedResponse embed response} as default responses. | ||
*/ | ||
public class DefaultCommandProvider implements SlashCommandResponseProvider { | ||
|
||
@Override | ||
public SlashCommandResponse success(@Nullable String message) { | ||
return EmbedResponse.success("Success", message != null ? message : "The command was executed successfully."); | ||
} | ||
|
||
@Override | ||
public SlashCommandResponse error(@Nullable String message) { | ||
return EmbedResponse.error("Error!", message != null ? message : "An error occurred while executing the command."); | ||
} | ||
|
||
@Override | ||
public SlashCommandResponse warning(@Nullable String message) { | ||
return EmbedResponse.warning("Warning!", message != null ? message : "A warning occurred while executing the command."); | ||
} | ||
|
||
@Override | ||
public SlashCommandResponse info(@Nullable String message) { | ||
return EmbedResponse.info("Information", message != null ? message : "An info occurred while executing the command."); | ||
} | ||
} |
Oops, something went wrong.