mirror of
https://github.com/aikar/commands.git
synced 2026-06-08 17:47:35 +00:00
Implement CommandPermissionResolver (#135)
This pull request is an initial attempt at implementing permission for commands for the JDA module.
It includes one added file, **JDACommandPermissionResolver** which implements CommandPermissionResolver.
I created a map to store the Discord permission offsets stored mapped by their kebab-cased name (i.e. "ADMINISTRATOR" becomes "administrator", "MANAGE_SERVER" becomes "manage-server".
The implementation of `hasPermission(JDACommandEvent, String)` is fairly simple:
- Get the issuer's Member object
- Check if it's null; if so, return false.
- Get the permission offset from the aforementioned map
- Check if it's null; if so, return false.
- Return whether or not the member has the permission.
---------------------------------------
This PR allows new commands to use the `@CommandPermission` annotation like so:
```java
@CommandAlias("test|t")
public class Command extends BaseCommand {
@Subcommand("admin|a")
@CommandPermission("manage-server")
public void adminCommand(MessageReceivedEvent event) {
//...
}
}
```
This commit is contained in:
committed by
Daniel Ennis
parent
7478d030d0
commit
fbfb8decd0
@@ -2,5 +2,5 @@ package co.aikar.commands;
|
||||
|
||||
|
||||
public interface CommandPermissionResolver {
|
||||
boolean hasPermission(JDACommandEvent event, String permission);
|
||||
boolean hasPermission(JDACommandManager manager, JDACommandEvent event, String permission);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class JDACommandEvent implements CommandIssuer {
|
||||
@Override
|
||||
public boolean hasPermission(String permission) {
|
||||
CommandPermissionResolver permissionResolver = this.manager.getPermissionResolver();
|
||||
return permissionResolver == null || permissionResolver.hasPermission(this, permission);
|
||||
return permissionResolver == null || permissionResolver.hasPermission(manager, this, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -91,7 +91,7 @@ public class JDACommandManager extends CommandManager<
|
||||
}
|
||||
}
|
||||
|
||||
private long getBotOwnerId() {
|
||||
public long getBotOwnerId() {
|
||||
// Just in case initialization on ReadyEvent fails.
|
||||
initializeBotOwner();
|
||||
return botOwner;
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package co.aikar.commands;
|
||||
|
||||
import net.dv8tion.jda.core.Permission;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JDACommandPermissionResolver implements CommandPermissionResolver {
|
||||
private Map<String, Integer> discordPermissionOffsets;
|
||||
|
||||
public JDACommandPermissionResolver() {
|
||||
discordPermissionOffsets = new HashMap<>();
|
||||
for (Permission permission : Permission.values()) {
|
||||
discordPermissionOffsets.put(permission.name().toLowerCase().replaceAll("_", "-"), permission.getOffset());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(JDACommandManager manager, JDACommandEvent event, String permission) {
|
||||
// Explicitly return true if the issuer is the bot's owner. They are always allowed.
|
||||
if (manager.getBotOwnerId() == event.getIssuer().getAuthor().getIdLong()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Integer permissionOffset = discordPermissionOffsets.get(permission);
|
||||
if (permissionOffset == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return event.getIssuer().getMember().hasPermission(
|
||||
Permission.getFromOffset(permissionOffset)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class JDAOptions {
|
||||
CommandConfig defaultConfig = new JDACommandConfig();
|
||||
CommandConfigProvider configProvider = null;
|
||||
CommandPermissionResolver permissionResolver = null;
|
||||
CommandPermissionResolver permissionResolver = new JDACommandPermissionResolver();
|
||||
|
||||
public JDAOptions() {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user