Skip to content

Commit

Permalink
Added Safety Alerts Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail committed Aug 28, 2024
1 parent 18c9508 commit 733f71f
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,17 @@ default int getMaxEmojis()
@Nullable
TextChannel getCommunityUpdatesChannel();

/**
* Provides the {@link TextChannel TextChannel} that receives discord safety alerts.
* <br>If this guild doesn't have the COMMUNITY {@link #getFeatures() feature}, this returns {@code null}.
*
* @return Possibly-null {@link TextChannel TextChannel} that is the saferty alerts channel.
*
* @see #getFeatures()
*/
@Nullable
TextChannel getSafetyAlertsChannel();

/**
* The {@link net.dv8tion.jda.api.entities.Member Member} object for the owner of this Guild.
* <br>This is null when the owner is no longer in this guild or not yet loaded (lazy loading).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package net.dv8tion.jda.api.events.guild.update;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Indicates that the safety alert channel of a {@link Guild Guild} changed.
*
* <p>Can be used to detect when a guild safety alert changes and retrieve the old one
*
* <p>Identifier: {@code safety_alerts_channel}
*/
public class GuildUpdateSafetyAlertsChannelEvent extends GenericGuildUpdateEvent<TextChannel>
{
public static final String IDENTIFIER = "safety_alerts_channel";

public GuildUpdateSafetyAlertsChannelEvent(@Nonnull JDA api, long responseNumber, @Nonnull Guild guild, @Nullable TextChannel oldSafetyAlertsChannel)
{
super(api, responseNumber, guild, oldSafetyAlertsChannel, guild.getSafetyAlertsChannel(), IDENTIFIER);
}

/**
* The previous safety alert channel.
*
* @return The previous safety alert channel
*/
@Nullable
public TextChannel getOldSafetyAlertsChannel()
{
return getOldValue();
}

/**
* The new safety alert channel.
*
* @return The new safety alert channel
*/
@Nullable
public TextChannel getNewSafetyAlertsChannel()
{
return getNewValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public void onGuildUpdateAfkChannel(@Nonnull GuildUpdateAfkChannelEvent event) {
public void onGuildUpdateSystemChannel(@Nonnull GuildUpdateSystemChannelEvent event) {}
public void onGuildUpdateRulesChannel(@Nonnull GuildUpdateRulesChannelEvent event) {}
public void onGuildUpdateCommunityUpdatesChannel(@Nonnull GuildUpdateCommunityUpdatesChannelEvent event) {}
public void onGuildUpdateSafetyAlertsChannel(@Nonnull GuildUpdateSafetyAlertsChannelEvent event) {}
public void onGuildUpdateAfkTimeout(@Nonnull GuildUpdateAfkTimeoutEvent event) {}
public void onGuildUpdateExplicitContentLevel(@Nonnull GuildUpdateExplicitContentLevelEvent event) {}
public void onGuildUpdateIcon(@Nonnull GuildUpdateIconEvent event) {}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/dv8tion/jda/api/managers/GuildManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public interface GuildManager extends Manager<GuildManager>
long BOOST_PROGRESS_BAR_ENABLED = 1 << 14;
/** Used to add or remove modifiable features (such as {@code "INVITES_DISABLED"}) */
long FEATURES = 1 << 15;
/** Used to rest the safety alerts channel field */
long SAFETY_ALERTS_CHANNEL = 1 << 16;

/**
* Resets the fields specified by the provided bit-flag pattern.
Expand Down Expand Up @@ -259,6 +261,22 @@ public interface GuildManager extends Manager<GuildManager>
@CheckReturnValue
GuildManager setCommunityUpdatesChannel(@Nullable TextChannel communityUpdatesChannel);

/**
* Sets the safety alerts {@link TextChannel} of this {@link Guild Guild}.
*
* @param safetyAlertsChannel
* The new safety alerts channel for this {@link Guild}
* or {@code null} to reset
*
* @throws IllegalArgumentException
* If the provided channel is not from this guild
*
* @return GuildManager for chaining convenience
*/
@Nonnull
@CheckReturnValue
GuildManager setSafetyAlertsChannel(@Nullable TextChannel safetyAlertsChannel);

/**
* Sets the afk {@link net.dv8tion.jda.api.entities.Guild.Timeout Timeout} of this {@link net.dv8tion.jda.api.entities.Guild Guild}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ public GuildImpl createGuild(long guildId, DataObject guildJson, TLongObjectMap<
final long systemChannelId = guildJson.getUnsignedLong("system_channel_id", 0L);
final long rulesChannelId = guildJson.getUnsignedLong("rules_channel_id", 0L);
final long communityUpdatesChannelId = guildJson.getUnsignedLong("public_updates_channel_id", 0L);
final long safetyAlertsChannelId = guildJson.getUnsignedLong("safety_alerts_channel_id", 0L);
final int boostCount = guildJson.getInt("premium_subscription_count", 0);
final int boostTier = guildJson.getInt("premium_tier", 0);
final int maxMembers = guildJson.getInt("max_members", 0);
Expand Down Expand Up @@ -399,7 +400,8 @@ public GuildImpl createGuild(long guildId, DataObject guildJson, TLongObjectMap<
guildObj.setAfkChannel(guildObj.getVoiceChannelById(afkChannelId))
.setSystemChannel(guildObj.getTextChannelById(systemChannelId))
.setRulesChannel(guildObj.getTextChannelById(rulesChannelId))
.setCommunityUpdatesChannel(guildObj.getTextChannelById(communityUpdatesChannelId));
.setCommunityUpdatesChannel(guildObj.getTextChannelById(communityUpdatesChannelId))
.setSafetyAlertsChannel(guildObj.getTextChannelById(safetyAlertsChannelId));

return guildObj;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/entities/GuildImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public class GuildImpl implements Guild
private TextChannel systemChannel;
private TextChannel rulesChannel;
private TextChannel communityUpdatesChannel;
private TextChannel safetyAlertsChannel;
private Role publicRole;
private VerificationLevel verificationLevel = VerificationLevel.UNKNOWN;
private NotificationLevel defaultNotificationLevel = NotificationLevel.UNKNOWN;
Expand Down Expand Up @@ -632,6 +633,13 @@ public TextChannel getCommunityUpdatesChannel()
return communityUpdatesChannel;
}

@Nullable
@Override
public TextChannel getSafetyAlertsChannel()
{
return safetyAlertsChannel;
}

@Nonnull
@Override
public RestAction<List<Webhook>> retrieveWebhooks()
Expand Down Expand Up @@ -2191,6 +2199,12 @@ public GuildImpl setCommunityUpdatesChannel(TextChannel communityUpdatesChannel)
return this;
}

public GuildImpl setSafetyAlertsChannel(TextChannel safetyAlertsChannel)
{
this.safetyAlertsChannel = safetyAlertsChannel;
return this;
}

public GuildImpl setPublicRole(Role publicRole)
{
this.publicRole = publicRole;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ protected Long handleInternally(DataObject content)
? null : guild.getChannelById(TextChannel.class, content.getLong("rules_channel_id"));
TextChannel communityUpdatesChannel = content.isNull("public_updates_channel_id")
? null : guild.getChannelById(TextChannel.class, content.getLong("public_updates_channel_id"));
TextChannel safetyAlertsChannel = content.isNull("safety_alerts_channel_id")
? null : guild.getChannelById(TextChannel.class, content.getLong("safety_alerts_channel_id"));
Set<String> features;
if (!content.isNull("features"))
{
Expand Down Expand Up @@ -304,6 +306,15 @@ protected Long handleInternally(DataObject content)
getJDA(), responseNumber,
guild, oldCommunityUpdatesChannel));
}
if (!Objects.equals(safetyAlertsChannel, guild.getSafetyAlertsChannel()))
{
TextChannel oldSafetyAlertsChannel = guild.getSafetyAlertsChannel();
guild.setSafetyAlertsChannel(safetyAlertsChannel);
getJDA().handleEvent(
new GuildUpdateSafetyAlertsChannelEvent(
getJDA(), responseNumber,
guild, oldSafetyAlertsChannel));
}
if (content.hasKey("nsfw_level") && nsfwLevel != guild.getNSFWLevel())
{
Guild.NSFWLevel oldNSFWLevel = guild.getNSFWLevel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class GuildManagerImpl extends ManagerBase<GuildManager> implements Guild

protected String name;
protected Icon icon, splash, banner;
protected String afkChannel, systemChannel, rulesChannel, communityUpdatesChannel;
protected String afkChannel, systemChannel, rulesChannel, communityUpdatesChannel, safetyAlertsChannel;
protected String description;
protected int afkTimeout;
protected int mfaLevel;
Expand Down Expand Up @@ -91,6 +91,8 @@ public GuildManagerImpl reset(long fields)
this.rulesChannel = null;
if ((fields & COMMUNITY_UPDATES_CHANNEL) == COMMUNITY_UPDATES_CHANNEL)
this.communityUpdatesChannel = null;
if ((fields & SAFETY_ALERTS_CHANNEL) == SAFETY_ALERTS_CHANNEL)
this.safetyAlertsChannel = null;
if ((fields & DESCRIPTION) == DESCRIPTION)
this.description = null;
if ((fields & BANNER) == BANNER)
Expand Down Expand Up @@ -203,6 +205,17 @@ public GuildManagerImpl setCommunityUpdatesChannel(TextChannel communityUpdatesC
return this;
}

@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setSafetyAlertsChannel(TextChannel safetyAlertsChannel)
{
Checks.check(safetyAlertsChannel == null || safetyAlertsChannel.getGuild().equals(getGuild()), "Channel must be from the same guild");
this.safetyAlertsChannel = safetyAlertsChannel == null ? null : safetyAlertsChannel.getId();
set |= SAFETY_ALERTS_CHANNEL;
return this;
}

@Nonnull
@Override
@CheckReturnValue
Expand Down

0 comments on commit 733f71f

Please sign in to comment.