Skip to content

Commit

Permalink
WIP web command bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jun 22, 2024
1 parent 9e18d68 commit 3a3cafa
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface CommandCallable {

List<String> aliases();

JsonObject toJson();
JsonObject toJson(PermissionHandler permHandler);

default void validatePermissions(ValueStore store, PermissionHandler permisser) throws IllegalArgumentException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public CommandGroup(CommandCallable parent, String[] aliases, ValueStore store,
this.aliases = Arrays.asList(aliases);
}

public JsonObject toJson() {
@Override
public JsonObject toJson(PermissionHandler permHandler) {
JsonObject root = new JsonObject();
for (Map.Entry<String, CommandCallable> entry : subcommands.entrySet()) {
root.add(entry.getKey(), entry.getValue().toJson());
root.add(entry.getKey(), entry.getValue().toJson(permHandler));
}
return root;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ public Object apply(ParameterData param, Object o) {
}

@Override
public JsonObject toJson() {
public JsonObject toJson(PermissionHandler permHandler) {
JsonObject command = new JsonObject();
//simpleHelp
//simpleDesc
Expand Down Expand Up @@ -930,6 +930,7 @@ public JsonObject toJson() {
JsonObject annotationsObj = new JsonObject();
for (Annotation annotation : annotations) {
if (annotation instanceof Command) continue;
if (permHandler != null && permHandler.isPermission(annotation)) continue;
JsonObject annJson = new JsonObject();
for (Method method : annotation.annotationType().getDeclaredMethods()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public class CommandManager2 {
private final PlaceholdersMap placeholders;
private PWGPTHandler pwgptHandler;

public JsonObject toJson(ValueStore htmlOptionsStore) {
JsonObject cmdJson = commands.toJson();
public JsonObject toJson(ValueStore htmlOptionsStore, PermissionHandler permHandler) {
JsonObject cmdJson = commands.toJson(permHandler);

Map<String, JsonObject> keysData = new LinkedHashMap<>();
Set<String> checkedOptions = new HashSet<>();
Expand Down Expand Up @@ -120,7 +120,7 @@ public JsonObject toJson(ValueStore htmlOptionsStore) {
JsonObject phJson = new JsonObject();
for (Class t : placeholders.getTypes()) {
Placeholders ph = placeholders.get(t);
phJson.add(t.getSimpleName(), ph.getCommands().toJson());
phJson.add(t.getSimpleName(), ph.getCommands().toJson(permHandler));
}

Gson gson = new Gson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public void validate(ValueStore store, Annotation annotation) {
}
}
}

public boolean isPermission(Annotation annotation) {
return Key.of(boolean.class, annotation) != null;
}
}
2 changes: 0 additions & 2 deletions src/main/java/link/locutus/discord/util/StringMan.java
Original file line number Diff line number Diff line change
Expand Up @@ -853,14 +853,12 @@ public static <T> List<Map.Entry<String, String>> autocompleteComma(String inpu
for (int i = 0; i < len; i++) {
String arg = split[i];
T parsed = parse.apply(arg);
// Dont modify the original list
if (options == optionsOriginal) options = new ArrayList<>(options);
options.remove(parsed);
prefixArgs.add(parsed);
}
String prefixEnd = prefixArgs.isEmpty() ? "" : ",";
String prefixKey = prefixArgs.stream().map(keyFunc).collect(Collectors.joining(",")) + prefixEnd;
String prefixValue = prefixArgs.stream().map(valueFunc).collect(Collectors.joining(",")) + prefixEnd;

options = getClosest(trailing, options, true);
if (options.size() > maxResults) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ private void handleOfficerDelete(DBNation previous, DBNation current) {
if (position == null || !position.hasAnyAdminPermission()) return;
}
DBAlliance alliance = previous.getAlliance(false);
if (alliance.getRank() < 50) {
if (alliance != null && alliance.getRank() < 50) {
String title = previous.getNation() + " (" + Rank.byId(previous.getPosition()) + ") deleted from " + previous.getAllianceName();
String body = previous.toEmbedString();
AlertUtil.forEachChannel(f -> true, GuildKey.ORBIS_OFFICER_LEAVE_ALERTS, new BiConsumer<MessageChannel, GuildDB>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import link.locutus.discord.db.ReportManager;
import link.locutus.discord.db.conflict.Conflict;
import link.locutus.discord.db.entities.*;
import link.locutus.discord.db.entities.grant.AGrantTemplate;
import link.locutus.discord.db.entities.grant.GrantTemplateManager;
import link.locutus.discord.db.entities.newsletter.Newsletter;
import link.locutus.discord.db.entities.newsletter.NewsletterManager;
import link.locutus.discord.db.guild.GuildKey;
import link.locutus.discord.db.guild.GuildSetting;
import link.locutus.discord.gpt.GPTUtil;
Expand Down Expand Up @@ -260,7 +264,7 @@ public WebOption getConflict() {
@Binding(types = GuildSetting.class)
public WebOption getGuildSetting() {
return new WebOption(GuildSetting.class).setRequiresGuild()
.setOptions((List<String>) Arrays.stream(GuildKey.values()).map(GuildSetting::name));
.setOptions((Arrays.stream(GuildKey.values()).map(GuildSetting::name).toList()));
}
//EmbeddingSource
@Binding(types = EmbeddingSource.class)
Expand Down Expand Up @@ -411,7 +415,7 @@ public WebOption getTreaty() {
data.add(Map.of(
"key", treaty.getId() + "",
"text", treaty.toLineString(),
"subtext", treaty.getTurnsRemaining() + " turns",
"subtext", treaty.getTurnsRemaining() + ""
));
}
return data;
Expand All @@ -434,23 +438,90 @@ public WebOption getDBBan() {
return data;
});
}

//DBTreasure
// @Binding(types = DBTreasure.class)
//ParametricCallable -> CommandCallable
//ICommand -> CommandCallable
@Binding(types = DBTreasure.class)
public WebOption getDBTreasure() {
List<String> names = Locutus.imp().getNationDB().getTreasuresByName().keySet().stream().toList();
return new WebOption(DBTreasure.class).setOptions(names);
}
//AGrantTemplate
@Binding(types = AGrantTemplate.class)
public WebOption getAGrantTemplate() {
return new WebOption(AGrantTemplate.class).setRequiresGuild().setQueryMap((db, user, nation) -> {
List<Map<String, String>> data = new ArrayList<>();
GrantTemplateManager manager = db.getGrantTemplateManager();
for (AGrantTemplate template : manager.getTemplates()) {
data.add(Map.of(
"key", template.getName() + "",
"text", template.getType().name() + ":" + template.getName()
));
}
return data;
});
}
//Newsletter

@Binding(types = Newsletter.class)
public WebOption getNewsletter() {
return new WebOption(Newsletter.class).setRequiresGuild().setQueryMap((db, user, nation) -> {
List<Map<String, String>> data = new ArrayList<>();
NewsletterManager manager = db.getNewsletterManager();
if (manager != null) {
for (Map.Entry<Integer, Newsletter> entry : manager.getNewsletters().entrySet()) {
Newsletter newsletter = entry.getValue();
data.add(Map.of(
"key", entry.getKey() + "",
"text", newsletter.getName()
));
}
}
return data;
});
}
//DBNation
@Binding(types = DBNation.class)
public WebOption getDBNation() {
return new WebOption(DBNation.class).setQueryMap((db, user, nation) -> {
List<Map<String, String>> data = new ArrayList<>();
for (DBNation n : Locutus.imp().getNationDB().getNations().values()) {
data.add(Map.of(
"key", n.getId() + "",
"text", n.getName()
));
}
return data;
});
}
//DBAlliance - prefix with AA:<id>
@Binding(types = DBAlliance.class)
public WebOption getDBAlliance() {
return new WebOption(DBAlliance.class).setQueryMap((db, user, nation) -> {
List<Map<String, String>> data = new ArrayList<>();
for (DBAlliance aa : Locutus.imp().getNationDB().getAlliances()) {
data.add(Map.of(
"key", "AA:" + aa.getId(),
"text", aa.getName()
));
}
return data;
});
}
//GuildDB
//Newsletter
//AGrantTemplate
@Binding(types = GuildDB.class)
public WebOption getGuildDB() {
return new WebOption(GuildDB.class).setRequiresUser().setQueryMap((db, user, nation) -> {
List<Map<String, String>> data = new ArrayList<>();
for (Guild guild : user.getMutualGuilds()) {
data.add(Map.of(
"key", guild.getId(),
"text", guild.getName()
));
}
return data;
});
}
// AllianceDepositLimit
// NationDepositLimit
// WikiCategory

//unused
//Class
// IAttack
Expand Down

0 comments on commit 3a3cafa

Please sign in to comment.