Skip to content

Commit 50703cf

Browse files
committed
Added the start of permission system...
1 parent a08b65b commit 50703cf

4 files changed

Lines changed: 107 additions & 6 deletions

File tree

src/main/java/org/mangorage/mangobotcore/api/command/v1/AbstractCommand.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public String getDescription() {
3838
}
3939

4040
public abstract R getFailedResult();
41+
public abstract R getNoPermission();
42+
43+
public abstract PermissionNode<C> getPermissionNode();
4144

4245
public List<String> aliases() {
4346
return List.of();
@@ -143,7 +146,11 @@ public R execute(CommandContext<C> ctx) {
143146
result.addMessage("Not enough arguments! Required: " + requiredArgs + ", Provided: 0");
144147
return getFailedResult();
145148
}
146-
return run(ctx);
149+
if (getPermissionNode().hasPermission(ctx)) {
150+
return run(ctx);
151+
} else {
152+
return getNoPermission();
153+
}
147154
}
148155

149156
String name = ctx.peek();
@@ -161,7 +168,11 @@ public R execute(CommandContext<C> ctx) {
161168
return getFailedResult();
162169
}
163170

164-
return run(ctx);
171+
if (getPermissionNode().hasPermission(ctx)) {
172+
return run(ctx);
173+
} else {
174+
return getNoPermission();
175+
}
165176
} catch (Throwable t) {
166177
result.addMessage(t.toString());
167178
return getFailedResult();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.mangorage.mangobotcore.api.command.v1;
2+
3+
public interface PermissionNode<C> {
4+
String getId();
5+
boolean hasPermission(CommandContext<C> commandContext);
6+
}

src/main/java/org/mangorage/mangobotcore/api/jda/command/v2/AbstractJDACommand.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public final JDACommandResult getFailedResult() {
2424
return JDACommandResult.ERROR;
2525
}
2626

27+
@Override
28+
public JDACommandResult getNoPermission() {
29+
return JDACommandResult.NO_PERMISSION;
30+
}
31+
2732
@Override
2833
public JDACommandResult execute(CommandContext<Message> commandContext) {
2934
final var context = commandContext.getContextObject();
@@ -34,10 +39,6 @@ public JDACommandResult execute(CommandContext<Message> commandContext) {
3439
if (getCommandType() == JDACommandType.DM && context.isFromGuild())
3540
return JDACommandResult.DM_ONLY;
3641

37-
if (!hasPermission(context)) {
38-
return JDACommandResult.NO_PERMISSION;
39-
}
40-
4142
// Continue with normal execution
4243
return super.execute(commandContext);
4344
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.mangorage.mangobotcore.api.jda.command.v2;
2+
3+
import net.dv8tion.jda.api.Permission;
4+
import net.dv8tion.jda.api.entities.Member;
5+
import net.dv8tion.jda.api.entities.Message;
6+
import net.dv8tion.jda.api.entities.Role;
7+
import org.mangorage.mangobotcore.api.command.v1.CommandContext;
8+
import org.mangorage.mangobotcore.api.command.v1.PermissionNode;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public final class JDAPermissionNode implements PermissionNode<Message> {
14+
15+
record GuildUser(Long guildId, Long userId) {}
16+
record GuildRole(Long guildId, Long roleId) {}
17+
18+
public static JDAPermissionNode create(String id) {
19+
return new JDAPermissionNode(id);
20+
}
21+
22+
private final String id;
23+
private final List<GuildRole> roleIds = new ArrayList<>();
24+
private final List<GuildUser> userIds = new ArrayList<>();
25+
private final List<Permission> requiredDiscordPermissions = new ArrayList<>();
26+
27+
private JDAPermissionNode(String id) {
28+
this.id = id;
29+
}
30+
31+
public void authorizeGuildUser(Member member) {
32+
userIds.add(new GuildUser(member.getGuild().getIdLong(), member.getUser().getIdLong()));
33+
}
34+
35+
public void revokeGuildUser(Member member) {
36+
userIds.remove(new GuildUser(member.getGuild().getIdLong(), member.getIdLong()));
37+
}
38+
39+
public void addRequiredPermission(Permission permission) {
40+
requiredDiscordPermissions.add(permission);
41+
}
42+
43+
public void removeRequiredPermission(Permission permission) {
44+
requiredDiscordPermissions.remove(permission);
45+
}
46+
47+
public void authoriseRoleId(Long guildId, Long roleId) {
48+
roleIds.add(new GuildRole(guildId, roleId));
49+
}
50+
51+
public void revokeRoleId(Long guildId, Long roleId) {
52+
roleIds.remove(new GuildRole(guildId, roleId));
53+
}
54+
55+
@Override
56+
public String getId() {
57+
return id;
58+
}
59+
60+
@Override
61+
public boolean hasPermission(CommandContext<Message> commandContext) {
62+
final var isGuild = commandContext.getContextObject().isFromGuild();
63+
if (isGuild) {
64+
final var member = commandContext.getContextObject().getMember();
65+
if (member == null) {
66+
return false;
67+
}
68+
final var guildId = member.getGuild().getIdLong();
69+
70+
if (roleIds.isEmpty() && userIds.isEmpty() && requiredDiscordPermissions.isEmpty()) {
71+
return member.hasPermission(Permission.ADMINISTRATOR);
72+
} else {
73+
for (Role role : member.getRoles()) {
74+
if (roleIds.contains(new GuildRole(guildId, role.getIdLong())))
75+
return true;
76+
}
77+
return userIds.contains(new GuildUser(guildId, member.getIdLong()));
78+
}
79+
} else {
80+
return userIds.isEmpty() || userIds.contains(new GuildUser(null, commandContext.getContextObject().getAuthor().getIdLong()));
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)