Skip to content

Commit

Permalink
Add support for guild locale (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Aug 5, 2020
1 parent 0f0fe96 commit 70240d1
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
8 changes: 8 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 @@ -414,6 +414,14 @@ default String getVanityUrl()
@Nullable
String getDescription();

/**
* The preferred locale for this guild.
*
* @return The preferred {@link Locale} for this guild
*/
@Nonnull
Locale getLocale();

/**
* The guild banner id.
* <br>This is shown in guilds below the guild name.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2015-2020 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;
import java.util.Locale;

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

public GuildUpdateLocaleEvent(@Nonnull JDA api, long responseNumber, @Nonnull Guild guild, @Nonnull Locale previous)
{
super(api, responseNumber, guild, previous, guild.getLocale(), IDENTIFIER);
}

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

@Nonnull
@Override
public Locale getNewValue()
{
return super.getNewValue();
}
}
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ public void onGuildUpdateOwner(@Nonnull GuildUpdateOwnerEvent event) {}
public void onGuildUpdateRegion(@Nonnull GuildUpdateRegionEvent event) {}
public void onGuildUpdateSplash(@Nonnull GuildUpdateSplashEvent event) {}
public void onGuildUpdateVerificationLevel(@Nonnull GuildUpdateVerificationLevelEvent event) {}
public void onGuildUpdateLocale(@Nonnull GuildUpdateLocaleEvent event) {}
public void onGuildUpdateFeatures(@Nonnull GuildUpdateFeaturesEvent event) {}
public void onGuildUpdateVanityCode(@Nonnull GuildUpdateVanityCodeEvent event) {}
public void onGuildUpdateBanner(@Nonnull GuildUpdateBannerEvent event) {}
Expand Down Expand Up @@ -596,6 +597,8 @@ else if (event instanceof GuildUpdateSplashEvent)
onGuildUpdateSplash((GuildUpdateSplashEvent) event);
else if (event instanceof GuildUpdateVerificationLevelEvent)
onGuildUpdateVerificationLevel((GuildUpdateVerificationLevelEvent) event);
else if (event instanceof GuildUpdateLocaleEvent)
onGuildUpdateLocale((GuildUpdateLocaleEvent) event);
else if (event instanceof GuildUpdateFeaturesEvent)
onGuildUpdateFeatures((GuildUpdateFeaturesEvent) event);
else if (event instanceof GuildUpdateVanityCodeEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public GuildImpl createGuild(long guildId, DataObject guildJson, TLongObjectMap<
final String description = guildJson.getString("description", null);
final String vanityCode = guildJson.getString("vanity_url_code", null);
final String bannerId = guildJson.getString("banner", null);
final String locale = guildJson.getString("preferred_locale", "en");
final DataArray roleArray = guildJson.getArray("roles");
final DataArray channelArray = guildJson.getArray("channels");
final DataArray emotesArray = guildJson.getArray("emojis");
Expand Down Expand Up @@ -205,6 +206,7 @@ public GuildImpl createGuild(long guildId, DataObject guildJson, TLongObjectMap<
.setDefaultNotificationLevel(Guild.NotificationLevel.fromKey(notificationLevel))
.setExplicitContentLevel(Guild.ExplicitContentLevel.fromKey(explicitContentLevel))
.setRequiredMFALevel(Guild.MFALevel.fromKey(mfaLevel))
.setLocale(locale)
.setBoostCount(boostCount)
.setBoostTier(boostTier)
.setMemberCount(memberCount);
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 @@ -111,6 +111,7 @@ public class GuildImpl implements Guild
private ExplicitContentLevel explicitContentLevel = ExplicitContentLevel.UNKNOWN;
private Timeout afkTimeout;
private BoostTier boostTier = BoostTier.NONE;
private Locale preferredLocale = Locale.ENGLISH;
private boolean available;
private boolean canSendVerification = false;
private int memberCount;
Expand Down Expand Up @@ -249,6 +250,13 @@ public String getDescription()
return description;
}

@Nonnull
@Override
public Locale getLocale()
{
return preferredLocale;
}

@Nullable
@Override
public String getBannerId()
Expand Down Expand Up @@ -1581,6 +1589,12 @@ public GuildImpl setAfkTimeout(Timeout afkTimeout)
return this;
}

public GuildImpl setLocale(String locale)
{
this.preferredLocale = Locale.forLanguageTag(locale);
return this;
}

public GuildImpl setBoostTier(int tier)
{
this.boostTier = BoostTier.fromKey(tier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.dv8tion.jda.internal.requests.WebSocketClient;

import java.util.Collections;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -80,6 +81,7 @@ protected Long handleInternally(DataObject content)
Guild.MFALevel mfaLevel = Guild.MFALevel.fromKey(content.getInt("mfa_level"));
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"));
VoiceChannel afkChannel = content.isNull("afk_channel_id")
? null : guild.getVoiceChannelsView().get(content.getLong("afk_channel_id"));
TextChannel systemChannel = content.isNull("system_channel_id")
Expand Down Expand Up @@ -263,6 +265,15 @@ protected Long handleInternally(DataObject content)
getJDA(), responseNumber,
guild, oldAfkTimeout));
}
if (!Objects.equals(locale, guild.getLocale()))
{
Locale oldLocale = guild.getLocale();
guild.setLocale(locale.toLanguageTag());
getJDA().handleEvent(
new GuildUpdateLocaleEvent(
getJDA(), responseNumber,
guild, oldLocale));
}
if (!Objects.equals(afkChannel, guild.getAfkChannel()))
{
VoiceChannel oldAfkChannel = guild.getAfkChannel();
Expand Down

0 comments on commit 70240d1

Please sign in to comment.