Skip to content

Commit 9624828

Browse files
committed
Use SnowflakeValues
1 parent f8aa901 commit 9624828

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

src/commander/java/com/mcmoddev/mmdbot/commander/util/script/ScriptingContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import org.graalvm.polyglot.proxy.ProxyExecutable;
2727
import org.graalvm.polyglot.proxy.ProxyInstantiable;
2828
import org.graalvm.polyglot.proxy.ProxyObject;
29+
import org.graalvm.polyglot.proxy.ProxyTime;
2930

31+
import java.time.LocalTime;
32+
import java.time.temporal.Temporal;
3033
import java.util.ArrayList;
3134
import java.util.Arrays;
3235
import java.util.LinkedHashMap;
@@ -82,6 +85,8 @@ public ScriptingContext set(String key, Object value) {
8285
}
8386
}
8487
map.put(key, objects);
88+
} else if (value instanceof LocalTime localTime) {
89+
map.put(key, ProxyTime.from(localTime));
8590
} else {
8691
map.put(key, value);
8792
}

src/commander/java/com/mcmoddev/mmdbot/commander/util/script/ScriptingUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ public static ScriptingContext createEmote(Emote emote) {
397397
.setFunction("getGuild", args -> emote.getGuild() == null ? null : createGuild(emote.getGuild()));
398398
}
399399

