Skip to content

Instantly share code, notes, and snippets.

@natanbc
Created October 10, 2018 15:00
Show Gist options
  • Save natanbc/bfe03119f2f0b4750d7a5b0f934c94e6 to your computer and use it in GitHub Desktop.
Save natanbc/bfe03119f2f0b4750d7a5b0f934c94e6 to your computer and use it in GitHub Desktop.
import com.mewna.catnip.Catnip;
import com.mewna.catnip.shard.DiscordEvent;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonObject;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
public class DiscordDB {
private final Map<String, String> tableMap = new HashMap<>();
private final Map<String, String> revTableMap = new HashMap<>();
private final Catnip catnip;
public DiscordDB(@Nonnull final Catnip catnip, @Nonnull final String dbGuildId) {
this.catnip = catnip;
catnip.rest().guild().getGuildChannels(dbGuildId).thenAccept(channels -> {
channels.forEach(c -> {
if(c.isText()) {
tableMap.put(c.asTextChannel().name(), c.id());
revTableMap.put(c.id(), c.asTextChannel().name());
}
});
});
catnip.on(DiscordEvent.CHANNEL_CREATE, channel -> {
if(!channel.isText() || !channel.asTextChannel().guildId().equals(dbGuildId)) {
return;
}
tableMap.put(channel.asTextChannel().name(), channel.id());
revTableMap.put(channel.id(), channel.asTextChannel().name());
});
catnip.on(DiscordEvent.CHANNEL_DELETE, channel -> {
if(!channel.isText() || !channel.asTextChannel().guildId().equals(dbGuildId)) {
return;
}
tableMap.remove(revTableMap.remove(channel.id()));
});
catnip.on(DiscordEvent.CHANNEL_UPDATE, channel -> {
if(!channel.isText() || !channel.asTextChannel().guildId().equals(dbGuildId)) {
return;
}
final String name = channel.asTextChannel().name();
final String id = channel.id();
tableMap.remove(revTableMap.put(id, name));
tableMap.put(name, id);
});
}
@Nonnull
@CheckReturnValue
public CompletionStage<JsonObject> fetch(@Nonnull final String table, @Nonnull final String key) {
final CompletableFuture<JsonObject> f = new CompletableFuture<>();
fetch0(table, key, f).thenRun(()->{
if(!f.isDone()) {
f.completeExceptionally(new NoSuchElementException());
}
});
return f;
}
@Nonnull
@CheckReturnValue
public CompletionStage<JsonObject> fetchOrDefault(@Nonnull final String table, @Nonnull final String key,
@Nonnull final JsonObject defaultValue) {
final CompletableFuture<JsonObject> f = new CompletableFuture<>();
fetch0(table, key, f).thenRun(()->{
if(!f.isDone()) {
f.complete(defaultValue);
}
});
return f;
}
@Nonnull
@CheckReturnValue
public CompletionStage<Void> write(@Nonnull final String table, @Nonnull final String key,
@Nonnull final JsonObject data) {
final String id = tableMap.get(table);
if(id == null) {
throw new IllegalArgumentException("No table named " + table);
}
return catnip.rest().channel().sendMessage(id, new JsonObject()
.put("key", key)
.put("data", data)
.encode()
).thenRun(() -> {});
}
@Nonnull
@CheckReturnValue
private CompletionStage<Void> fetch0(@Nonnull final String table, @Nonnull final String key,
@Nonnull final CompletableFuture<JsonObject> dest) {
final String id = tableMap.get(table);
if(id == null) {
throw new IllegalArgumentException("No table named " + table);
}
return catnip.rest().channel().getChannelMessages(id).fetchWhile(message -> {
try {
final JsonObject object = new JsonObject(message.content());
if(key.equals(object.getString("key"))) {
dest.complete(object.getJsonObject("data"));
return false;
}
} catch(final DecodeException ignored) {}
return true;
}).exceptionally(e -> {
dest.completeExceptionally(e);
return null;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment