@@ -1,3 +1,4 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
namespace Discord; | |||
@@ -18,14 +19,25 @@ public class RoleConnection | |||
public string PlatformUsername { get; } | |||
/// <summary> | |||
/// | |||
/// Gets the object mapping <see cref="RoleConnectionMetadata"/> keys to their string-ified values. | |||
/// </summary> | |||
public IReadOnlyDictionary<string, object> Metadata { get; } | |||
public IReadOnlyDictionary<string, string> Metadata { get; } | |||
internal RoleConnection(string platformName, string platformUsername, IReadOnlyDictionary<string, object> metadata) | |||
internal RoleConnection(string platformName, string platformUsername, IReadOnlyDictionary<string, string> metadata) | |||
{ | |||
PlatformName = platformName; | |||
PlatformUsername = platformUsername; | |||
Metadata = metadata; | |||
} | |||
/// <summary> | |||
/// Initializes a new <see cref="RoleConnectionProperties"/> with the data from this object. | |||
/// </summary> | |||
public RoleConnectionProperties ToRoleConnectionProperties() | |||
=> new() | |||
{ | |||
PlatformName = PlatformName, | |||
PlatformUsername = PlatformUsername, | |||
Metadata = Metadata.ToDictionary() | |||
}; | |||
} |
@@ -120,5 +120,19 @@ public class RoleConnectionMetadataProperties | |||
/// Initializes a new instance of <see cref="RoleConnectionMetadataProperties"/>. | |||
/// </summary> | |||
public RoleConnectionMetadataProperties() { } | |||
/// <summary> | |||
/// Initializes a new <see cref="RoleConnectionMetadataProperties"/> with the data from provided <see cref="RoleConnectionMetadata"/>. | |||
/// </summary> | |||
public static RoleConnectionMetadataProperties FromRoleConnectionMetadata(RoleConnectionMetadata metadata) | |||
=> new() | |||
{ | |||
Name = metadata.Name, | |||
Description = metadata.Description, | |||
Type = metadata.Type, | |||
Key = metadata.Key, | |||
NameLocalizations = metadata.NameLocalizations, | |||
DescriptionLocalizations = metadata.DescriptionLocalizations | |||
}; | |||
} | |||
@@ -0,0 +1,141 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
namespace Discord; | |||
/// <summary> | |||
/// Represents the properties used to modify user's <see cref="RoleConnection"/>. | |||
/// </summary> | |||
public class RoleConnectionProperties | |||
{ | |||
private const int MaxPlatformNameLength = 50; | |||
private const int MaxPlatformUsernameLength = 100; | |||
private const int MaxMetadataRecords = 100; | |||
private string _platformName; | |||
private string _platformUsername; | |||
private Dictionary<string, string> _metadata; | |||
/// <summary> | |||
/// Gets or sets the vanity name of the platform a bot has connected. Max 50 characters. | |||
/// </summary> | |||
public string PlatformName | |||
{ | |||
get => _platformName; | |||
set | |||
{ | |||
if (value is not null) | |||
Preconditions.AtMost(value.Length, MaxPlatformNameLength, nameof(PlatformName), $"Platform name length must be less or equal to {MaxPlatformNameLength}"); | |||
_platformName = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the username on the platform a bot has connected. Max 100 characters. | |||
/// </summary> | |||
public string PlatformUsername | |||
{ | |||
get => _platformUsername; | |||
set | |||
{ | |||
if(value is not null) | |||
Preconditions.AtMost(value.Length, MaxPlatformUsernameLength, nameof(PlatformUsername), $"Platform username length must be less or equal to {MaxPlatformUsernameLength}"); | |||
_platformUsername = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets object mapping <see cref="RoleConnectionMetadata"/> keys to their string-ified values. | |||
/// </summary> | |||
public Dictionary<string, string> Metadata | |||
{ | |||
get => _metadata; | |||
set | |||
{ | |||
if (value is not null) | |||
Preconditions.AtMost(value.Count, MaxPlatformUsernameLength, nameof(Metadata), $"Metadata records count must be less or equal to {MaxMetadataRecords}"); | |||
_metadata = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Adds a metadata record with the provided key and value. | |||
/// </summary> | |||
/// <returns>The current <see cref="RoleConnectionProperties"/>.</returns> | |||
public RoleConnectionProperties WithDate(string key, DateTimeOffset value) | |||
=> AddMetadataRecord(key, value.ToString("O")); | |||
/// <summary> | |||
/// Adds a metadata record with the provided key and value. | |||
/// </summary> | |||
/// <returns>The current <see cref="RoleConnectionProperties"/>.</returns> | |||
public RoleConnectionProperties WithBool(string key, bool value) | |||
=> AddMetadataRecord(key, value ? "1" : "0"); | |||
/// <summary> | |||
/// Adds a metadata record with the provided key and value. | |||
/// </summary> | |||
/// <returns>The current <see cref="RoleConnectionProperties"/>.</returns> | |||
public RoleConnectionProperties WithNumber(string key, int value) | |||
=> AddMetadataRecord(key, value.ToString()); | |||
/// <summary> | |||
/// Adds a metadata record with the provided key and value. | |||
/// </summary> | |||
/// <returns>The current <see cref="RoleConnectionProperties"/>.</returns> | |||
public RoleConnectionProperties WithNumber(string key, uint value) | |||
=> AddMetadataRecord(key, value.ToString()); | |||
/// <summary> | |||
/// Adds a metadata record with the provided key and value. | |||
/// </summary> | |||
/// <returns>The current <see cref="RoleConnectionProperties"/>.</returns> | |||
public RoleConnectionProperties WithNumber(string key, long value) | |||
=> AddMetadataRecord(key, value.ToString()); | |||
/// <summary> | |||
/// Adds a metadata record with the provided key and value. | |||
/// </summary> | |||
/// <returns>The current <see cref="RoleConnectionProperties"/>.</returns> | |||
public RoleConnectionProperties WithNumber(string key, ulong value) | |||
=> AddMetadataRecord(key, value.ToString()); | |||
internal RoleConnectionProperties AddMetadataRecord(string key, string value) | |||
{ | |||
Metadata ??= new Dictionary<string, string>(); | |||
if(!Metadata.ContainsKey(key)) | |||
Preconditions.AtMost(Metadata.Count + 1, MaxPlatformUsernameLength, nameof(Metadata), $"Metadata records count must be less or equal to {MaxMetadataRecords}"); | |||
_metadata[key] = value; | |||
return this; | |||
} | |||
/// <summary> | |||
/// Initializes a new instance of <see cref="RoleConnectionProperties"/>. | |||
/// </summary> | |||
/// <param name="platformName">The name of the platform a bot has connected.</param>s | |||
/// <param name="platformUsername">Gets the username on the platform a bot has connected.</param> | |||
/// <param name="metadata">Object mapping <see cref="RoleConnectionMetadata"/> keys to their values.</param> | |||
public RoleConnectionProperties(string platformName, string platformUsername, IDictionary<string, string> metadata = null) | |||
{ | |||
PlatformName = platformName; | |||
PlatformUsername = platformUsername; | |||
Metadata = metadata.ToDictionary(); | |||
} | |||
/// <summary> | |||
/// Initializes a new instance of <see cref="RoleConnectionProperties"/>. | |||
/// </summary> | |||
public RoleConnectionProperties() {} | |||
/// <summary> | |||
/// Initializes a new <see cref="RoleConnectionProperties"/> with the data from provided <see cref="RoleConnection"/>. | |||
/// </summary> | |||
public static RoleConnectionProperties FromRoleConnection(RoleConnection roleConnection) | |||
=> new() | |||
{ | |||
PlatformName = roleConnection.PlatformName, | |||
PlatformUsername = roleConnection.PlatformUsername, | |||
Metadata = roleConnection.Metadata.ToDictionary() | |||
}; | |||
} |
@@ -265,7 +265,7 @@ namespace Discord.Rest | |||
=> client.ApiClient.RemoveRoleAsync(guildId, userId, roleId, options); | |||
#endregion | |||
#region Role Subscription Metadata | |||
#region Role Connection Metadata | |||
public static async Task<IReadOnlyCollection<RoleConnectionMetadata>> GetRoleConnectionMetadataRecordsAsync(BaseDiscordClient client, RequestOptions options = null) | |||
=> (await client.ApiClient.GetApplicationRoleConnectionMetadataRecordsAsync(options)) | |||
@@ -308,6 +308,33 @@ namespace Discord.Rest | |||
: null)) | |||
.ToImmutableArray(); | |||
public static async Task<RoleConnection> GetUserRoleConnectionAsync(ulong applicationId, BaseDiscordClient client, RequestOptions options = null) | |||
{ | |||
var roleConnection = await client.ApiClient.GetUserApplicationRoleConnectionAsync(applicationId, options); | |||
return new RoleConnection(roleConnection.PlatformName.GetValueOrDefault(null), | |||
roleConnection.PlatformUsername.GetValueOrDefault(null), | |||
roleConnection.Metadata.GetValueOrDefault()); | |||
} | |||
public static async Task<RoleConnection> ModifyUserRoleConnectionAsync(ulong applicationId, RoleConnectionProperties roleConnection, BaseDiscordClient client, RequestOptions options = null) | |||
{ | |||
var updatedConnection = await client.ApiClient.ModifyUserApplicationRoleConnectionAsync(applicationId, | |||
new API.RoleConnection | |||
{ | |||
PlatformName = roleConnection.PlatformName, | |||
PlatformUsername = roleConnection.PlatformUsername, | |||
Metadata = roleConnection.Metadata | |||
}, options); | |||
return new RoleConnection( | |||
updatedConnection.PlatformName.GetValueOrDefault(null), | |||
updatedConnection.PlatformUsername.GetValueOrDefault(null), | |||
updatedConnection.Metadata.GetValueOrDefault()?.ToImmutableDictionary() | |||
); | |||
} | |||
#endregion | |||
} | |||
} |
@@ -2488,11 +2488,11 @@ namespace Discord.API | |||
public async Task<RoleConnectionMetadata[]> UpdateApplicationRoleConnectionMetadataRecordsAsync(RoleConnectionMetadata[] roleConnections, RequestOptions options = null) | |||
=> await SendJsonAsync <RoleConnectionMetadata[]>("PUT", () => $"applications/{CurrentApplicationId}/role-connections/metadata", roleConnections, new BucketIds(), options: options).ConfigureAwait(false); | |||
public async Task<RoleConnection> GetUserApplicationRoleConnection(RequestOptions options = null) | |||
=> await SendAsync<RoleConnection>("GET", () => $"users/@me/applications/{CurrentApplicationId}/role-connection", new BucketIds(), options: options); | |||
public async Task<RoleConnection> GetUserApplicationRoleConnectionAsync(ulong applicationId, RequestOptions options = null) | |||
=> await SendAsync<RoleConnection>("GET", () => $"users/@me/applications/{applicationId}/role-connection", new BucketIds(), options: options); | |||
public async Task<RoleConnection> GetUserApplicationRoleConnection(RoleConnection connection, RequestOptions options = null) | |||
=> await SendJsonAsync<RoleConnection>("PUT", () => $"users/@me/applications/{CurrentApplicationId}/role-connection", connection, new BucketIds(), options: options); | |||
public async Task<RoleConnection> ModifyUserApplicationRoleConnectionAsync(ulong applicationId, RoleConnection connection, RequestOptions options = null) | |||
=> await SendJsonAsync<RoleConnection>("PUT", () => $"users/@me/applications/{applicationId}/role-connection", connection, new BucketIds(), options: options); | |||
#endregion | |||
} | |||
@@ -241,6 +241,12 @@ namespace Discord.Rest | |||
return ClientHelper.ModifyRoleConnectionMetadataRecordsAsync(metadata, this, options); | |||
} | |||
public Task<RoleConnection> GetUserApplicationRoleConnectionAsync(ulong applicationId, RequestOptions options = null) | |||
=> ClientHelper.GetUserRoleConnectionAsync(applicationId, this, options); | |||
public Task<RoleConnection> ModifyUserApplicationRoleConnectionAsync(ulong applicationId, RoleConnectionProperties roleConnection, RequestOptions options = null) | |||
=> ClientHelper.ModifyUserRoleConnectionAsync(applicationId, roleConnection, this, options); | |||
#endregion | |||
#region IDiscordClient | |||