400+
@SuppressWarnings("rawtypes")
400401
public static ProxyExecutable functionObject(Function<List<Value>, Object> function) {
401402
return new ScriptingContext.NameableProxyExecutable() {
402403

src/watcher/java/com/mcmoddev/mmdbot/watcher/TheWatcher.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.spongepowered.configurate.ConfigurateException;
7575
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
7676
import org.spongepowered.configurate.reference.ConfigurationReference;
77+
import org.spongepowered.configurate.serialize.TypeSerializerCollection;
7778
import org.sqlite.SQLiteDataSource;
7879

7980
import javax.security.auth.login.LoginException;
@@ -88,7 +89,13 @@
8889
import java.util.concurrent.TimeUnit;
8990
import java.util.function.Consumer;
9091

92+
@SuppressWarnings("unused")
9193
public final class TheWatcher implements Bot {
94+
static final TypeSerializerCollection ADDED_SERIALIZERS = TypeSerializerCollection.defaults()
95+
.childBuilder()
96+
.register(Punishment.class, new Punishment.Serializer())
97+
.register(SnowflakeValue.class, new SnowflakeValue.Serializer())
98+
.build();
9299

93100
public static final Logger LOGGER = LoggerFactory.getLogger("TheWatcher");
94101

@@ -199,7 +206,7 @@ public void start() {
199206
.emitComments(true)
200207
.prettyPrinting(true)
201208
.path(configPath)
202-
.defaultOptions(opts -> opts.serializers(build -> build.register(Punishment.class, new Punishment.Serializer())))
209+
.defaultOptions(ops -> ops.serializers(ADDED_SERIALIZERS))
203210
.build();
204211
Objects.requireNonNull(loader.defaultOptions().serializers().get(Punishment.class));
205212
final var cPair =
@@ -250,8 +257,11 @@ public void start() {
250257
final var coOwners = config.bot().getOwners().subList(1, config.bot().getOwners().size());
251258

252259
commandClient = new CommandClientBuilder()
253-
.setOwnerId(config.bot().getOwners().get(0))
254-
.setCoOwnerIds(coOwners.toArray(String[]::new))
260+
.setOwnerId(config.bot().getOwners().get(0).asString())
261+
.setCoOwnerIds(coOwners
262+
.stream()
263+
.map(SnowflakeValue::asString)
264+
.toArray(String[]::new))
255265
.setPrefixes(config.bot().getPrefixes().toArray(String[]::new))
256266
.setManualUpsert(true)
257267
.useHelpBuilder(false)
@@ -262,7 +272,7 @@ public void start() {
262272
COMMANDS_LISTENER.addListener((EventListener) commandClient);
263273

264274
final var upserter = new CommandUpserter(commandClient, config.bot().areCommandsForcedGuildOnly(),
265-
SnowflakeValue.of(config.bot().guild()));
275+
config.bot().guild());
266276
COMMANDS_LISTENER.addListener(upserter);
267277

268278
// Buttons
@@ -278,9 +288,7 @@ public void start() {
278288
.create(dotenv.get("BOT_TOKEN"), INTENTS)
279289
.addEventListeners(listenerConsumer((ReadyEvent event) -> {
280290
getLogger().warn("The Watcher is ready to work! Logged in as {}", event.getJDA().getSelfUser().getAsTag());
281-
Events.MISC_BUS.addListener(-1, (TaskScheduler.CollectTasksEvent ctEvent) -> {
282-
ctEvent.addTask(new TaskScheduler.Task(new ChannelMessageChecker(event.getJDA()), 0, 1, TimeUnit.DAYS));
283-
});
291+
Events.MISC_BUS.addListener(-1, (TaskScheduler.CollectTasksEvent ctEvent) -> ctEvent.addTask(new TaskScheduler.Task(new ChannelMessageChecker(event.getJDA()), 0, 1, TimeUnit.DAYS)));
284292
}), COMMANDS_LISTENER, MISC_LISTENER, PUNISHABLE_ACTIONS_LISTENER)
285293
.setActivity(Activity.of(oldConfig.getActivityType(), oldConfig.getActivityName()))
286294
.disableCache(CacheFlag.CLIENT_STATUS)
@@ -343,7 +351,17 @@ private static <E extends Event> EventListener listenerConsumer(final Consumer<E
343351
}
344352

345353
public static boolean isBotMaintainer(final Member member) {
346-
return member.getRoles().stream().anyMatch(r -> getInstance().getConfig().roles().getBotMaintainers().contains(r.getId()));
354+
final var maintainers = getInstance().getConfig().roles().getBotMaintainers();
355+
return member.getRoles()
356+
.stream()
357+
.anyMatch(r -> {
358+
for (final var m : maintainers) {
359+
if (m.test(r)) {
360+
return true;
361+
}
362+
}
363+
return false;
364+
});
347365
}
348366

349367
public static Jdbi database() {

src/watcher/java/com/mcmoddev/mmdbot/watcher/util/Configuration.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.mcmoddev.mmdbot.watcher.util;
2222

2323
import com.jagrosh.jdautilities.commons.utils.SafeIdUtil;
24+
import com.mcmoddev.mmdbot.core.util.config.SnowflakeValue;
2425
import com.mcmoddev.mmdbot.watcher.TheWatcher;
2526
import com.mcmoddev.mmdbot.watcher.punishments.Punishment;
2627
import net.dv8tion.jda.api.Permission;
@@ -54,18 +55,18 @@ public static final class Bot {
5455
@Required
5556
@Setting("owners")
5657
@Comment("The Snowflake IDs of the owners of the bot.")
57-
private List<String> owners = new ArrayList<>();
58+
private List<SnowflakeValue> owners = new ArrayList<>();
5859

59-
public List<String> getOwners() {
60+
public List<SnowflakeValue> getOwners() {
6061
return owners;
6162
}
6263

6364
@Required
6465
@Setting("guild")
6566
@Comment("The main guild of the bot.")
66-
private String guild = "";
67+
private SnowflakeValue guild = SnowflakeValue.EMPTY;
6768

68-
public String guild() {
69+
public SnowflakeValue guild() {
6970
return guild;
7071
}
7172

@@ -100,9 +101,9 @@ public static final class Roles {
100101
@Required
101102
@Setting("bot_maintainers")
102103
@Comment("A list of Snowflake IDs representing the roles which are bot maintainers.")
103-
private List<String> botMaintainers = new ArrayList<>();
104+
private List<SnowflakeValue> botMaintainers = new ArrayList<>();
104105

105-
public List<String> getBotMaintainers() {
106+
public List<SnowflakeValue> getBotMaintainers() {
106107
return botMaintainers;
107108
}
108109
}

0 commit comments

Comments
 (0)