Browse Source

feature: #1381 Guild PreferredLocale support (#1387)

* Fix #1381 Guild PreferredLocale support

Adds support for getting and modifying a guild's preferred_locale. This is a language tag in IETF BCP 47 format, which works with the built-in CultureInfo.

While Discord only supports a number of cultures, I think that this restriction should be handled at the API and not by the wrapper. (Also easier on our end)

* Add PreferredCulture to IGuild

This property was defined in RestGuild and SocketGuild, so it only makes sense to make it part of IGuild as well.
tags/2.2.0
Chris Johnston Christopher F 5 years ago
parent
commit
a61adb07e0
7 changed files with 65 additions and 0 deletions
  1. +20
    -0
      src/Discord.Net.Core/Entities/Guilds/GuildProperties.cs
  2. +19
    -0
      src/Discord.Net.Core/Entities/Guilds/IGuild.cs
  3. +2
    -0
      src/Discord.Net.Rest/API/Common/Guild.cs
  4. +2
    -0
      src/Discord.Net.Rest/API/Rest/ModifyGuildParams.cs
  5. +6
    -0
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
  6. +8
    -0
      src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
  7. +8
    -0
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs

+ 20
- 0
src/Discord.Net.Core/Entities/Guilds/GuildProperties.cs View File

@@ -1,3 +1,5 @@
using System.Globalization;

namespace Discord
{
/// <summary>
@@ -84,5 +86,23 @@ namespace Discord
/// are enabled, without the need to manipulate the logic of the flag.
/// </remarks>
public Optional<SystemChannelMessageDeny> SystemChannelFlags { get; set; }
/// <summary>
/// Gets or sets the preferred locale of the guild in IETF BCP 47 language tag format.
/// </summary>
/// <remarks>
/// This property takes precedence over <see cref="PreferredCulture"/>.
/// When it is set, the value of <see cref="PreferredCulture"/>
/// will not be used.
/// </remarks>
public Optional<string> PreferredLocale { get; set; }
/// <summary>
/// Gets or sets the preferred locale of the guild.
/// </summary>
/// <remarks>
/// The <see cref="PreferredLocale"/> property takes precedence
/// over this property. When <see cref="PreferredLocale"/> is set,
/// the value of <see cref="PreferredCulture"/> will be unused.
/// </remarks>
public Optional<CultureInfo> PreferredCulture { get; set; }
}
}

+ 19
- 0
src/Discord.Net.Core/Entities/Guilds/IGuild.cs View File

@@ -1,6 +1,7 @@
using Discord.Audio;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;

namespace Discord
@@ -249,6 +250,24 @@ namespace Discord
/// </returns>
int PremiumSubscriptionCount { get; }

/// <summary>
/// Gets the preferred locale of this guild in IETF BCP 47
/// language tag format.
/// </summary>
/// <returns>
/// The preferred locale of the guild in IETF BCP 47
/// language tag format.
/// </returns>
string PreferredLocale { get; }

/// <summary>
/// Gets the preferred culture of this guild.
/// </summary>
/// <returns>
/// The preferred culture information of this guild.
/// </returns>
CultureInfo PreferredCulture { get; }

/// <summary>
/// Modifies this guild.
/// </summary>


+ 2
- 0
src/Discord.Net.Rest/API/Common/Guild.cs View File

@@ -58,5 +58,7 @@ namespace Discord.API
public SystemChannelMessageDeny SystemChannelFlags { get; set; }
[JsonProperty("premium_subscription_count")]
public int? PremiumSubscriptionCount { get; set; }
[JsonProperty("preferred_locale")]
public string PreferredLocale { get; set; }
}
}

+ 2
- 0
src/Discord.Net.Rest/API/Rest/ModifyGuildParams.cs View File

@@ -32,5 +32,7 @@ namespace Discord.API.Rest
public Optional<ExplicitContentFilterLevel> ExplicitContentFilter { get; set; }
[JsonProperty("system_channel_flags")]
public Optional<SystemChannelMessageDeny> SystemChannelFlags { get; set; }
[JsonProperty("preferred_locale")]
public string PreferredLocale { get; set; }
}
}

+ 6
- 0
src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs View File

@@ -68,6 +68,12 @@ namespace Discord.Rest
if (args.SystemChannelFlags.IsSpecified)
apiArgs.SystemChannelFlags = args.SystemChannelFlags.Value;

// PreferredLocale takes precedence over PreferredCulture
if (args.PreferredLocale.IsSpecified)
apiArgs.PreferredLocale = args.PreferredLocale.Value;
else if (args.PreferredCulture.IsSpecified)
apiArgs.PreferredLocale = args.PreferredCulture.Value.Name;

return await client.ApiClient.ModifyGuildAsync(guild.Id, apiArgs, options).ConfigureAwait(false);
}
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>


+ 8
- 0
src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs View File

@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using EmbedModel = Discord.API.GuildEmbed;
@@ -64,6 +65,11 @@ namespace Discord.Rest
public string Description { get; private set; }
/// <inheritdoc />
public int PremiumSubscriptionCount { get; private set; }
/// <inheritdoc />
public string PreferredLocale { get; private set; }

/// <inheritdoc />
public CultureInfo PreferredCulture { get; private set; }

/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
@@ -124,6 +130,8 @@ namespace Discord.Rest
SystemChannelFlags = model.SystemChannelFlags;
Description = model.Description;
PremiumSubscriptionCount = model.PremiumSubscriptionCount.GetValueOrDefault();
PreferredLocale = model.PreferredLocale;
PreferredCulture = new CultureInfo(PreferredLocale);

if (model.Emojis != null)
{


+ 8
- 0
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -5,6 +5,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -105,6 +106,11 @@ namespace Discord.WebSocket
public string Description { get; private set; }
/// <inheritdoc />
public int PremiumSubscriptionCount { get; private set; }
/// <inheritdoc />
public string PreferredLocale { get; private set; }

/// <inheritdoc />
public CultureInfo PreferredCulture { get; private set; }

/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
@@ -374,6 +380,8 @@ namespace Discord.WebSocket
SystemChannelFlags = model.SystemChannelFlags;
Description = model.Description;
PremiumSubscriptionCount = model.PremiumSubscriptionCount.GetValueOrDefault();
PreferredLocale = model.PreferredLocale;
PreferredCulture = new CultureInfo(PreferredLocale);

if (model.Emojis != null)
{


Loading…
Cancel
Save