Browse Source

[Feature] Modify guild features (#2605)

* initial commit

* Update src/Discord.Net.Core/Entities/Guilds/GuildProperties.cs

---------

Co-authored-by: Casmir <68127614+csmir@users.noreply.github.com>
dev
Misha133 GitHub 2 years ago
parent
commit
177df2ce70
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 4 deletions
  1. +10
    -2
      src/Discord.Net.Core/Entities/Guilds/GuildFeature.cs
  2. +5
    -0
      src/Discord.Net.Core/Entities/Guilds/GuildProperties.cs
  3. +2
    -0
      src/Discord.Net.Rest/API/Rest/ModifyGuildParams.cs
  4. +2
    -1
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
  5. +45
    -1
      src/Discord.Net.Rest/Net/Converters/GuildFeaturesConverter.cs

+ 10
- 2
src/Discord.Net.Core/Entities/Guilds/GuildFeature.cs View File

@@ -34,11 +34,11 @@ namespace Discord
/// </summary>
Commerce = 1L << 4,
/// <summary>
/// The guild can enable welcome screen, Membership Screening, stage channels and discovery, and receives community updates.
/// The guild can enable welcome screen, Membership Screening, stage channels and discovery, and receives community updates. This feature is mutable.
/// </summary>
Community = 1L << 5,
/// <summary>
/// The guild is able to be discovered in the directory.
/// The guild is able to be discovered in the directory. This feature is mutable.
/// </summary>
Discoverable = 1L << 6,
/// <summary>
@@ -185,5 +185,13 @@ namespace Discord
/// The guild has been set as a support server on the App Directory.
/// </summary>
DeveloperSupportServer = 1L << 42,
/// <summary>
/// The guild has invites disabled. This feature is mutable.
/// </summary>
InvitesDisabled = 1L << 43,
/// <summary>
/// The guild has auto moderation enabled.
/// </summary>
AutoModeration = 1L << 44
}
}

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

@@ -113,5 +113,10 @@ namespace Discord
/// Gets or sets if the boost progress bar is enabled.
/// </summary>
public Optional<bool> IsBoostProgressBarEnabled { get; set; }

/// <summary>
/// Gets or sets the guild features enabled in this guild. Features that are not mutable will be ignored.
/// </summary>
public Optional<GuildFeature> Features { get; set; }
}
}

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

@@ -37,5 +37,7 @@ namespace Discord.API.Rest
public string PreferredLocale { get; set; }
[JsonProperty("premium_progress_bar_enabled")]
public Optional<bool> IsBoostProgressBarEnabled { get; set; }
[JsonProperty("features")]
public Optional<GuildFeatures> GuildFeatures { get; set; }
}
}

+ 2
- 1
src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs View File

@@ -39,7 +39,8 @@ namespace Discord.Rest
VerificationLevel = args.VerificationLevel,
ExplicitContentFilter = args.ExplicitContentFilter,
SystemChannelFlags = args.SystemChannelFlags,
IsBoostProgressBarEnabled = args.IsBoostProgressBarEnabled
IsBoostProgressBarEnabled = args.IsBoostProgressBarEnabled,
GuildFeatures = args.Features.IsSpecified ? new GuildFeatures(args.Features.Value, Array.Empty<string>()) : Optional.Create<GuildFeatures>(),
};

if (apiArgs.Banner.IsSpecified)


+ 45
- 1
src/Discord.Net.Rest/Net/Converters/GuildFeaturesConverter.cs View File

@@ -2,6 +2,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace Discord.Net.Converters
{
@@ -36,9 +37,52 @@ namespace Discord.Net.Converters

return new GuildFeatures(features, experimental.ToArray());
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
var guildFeatures = (GuildFeatures)value;

var enumValues = Enum.GetValues(typeof(GuildFeature));

writer.WriteStartArray();

foreach (var enumValue in enumValues)
{
var val = (GuildFeature)enumValue;
if (val is GuildFeature.None)
continue;

if (guildFeatures.Value.HasFlag(val))
{
writer.WriteValue(FeatureToApiString(val));
}
}
writer.WriteEndArray();
}

private string FeatureToApiString(GuildFeature feature)
{
var builder = new StringBuilder();
var firstChar = true;

foreach (var c in feature.ToString().ToCharArray())
{
if (char.IsUpper(c))
{
if (firstChar)
firstChar = false;
else
builder.Append("_");

builder.Append(c);
}
else
{
builder.Append(char.ToUpper(c));
}
}

return builder.ToString();
}
}
}

Loading…
Cancel
Save