Skip to content

Commit

Permalink
Add NSFWLevel (#1793)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xirado committed Sep 5, 2021
1 parent ce91865 commit 187fffc
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 1 deletion.
75 changes: 75 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 @@ -1116,6 +1116,17 @@ default Region getRegion()
@Nonnull
Member getSelfMember();

/**
* Returns the NSFW Level that this guild is classified with.
* <br>For a short description of the different values, see {@link net.dv8tion.jda.api.entities.Guild.NSFWLevel NSFWLevel}.
* <p>
* This value can only be modified by Discord after reviewing the Guild.
*
* @return The NSFWLevel of this guild.
*/
@Nonnull
NSFWLevel getNSFWLevel();

/**
* Gets the Guild specific {@link net.dv8tion.jda.api.entities.Member Member} object for the provided
* {@link net.dv8tion.jda.api.entities.User User}.
Expand Down Expand Up @@ -5869,6 +5880,70 @@ public static ExplicitContentLevel fromKey(int key)
}
}

/**
* Represents the NSFW level for this guild.
*/
enum NSFWLevel
{
/**
* Discord has not rated this guild.
*/
DEFAULT(0),
/**
* Is classified as a NSFW server
*/
EXPLICIT(1),
/**
* Doesn't classify as a NSFW server
*/
SAFE(2),
/**
* Is classified as NSFW and has an age restriction in place
*/
AGE_RESTRICTED(3),
/**
* Placeholder for unsupported levels.
*/
UNKNOWN(-1);

private final int key;

NSFWLevel(int key)
{
this.key = key;
}

/**
* The Discord id key used to represent this NSFW level.
*
* @return Integer id for this NSFW level.
*/
public int getKey()
{
return key;
}

/**
* Used to retrieve a {@link net.dv8tion.jda.api.entities.Guild.NSFWLevel NSFWLevel} based
* on the Discord id key.
*
* @param key
* The Discord id key representing the requested NSFWLevel.
*
* @return The NSFWLevel related to the provided key, or {@link #UNKNOWN NSFWLevel.UNKNOWN} if the key is not recognized.
*/
@Nonnull
public static NSFWLevel fromKey(int key)
{
for (NSFWLevel level : values())
{
if (level.getKey() == key)
return level;
}
return UNKNOWN;
}
}

/**
* The boost tier for this guild.
* <br>Each tier unlocks new perks for a guild that can be seen in the {@link #getFeatures() features}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.events.guild.update;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;

import javax.annotation.Nonnull;

/**
* Indicates that the {@link net.dv8tion.jda.api.entities.Guild.NSFWLevel NSFWLevel} of a {@link net.dv8tion.jda.api.entities.Guild Guild} changed.
*
* <p>Can be used to detect when a NSFWLevel changes and retrieve the old one
*
* <p>Identifier: {@code nsfw_level}
*/
public class GuildUpdateNSFWLevelEvent extends GenericGuildUpdateEvent<Guild.NSFWLevel>
{
public static final String IDENTIFIER = "nsfw_level";

public GuildUpdateNSFWLevelEvent(@Nonnull JDA api, long responseNumber, @Nonnull Guild guild, @Nonnull Guild.NSFWLevel oldNSFWLevel)
{
super(api, responseNumber, guild, oldNSFWLevel, guild.getNSFWLevel(), IDENTIFIER);
}

/**
* The old {@link Guild.NSFWLevel NSFWLevel}
*
* @return The old NSFWLevel
*/
@Nonnull
public Guild.NSFWLevel getOldNSFWLevel()
{
return getOldValue();
}

/**
* The new {@link Guild.NSFWLevel NSFWLevel}
*
* @return The new NSFWLevel
*/
@Nonnull
public Guild.NSFWLevel getNewNSFWLevel()
{
return getNewValue();
}

@Nonnull
@Override
public Guild.NSFWLevel getOldValue()
{
return super.getOldValue();
}

@Nonnull
@Override
public Guild.NSFWLevel getNewValue()
{
return super.getNewValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ public void onGuildUpdateBoostTier(@Nonnull GuildUpdateBoostTierEvent event) {}
public void onGuildUpdateBoostCount(@Nonnull GuildUpdateBoostCountEvent event) {}
public void onGuildUpdateMaxMembers(@Nonnull GuildUpdateMaxMembersEvent event) {}
public void onGuildUpdateMaxPresences(@Nonnull GuildUpdateMaxPresencesEvent event) {}
public void onGuildUpdateNSFWLevel(@Nonnull GuildUpdateNSFWLevelEvent event) {}

//Guild Invite Events
public void onGuildInviteCreate(@Nonnull GuildInviteCreateEvent event) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public GuildImpl createGuild(long guildId, DataObject guildJson, TLongObjectMap<
final int verificationLevel = guildJson.getInt("verification_level", 0);
final int notificationLevel = guildJson.getInt("default_message_notifications", 0);
final int explicitContentLevel = guildJson.getInt("explicit_content_filter", 0);
final int nsfwLevel = guildJson.getInt("nsfw_level", -1);

guildObj.setAvailable(true)
.setName(name)
Expand All @@ -224,7 +225,8 @@ public GuildImpl createGuild(long guildId, DataObject guildJson, TLongObjectMap<
.setLocale(locale)
.setBoostCount(boostCount)
.setBoostTier(boostTier)
.setMemberCount(memberCount);
.setMemberCount(memberCount)
.setNSFWLevel(Guild.NSFWLevel.fromKey(nsfwLevel));

SnowflakeCacheViewImpl<Guild> guildView = getJDA().getGuildsView();
try (UnlockHook hook = guildView.writeLock())
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 @@ -105,6 +105,7 @@ public class GuildImpl implements Guild
private NotificationLevel defaultNotificationLevel = NotificationLevel.UNKNOWN;
private MFALevel mfaLevel = MFALevel.UNKNOWN;
private ExplicitContentLevel explicitContentLevel = ExplicitContentLevel.UNKNOWN;
private NSFWLevel nsfwLevel = NSFWLevel.UNKNOWN;
private Timeout afkTimeout;
private BoostTier boostTier = BoostTier.NONE;
private Locale preferredLocale = Locale.ENGLISH;
Expand Down Expand Up @@ -1943,6 +1944,12 @@ public GuildImpl setMemberCount(int count)
return this;
}

public GuildImpl setNSFWLevel(NSFWLevel nsfwLevel)
{
this.nsfwLevel = nsfwLevel;
return this;
}

// -- Map getters --

public SortedSnowflakeCacheViewImpl<Category> getCategoriesView()
Expand Down Expand Up @@ -1980,6 +1987,13 @@ public MemberCacheViewImpl getMembersView()
return memberCache;
}

@Nonnull
@Override
public NSFWLevel getNSFWLevel()
{
return nsfwLevel;
}

@Nullable
public CacheView.SimpleCacheView<MemberPresenceImpl> getPresenceView()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected Long handleInternally(DataObject content)
Guild.VerificationLevel verificationLevel = Guild.VerificationLevel.fromKey(content.getInt("verification_level"));
Guild.NotificationLevel notificationLevel = Guild.NotificationLevel.fromKey(content.getInt("default_message_notifications"));
Guild.MFALevel mfaLevel = Guild.MFALevel.fromKey(content.getInt("mfa_level"));
Guild.NSFWLevel nsfwLevel = Guild.NSFWLevel.fromKey(content.getInt("nsfw_level", -1));
Guild.ExplicitContentLevel explicitContentLevel = Guild.ExplicitContentLevel.fromKey(content.getInt("explicit_content_filter"));
Guild.Timeout afkTimeout = Guild.Timeout.fromKey(content.getInt("afk_timeout"));
Locale locale = Locale.forLanguageTag(content.getString("preferred_locale"));
Expand Down Expand Up @@ -314,6 +315,15 @@ protected Long handleInternally(DataObject content)
getJDA(), responseNumber,
guild, oldCommunityUpdatesChannel));
}
if (content.hasKey("nsfw_level") && nsfwLevel != guild.getNSFWLevel())
{
Guild.NSFWLevel oldNSFWLevel = guild.getNSFWLevel();
guild.setNSFWLevel(nsfwLevel);
getJDA().handleEvent(
new GuildUpdateNSFWLevelEvent(
getJDA(), responseNumber,
guild, oldNSFWLevel));
}
return null;
}
}

0 comments on commit 187fffc

Please sign in to comment.