Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(guilds): implemented support for incident actions #243

Merged
merged 10 commits into from
Feb 24, 2024
40 changes: 37 additions & 3 deletions src/main/java/com/seailz/discordjar/model/guild/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -143,7 +146,7 @@ public Guild(
List<Sticker> stickers,
boolean premiumProgressBarEnabled,
String safetyAlertChannelId,
DiscordJar discordJar,
IncidentsData incidentsData, DiscordJar discordJar,
JsonCache roleCache
) {
this.id = id;
Expand Down Expand Up @@ -187,6 +190,7 @@ public Guild(
this.stickers = stickers;
this.premiumProgressBarEnabled = premiumProgressBarEnabled;
this.safetyAlertChannelId = safetyAlertChannelId;
this.incidentsData = incidentsData;
this.discordJar = discordJar;
this.roleCache = roleCache;
}
Expand Down Expand Up @@ -330,6 +334,9 @@ public List<Sticker> stickers() {
public boolean premiumProgressBarEnabled() {
return premiumProgressBarEnabled;
}
public IncidentsData incidentsData() {
return incidentsData;
}

public DiscordJar discordJar() {
return discordJar;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -676,6 +684,8 @@ public static Guild decompile(JSONObject obj, DiscordJar discordJar, boolean byp
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,
Expand Down Expand Up @@ -718,7 +728,7 @@ public static Guild decompile(JSONObject obj, DiscordJar discordJar, boolean byp
stickers,
premiumProgressBarEnabled,
safetyAlertsChannelId,
discordJar,
incidentsData, discordJar,
JsonCache.newc(new DiscordRequest(
new JSONObject(),
new HashMap<>(),
Expand Down Expand Up @@ -1735,6 +1745,30 @@ public Response<Void> deleteScheduledEvent(String id) {
return res;
}

public Response<Void> modifyGuildIncidentActions(@NotNull IncidentsData incidentsData) {
Response<Void> 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.PUT
).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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <br> 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 == null ? JSONObject.NULL : invitesDisabledUntil.toString())
.put("dms_disabled_until", dmsDisabledUntil == null ? JSONObject.NULL : 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)
);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/seailz/discordjar/utils/URLS.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,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}";
}
Expand Down
Loading