Skip to content

Commit

Permalink
Allow embeds to have fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Aug 23, 2023
1 parent fac0eba commit 4102d30
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/net/neoforged/camelot/script/ScriptMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.graalvm.polyglot.Value;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* A wrapper for a {@link Value} that is treated like a map.
*
Expand Down Expand Up @@ -37,6 +41,39 @@ public Integer getInt(String key) {
return val.asInt();
}

/**
* Gets the value with the given {@code key} as a boolean.
*
* @param key the key of the value to query
* @return the value with the given key, or {@code null} if one doesn't exist
*/
@Nullable
public Boolean getBoolean(String key) {
final Value val = value.getMember(key);
if (val == null) return null;
return val.asBoolean();
}

/**
* Gets the value with the given {@code key} as a boolean.
* @param key the key of the value to query
* @return the value with the given key as a list, or an {@link List#of() empty list} if one doesn't exist
*/
public List<Value> getList(String key) {
final Value val = value.getMember(key);
if (val == null) return List.of();
if (val.hasIterator()) {
final List<Value> values = new ArrayList<>();
final var itr = val.getIterator();
while (itr.hasIteratorNextElement()) {
values.add(itr.getIteratorNextElement());
}
return values;
} else {
return List.of(val);
}
}

/**
* Converts this map into an embed.
*/
Expand All @@ -54,6 +91,12 @@ public MessageEmbed asEmbed() {
}
}

getList("fields").stream().map(ScriptMap::new).forEach(field -> builder.addField(
Objects.requireNonNull(field.getString("name")),
Objects.requireNonNull(field.getString("value")),
Objects.requireNonNullElse(field.getBoolean("inline"), false)
));

final Integer colour = getInt("color");
if (colour != null) {
builder.setColor(colour);
Expand Down

0 comments on commit 4102d30

Please sign in to comment.