Skip to content

Commit

Permalink
feat(teams): implemented auto model decoding for guilds & all children
Browse files Browse the repository at this point in the history
  • Loading branch information
seailz committed Sep 13, 2023
1 parent 2f275b1 commit 8c89623
Show file tree
Hide file tree
Showing 31 changed files with 524 additions and 996 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -771,11 +771,11 @@ public Guild getGuildById(String id) {
public Sticker getStickerById(String id) {
Checker.isSnowflake(id, "Given id is not a snowflake");
try {
return Sticker.decompile(new DiscordRequest(
return (Sticker) ModelDecoder.decodeObject(new DiscordRequest(
new JSONObject(), new HashMap<>(),
URLS.GET.STICKER.GET_STICKER.replace("{sticker.id}", id),
this, URLS.GET.STICKER.GET_STICKER, RequestMethod.GET
).invoke().body(), this);
).invoke().body(), Sticker.class, this);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.utils.Checker;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import org.json.JSONObject;
Expand Down Expand Up @@ -112,7 +113,7 @@ public List<Guild> run() {
}

List<Guild> returnGuilds = new ArrayList<>();
response.arr().forEach(guild -> returnGuilds.add(Guild.decompile((JSONObject) guild, discordJar)));
response.arr().forEach(guild -> returnGuilds.add((Guild) ModelDecoder.decodeObject((JSONObject) guild, Guild.class, discordJar)));

returnGuilds.forEach(g -> discordJar.getGuildCache().cache(g));
return returnGuilds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.emoji.sticker.Sticker;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;
Expand Down Expand Up @@ -54,7 +55,7 @@ public CompletableFuture<Sticker> run() {
CompletableFuture<Sticker> stickerCompletableFuture = new CompletableFuture<>();
stickerCompletableFuture.completeAsync(() -> {
try {
return Sticker.decompile(
return (Sticker) ModelDecoder.decodeObject(
new DiscordRequest(
new JSONObject()
.put("name", name != null ? name : JSONObject.NULL)
Expand All @@ -69,6 +70,7 @@ public CompletableFuture<Sticker> run() {
URLS.PATCH.GUILD.STICKER.MODIFY_GUILD_STICKER,
RequestMethod.PATCH
).invoke().body(),
Sticker.class,
discordJar
);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
Expand Down
34 changes: 21 additions & 13 deletions src/main/java/com/seailz/discordjar/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.model.guild.Member;
import com.seailz.discordjar.utils.model.Model;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -171,26 +173,32 @@ public T getById(String id) throws DiscordRequest.UnhandledDiscordAPIErrorExcept
try {
decompile = clazz.getMethod("decompile", JSONObject.class, DiscordJar.class, String.class, Guild.class);
} catch (NoSuchMethodException exx) {
Logger.getLogger("DiscordJar").severe("Was unable to return object from cache, please report this to discord.jar's github!");
throw new RuntimeException(exx);
decompile = null;
}
}
}

try {
if (decompile == null) {
if (response == null) return null;
returnObject.set(decompile.invoke(null, response.body(), discordJar));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e) {
returnObject.set(ModelDecoder.decodeObject(
response.body(), (Class<? extends Model>) clazz, discordJar
));
} else {
try {
returnObject.set(decompile.invoke(null, response.body()));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
if (response == null) return null;
returnObject.set(decompile.invoke(null, response.body(), discordJar));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e) {
try {
if (guild != null) {
returnObject.set(decompile.invoke(null, response.body(), discordJar, guild.id(), guild));
} else throw new IllegalArgumentException(ex);
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e1) {
Logger.getLogger("DiscordJar").severe("Was unable to return object from cache, please report this to discord.jar's github!");
return null;
returnObject.set(decompile.invoke(null, response.body()));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
try {
if (guild != null) {
returnObject.set(decompile.invoke(null, response.body(), discordJar, guild.id(), guild));
} else throw new IllegalArgumentException(ex);
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e1) {
Logger.getLogger("DiscordJar").severe("Was unable to return object from cache, please report this to discord.jar's github!");
return null;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/seailz/discordjar/cache/JsonCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface JsonCache {
*
* @param interval The interval on which to invalidate the cache.
*/
default void reset(int interval) {
default JsonCache reset(int interval) {
new Thread(() -> {
while (true) {
try {
Expand All @@ -73,6 +73,7 @@ default void reset(int interval) {
invalidate();
}
}, "djar--CacheInvalidate" + new Random().nextInt(999)).start();
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.utils.model.ModelDecoder;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand All @@ -21,6 +22,6 @@ public GuildCreateEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObj

@NotNull
public Guild getGuild() {
return Guild.decompile(getJson().getJSONObject("d"), getBot());
return (Guild) ModelDecoder.decodeObject(getJson().getJSONObject("d"), Guild.class, getBot());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.utils.model.ModelDecoder;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand All @@ -13,6 +14,6 @@ public GuildUpdateEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObj

@NotNull
public Guild getGuild() {
return Guild.decompile(getJson().getJSONObject("d"), getBot());
return (Guild) ModelDecoder.decodeObject(getJson().getJSONObject("d"), Guild.class, getBot());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.seailz.discordjar.model.interaction.callback.InteractionCallbackType;
import com.seailz.discordjar.utils.TriFunction;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.voice.model.VoiceServerUpdate;
import com.seailz.discordjar.voice.model.VoiceState;
Expand Down Expand Up @@ -115,7 +116,7 @@ public enum DispatchedEvents {
if (p.getJSONObject("d").has("unavailable") && p.getJSONObject("d").getBoolean("unavailable"))
// Guild is unavailable, don't cache it
return GuildCreateEvent.class;
Guild guild = Guild.decompile(p.getJSONObject("d"), g);
Guild guild = (Guild) ModelDecoder.decodeObject(p.getJSONObject("d"), Guild.class, g);
g.getGuildCache().cache(guild);

JSONArray arr = p.getJSONObject("d").getJSONArray("channels");
Expand Down Expand Up @@ -152,14 +153,14 @@ public enum DispatchedEvents {
}),
GUILD_UPDATE((p, g, d) -> {
// modify cached guild, if it exists
Guild guild = Guild.decompile(p.getJSONObject("d"), d);
Guild guild = (Guild) ModelDecoder.decodeObject(p.getJSONObject("d"), Guild.class, d);
d.getGuildCache().cache(guild);

return GuildUpdateEvent.class;
}),
GUILD_DELETE((p, g, d) -> {
// remove cached guild, if it exists
Guild guild = Guild.decompile(p.getJSONObject("d"), d);
Guild guild = (Guild) ModelDecoder.decodeObject(p.getJSONObject("d"), Guild.class, d);
d.getGuildCache().remove(guild);

return GuildDeleteEvent.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,6 @@ public Application(String id, String name, String iconHash, String description,

public DiscordJar discordJar() { return discordJar; }

@Override
public HashMap<String, Function<JSONObject, ?>> customDecoders() {
return new HashMap<>(){{
put("guild", o -> Guild.decompile(o, discordJar));
}};
}

private Application() {}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.seailz.discordjar.model.component.button.Button;
import com.seailz.discordjar.model.component.button.ButtonStyle;
import com.seailz.discordjar.model.emoji.Emoji;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.registry.components.ButtonRegistry;
import org.json.JSONObject;

Expand Down Expand Up @@ -126,7 +127,7 @@ public static Button decompile(JSONObject obj, DiscordJar discordJar) {
if (obj.has("custom_id")) button.setCustomId(obj.getString("custom_id"));
if (obj.has("url")) button.setUrl(obj.getString("url"));
if (obj.has("disabled")) button.setDisabled(obj.getBoolean("disabled"));
if (obj.has("emoji")) button.setEmoji(Emoji.decompile(obj.getJSONObject("emoji"), discordJar));
if (obj.has("emoji")) button.setEmoji((Emoji) ModelDecoder.decodeObject(obj.getJSONObject("emoji"), Emoji.class, discordJar));
button.setRaw(obj);
return button;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.core.Compilerable;
import com.seailz.discordjar.model.emoji.Emoji;
import com.seailz.discordjar.utils.model.ModelDecoder;
import org.json.JSONObject;

/**
Expand Down Expand Up @@ -100,7 +101,7 @@ public static SelectOption decompile(JSONObject obj, DiscordJar discordJar) {
option.setDescription(obj.getString("description"));
}
if (obj.has("emoji")) {
option.setEmoji(Emoji.decompile(obj.getJSONObject("emoji"), discordJar));
option.setEmoji((Emoji) ModelDecoder.decodeObject(obj.getJSONObject("emoji"), Emoji.class, discordJar));
}
if (obj.has("default")) {
option.setDefaultSelected(obj.getBoolean("default"));
Expand Down
Loading

0 comments on commit 8c89623

Please sign in to comment.