Skip to content

Commit bc97849

Browse files
author
Kaleidox
committed
fix lifecycle order
1 parent 2f0afd6 commit bc97849

6 files changed

Lines changed: 58 additions & 18 deletions

File tree

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
package org.comroid.api.io;
22

33
import lombok.SneakyThrows;
4+
import lombok.Value;
45

56
import java.io.File;
7+
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
10+
@Value
11+
public class FileFlag {
12+
private static final Map<File, FileFlag> cache = new ConcurrentHashMap<>();
613

7-
public final class FileFlag {
814
@SneakyThrows
915
public static boolean enable(File file) {
16+
return cache.containsKey(file) && cache.get(file).enable();
17+
}
18+
19+
public static boolean consume(File file) {
20+
return cache.containsKey(file) && cache.get(file).consume();
21+
}
22+
23+
File file;
24+
25+
@SneakyThrows
26+
public boolean enable() {
1027
if (!file.isAbsolute()) return enable(file.getAbsoluteFile());
1128
return file.exists() || file.createNewFile();
1229
}
1330

14-
public static boolean consume(File file) {
31+
public boolean consume() {
1532
if (!file.isAbsolute()) return consume(file.getAbsoluteFile());
1633
return file.exists() && file.delete();
1734
}
18-
19-
private FileFlag() {
20-
throw new UnsupportedOperationException();
21-
}
2235
}

src/main/java/org/comroid/api/tree/Component.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.comroid.api.func.ext.Wrap;
2424
import org.comroid.api.func.util.Bitmask;
2525
import org.comroid.api.func.util.Cache;
26+
import org.comroid.api.func.util.Streams;
2627
import org.comroid.api.info.Constraint;
2728
import org.comroid.api.info.Log;
2829
import org.comroid.api.info.Maintenance;
@@ -104,13 +105,13 @@ default String getFullName() {
104105
State getCurrentState();
105106

106107
@Override
107-
default <T extends Component> Stream<T> components(@Nullable Class<? super T> type) {
108+
default <T> Stream<T> components(@Nullable Class<? super T> type) {
108109
return Stream.<T>concat(streamChildren(type),
109110
isSubComponent() ? Stream.of(getParent()).filter(Objects::nonNull).flatMap(comp -> comp.components(type)) : empty()).distinct();
110111
}
111112

112113
@Override
113-
default <T extends Component> Wrap<T> component(@Nullable Class<? super T> type) {
114+
default <T> Wrap<T> component(@Nullable Class<? super T> type) {
114115
return () -> uncheckedCast(components(type).findAny().orElse(null));
115116
}
116117

@@ -360,15 +361,19 @@ private void cleanupChildren() {
360361
}
361362

362363
private void injectDependencies() {
363-
dependencies().stream().filter(dep -> dep.prop != null && dep.prop.canSet()).<Map.Entry<Dependency, Component>>flatMap(dep -> {
364+
dependencies().stream().filter(dep -> dep.prop != null && dep.prop.canSet()).<Map.Entry<Dependency, Object>>flatMap(dep -> {
364365
var results = components(dep.type).toList();
365366
if (results.size() > 1) {
366367
final var names = dep.name.isEmpty() ? Stream.concat(Stream.of(dep.prop.getName()), dep.prop.getAliases().stream())
367368
.collect(Collectors.toSet()) : Set.of(dep.name);
368-
var byName = results.stream().filter(it -> names.stream().anyMatch(alias -> equalsIgnoreCase(alias, it.getName()))).toList();
369+
var byName = results.stream()
370+
.flatMap(Streams.cast(Named.class))
371+
.filter(it -> names.stream().anyMatch(alias -> equalsIgnoreCase(alias, it.getName())))
372+
.toList();
369373
if (byName.isEmpty()) {
370374
Log.at(Level.WARNING, "Exact name match yielded no results; attempting to find with contains()");
371375
byName = results.stream()
376+
.flatMap(Streams.cast(Named.class))
372377
.filter(it -> names.stream().map(String::toLowerCase).anyMatch(alias -> it.getName().toLowerCase().contains(alias)))
373378
.toList();
374379
}
@@ -419,7 +424,7 @@ public void stop() {
419424
}
420425

421426
private static <T> boolean test(T it, State state) {
422-
return it instanceof Component && ((Component) it).testState(state);
427+
return !(it instanceof Component) || ((Component) it).testState(state);
423428
}
424429
}
425430

src/main/java/org/comroid/api/tree/ComponentContextSupplier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.stream.Stream;
77

88
public interface ComponentContextSupplier {
9-
<T extends Component> Stream<T> components(@Nullable Class<? super T> type);
9+
<T> Stream<T> components(@Nullable Class<? super T> type);
1010

11-
<T extends Component> Wrap<T> component(@Nullable Class<? super T> type);
11+
<T> Wrap<T> component(@Nullable Class<? super T> type);
1212
}

src/main/java/org/comroid/api/tree/Container.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ static Container of(Object... children) {
4848
void clearChildren();
4949

5050
default <T> Stream<T> streamChildren(@Nullable Class<? super T> type) {
51+
return streamChildren(type, true);
52+
}
53+
54+
default <T> Stream<T> streamChildren(@Nullable Class<? super T> type, boolean recurse) {
5155
return Stream.concat(getChildren().stream(), streamOwnChildren())
52-
.collect(Streams.expandRecursive(it -> Stream.of(it).flatMap(Streams.cast(Container.class)).flatMap(c -> c.streamChildren(type))))
56+
.collect(Streams.expandRecursive(it -> recurse
57+
? Stream.of(it).flatMap(Streams.cast(Container.class)).flatMap(c -> c.streamChildren(type))
58+
: Stream.empty()))
5359
.distinct()
5460
.flatMap(Streams.cast(type))
5561
.sorted(Comparator.comparing(Object::getClass, Order.COMPARATOR))
@@ -116,13 +122,14 @@ public void start() {
116122
@SafeVarargs
117123
@SneakyThrows
118124
protected final <T> void runOnChildren(Class<T> type, ThrowingConsumer<T, Throwable> task, Predicate<T> test, T... extra) {
119-
final List<Throwable> errors = streamChildren(type).collect(append(moreMembers().flatMap(cast(type))))
125+
final List<Throwable> errors = streamChildren(type, false).collect(append(moreMembers().flatMap(cast(type))))
120126
.collect(append(extra))
121127
.filter(Objects::nonNull)
122128
.filter(Predicate.not(this::equals))
123129
// extra enabled check for EnabledState objects
124130
.filter(it -> !(it instanceof EnabledState) || ((EnabledState) it).isEnabled())
125131
.filter(test)
132+
.distinct()
126133
.flatMap(it -> {
127134
try {
128135
task.accept(it);

src/main/java/org/comroid/interaction/adapter/jda/DiscordCommandRegistrator.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,21 @@ public void initialize() {
8484
}
8585
}
8686

87-
log.info("Upserting %d interactions to discord bot %s".formatted(all.size(), adp.getJda().getSelfUser()));
87+
var jda = adp.getJda();
88+
log.info("Upserting %d interactions to discord bot %s".formatted(all.size(), jda.getSelfUser()));
89+
90+
RestAction<?> action = PURGE_COMMANDS.consume() ? jda.retrieveCommands().flatMap(cmds -> {
91+
log.fine("Puring %d previously defined commands".formatted(cmds.size()));
92+
93+
RestAction<?> sub = new CompletedRestAction<>(jda, (Object) null);
94+
for (var cmd : cmds)
95+
sub = sub.flatMap($ -> jda.deleteCommandById(cmd.getId()));
96+
97+
return sub;
98+
}) : new CompletedRestAction<>(jda, (Object) null);
99+
100+
for (var data : all) action = action.flatMap($ -> jda.upsertCommand(data));
88101

89-
RestAction<?> action = new CompletedRestAction<>(adp.getJda(), (Object) null);
90-
for (var data : all) action = action.flatMap($ -> adp.getJda().upsertCommand(data));
91102
action.queue();
92103
}
93104

src/main/java/org/comroid/interaction/adapter/jda/JdaAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.comroid.api.attr.IntegerAttribute;
1919
import org.comroid.api.data.seri.DataNode;
2020
import org.comroid.api.func.util.Streams;
21+
import org.comroid.api.io.FileFlag;
2122
import org.comroid.api.java.Activator;
2223
import org.comroid.api.tree.Component;
2324
import org.comroid.interaction.InteractionCore;
@@ -28,6 +29,7 @@
2829
import org.comroid.interaction.node.ParameterNode;
2930
import org.jspecify.annotations.NonNull;
3031

32+
import java.io.File;
3133
import java.util.Arrays;
3234
import java.util.Map;
3335
import java.util.Objects;
@@ -37,6 +39,8 @@
3739
@Value
3840
@NonFinal
3941
public class JdaAdapter extends Component.Base implements EventListener {
42+
public static final FileFlag PURGE_COMMANDS = new FileFlag(new File("./purge_commands"));
43+
4044
public static final String KEY_CONTEXT = "context.discord";
4145
public static final String CONTEXT_COMMAND = "command";
4246
public static final String CONTEXT_MESSAGE = "message";

0 commit comments

Comments
 (0)