From e0df2fe94dd580a497bdee6b4d5e935f48d3b768 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Fri, 29 Sep 2023 16:38:30 +0100 Subject: [PATCH 1/3] feat(guilds): implemented support for incident actions --- .../seailz/discordjar/model/guild/Guild.java | 40 ++++++++++- .../model/guild/incident/IncidentsData.java | 71 +++++++++++++++++++ .../com/seailz/discordjar/utils/URLS.java | 1 + 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/seailz/discordjar/model/guild/incident/IncidentsData.java diff --git a/src/main/java/com/seailz/discordjar/model/guild/Guild.java b/src/main/java/com/seailz/discordjar/model/guild/Guild.java index 4739337d..bdcdcd32 100644 --- a/src/main/java/com/seailz/discordjar/model/guild/Guild.java +++ b/src/main/java/com/seailz/discordjar/model/guild/Guild.java @@ -19,6 +19,7 @@ import com.seailz.discordjar.model.emoji.Emoji; import com.seailz.discordjar.model.emoji.sticker.Sticker; import com.seailz.discordjar.model.guild.filter.ExplicitContentFilterLevel; +import com.seailz.discordjar.model.guild.incident.IncidentsData; import com.seailz.discordjar.model.guild.mfa.MFALevel; import com.seailz.discordjar.model.guild.notification.DefaultMessageNotificationLevel; import com.seailz.discordjar.model.guild.premium.PremiumTier; @@ -98,6 +99,8 @@ public class Guild implements Compilerable, Snowflake, CDNAble { private final boolean premiumProgressBarEnabled; private final String safetyAlertChannelId; private Channel safetyAlertChannel = null; + private final IncidentsData incidentsData; + private final DiscordJar discordJar; private final JsonCache roleCache; @@ -143,7 +146,7 @@ public Guild( List stickers, boolean premiumProgressBarEnabled, String safetyAlertChannelId, - DiscordJar discordJar, + IncidentsData incidentsData, DiscordJar discordJar, JsonCache roleCache ) { this.id = id; @@ -187,6 +190,7 @@ public Guild( this.stickers = stickers; this.premiumProgressBarEnabled = premiumProgressBarEnabled; this.safetyAlertChannelId = safetyAlertChannelId; + this.incidentsData = incidentsData; this.discordJar = discordJar; this.roleCache = roleCache; } @@ -330,6 +334,9 @@ public List stickers() { public boolean premiumProgressBarEnabled() { return premiumProgressBarEnabled; } + public IncidentsData incidentsData() { + return incidentsData; + } public DiscordJar discordJar() { return discordJar; @@ -381,7 +388,8 @@ public JSONObject compile() { .put("welcome_screen", welcomeScreen) .put("stickers", stickers) .put("premium_progress_bar_enabled", premiumProgressBarEnabled) - .put("safety_alerts_channel_id", safetyAlertChannelId); + .put("safety_alerts_channel_id", safetyAlertChannelId) + .put("incidents_data", incidentsData.compile()); } @NotNull @@ -671,6 +679,8 @@ public static Guild decompile(JSONObject obj, DiscordJar discordJar) { safetyAlertsChannelId = obj.getString("safety_alerts_channel_id"); } + IncidentsData incidentsData = obj.has("incidents_data") && !obj.isNull("incidents_data") ? IncidentsData.decompile(obj.getJSONObject("incidents_data")) : null; + Guild g = new Guild( id, name, @@ -713,7 +723,7 @@ public static Guild decompile(JSONObject obj, DiscordJar discordJar) { stickers, premiumProgressBarEnabled, safetyAlertsChannelId, - discordJar, + incidentsData, discordJar, JsonCache.newc(new DiscordRequest( new JSONObject(), new HashMap<>(), @@ -1730,6 +1740,30 @@ public Response deleteScheduledEvent(String id) { return res; } + public Response modifyGuildIncidentActions(@NotNull IncidentsData incidentsData) { + Response res = new Response<>(); + new java.lang.Thread(() -> { + try { + new DiscordRequest( + incidentsData.compile(), + new HashMap<>(), + URLS.PATCH.GUILD.MODIFY_GUILD_INCIDENT_ACTIONS.replace("{guild.id}", this.id), + discordJar, + URLS.PATCH.GUILD.MODIFY_GUILD_INCIDENT_ACTIONS, + RequestMethod.PATCH + ).invoke(); + res.complete(null); + } catch (DiscordRequest.UnhandledDiscordAPIErrorException e) { + res.completeError(new Response.Error( + e.getCode(), + e.getMessage(), + e.getBody() + )); + } + }).start(); + return res; + } + public @NotNull ModifyScheduledEventAction modifyScheduledEvent(String id) { return new ModifyScheduledEventAction(discordJar, this.id, id); } diff --git a/src/main/java/com/seailz/discordjar/model/guild/incident/IncidentsData.java b/src/main/java/com/seailz/discordjar/model/guild/incident/IncidentsData.java new file mode 100644 index 00000000..48fdaac8 --- /dev/null +++ b/src/main/java/com/seailz/discordjar/model/guild/incident/IncidentsData.java @@ -0,0 +1,71 @@ +package com.seailz.discordjar.model.guild.incident; + +import com.seailz.discordjar.core.Compilerable; +import org.jetbrains.annotations.NotNull; +import org.joda.time.DateTime; +import org.json.JSONObject; + +/** + * Represents an IncidentsData object. + *
Each value is nullable & has a limit of 24 hours into the future. + */ +public record IncidentsData( + DateTime invitesDisabledUntil, + DateTime dmsDisabledUntil +) implements Compilerable { + + public boolean areInvitesDisabled() { + if (invitesDisabledUntil == null) return false; + return invitesDisabledUntil.isAfterNow(); + } + + public boolean areDmsDisabled() { + if (dmsDisabledUntil == null) return false; + return dmsDisabledUntil.isAfterNow(); + } + + @Override + public JSONObject compile() { + return new JSONObject() + .put("invites_disabled_until", invitesDisabledUntil.toString()) + .put("dms_disabled_until", dmsDisabledUntil.toString()); + } + + public static IncidentsData decompile(@NotNull JSONObject obj) { + DateTime invitesDisabledUntil = obj.has("invites_disabled_until") && !obj.isNull("invites_disabled_until") ? DateTime.parse(obj.getString("invites_disabled_until")) : null; + DateTime dmsDisabledUntil = obj.has("dms_disabled_until") && !obj.isNull("dms_disabled_until") ? DateTime.parse(obj.getString("dms_disabled_until")) : null; + + return new IncidentsData( + invitesDisabledUntil, + dmsDisabledUntil + ); + } + + public static IncidentsData none() { + return new IncidentsData( + null, + null + ); + } + + public static IncidentsData invitesDisabled23Hours() { + return new IncidentsData( + DateTime.now().plusHours(23), + null + ); + } + + public static IncidentsData dmsDisabled23Hours() { + return new IncidentsData( + null, + DateTime.now().plusHours(23) + ); + } + + public static IncidentsData allDisabled23Hours() { + return new IncidentsData( + DateTime.now().plusHours(23), + DateTime.now().plusHours(23) + ); + } +} diff --git a/src/main/java/com/seailz/discordjar/utils/URLS.java b/src/main/java/com/seailz/discordjar/utils/URLS.java index 4e80c0d1..dac71d82 100644 --- a/src/main/java/com/seailz/discordjar/utils/URLS.java +++ b/src/main/java/com/seailz/discordjar/utils/URLS.java @@ -445,6 +445,7 @@ public static class COMMANDS { public static class PATCH { public static class GUILD { + public static String MODIFY_GUILD_INCIDENT_ACTIONS = "/guilds/{guild.id}/incident-actions"; public static class SCHEDULED_EVENTS { public static String MODIFY_GUILD_SCHEDULED_EVENT = "/guilds/{guild.id}/scheduled-events/{event.id}"; } From cf68f133273c2dad9b9afdd624cfb40a6b8f11d7 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Thu, 26 Oct 2023 12:26:52 +0100 Subject: [PATCH 2/3] fix(incident-actions): fixed an issue with IncidentsData#compile where if either of the values were null it would fail to compile and throw a null pointer exception --- .../seailz/discordjar/model/guild/incident/IncidentsData.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/seailz/discordjar/model/guild/incident/IncidentsData.java b/src/main/java/com/seailz/discordjar/model/guild/incident/IncidentsData.java index 48fdaac8..3422b8a0 100644 --- a/src/main/java/com/seailz/discordjar/model/guild/incident/IncidentsData.java +++ b/src/main/java/com/seailz/discordjar/model/guild/incident/IncidentsData.java @@ -27,8 +27,8 @@ public boolean areDmsDisabled() { @Override public JSONObject compile() { return new JSONObject() - .put("invites_disabled_until", invitesDisabledUntil.toString()) - .put("dms_disabled_until", dmsDisabledUntil.toString()); + .put("invites_disabled_until", invitesDisabledUntil == null ? JSONObject.NULL : invitesDisabledUntil.toString()) + .put("dms_disabled_until", dmsDisabledUntil == null ? JSONObject.NULL : dmsDisabledUntil.toString()); } public static IncidentsData decompile(@NotNull JSONObject obj) { From 68beb7849d81420fb007a091f4bd05d0b1d1aa0f Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Thu, 26 Oct 2023 12:32:44 +0100 Subject: [PATCH 3/3] fix(incident-actions): Guild#modifyGuildIncidentActions was using the wrong HTTP verb --- src/main/java/com/seailz/discordjar/model/guild/Guild.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/seailz/discordjar/model/guild/Guild.java b/src/main/java/com/seailz/discordjar/model/guild/Guild.java index bdcdcd32..7e94ea3d 100644 --- a/src/main/java/com/seailz/discordjar/model/guild/Guild.java +++ b/src/main/java/com/seailz/discordjar/model/guild/Guild.java @@ -1750,7 +1750,7 @@ public Response modifyGuildIncidentActions(@NotNull IncidentsData incident URLS.PATCH.GUILD.MODIFY_GUILD_INCIDENT_ACTIONS.replace("{guild.id}", this.id), discordJar, URLS.PATCH.GUILD.MODIFY_GUILD_INCIDENT_ACTIONS, - RequestMethod.PATCH + RequestMethod.PUT ).invoke(); res.complete(null); } catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {