Some endpoints in Discord API require a Bearer token, which can be obtained through [OAuth2 flow](https://discord.com/developers/docs/topics/oauth2). Discord.Net allows you to interact with these endpoints using the [DiscordRestClient].
The [DiscordRestClient] gets the current user when `LoginAsync()` is called. The user object can be found in the `CurrentUser` property.
If you need to fetch the user again, the `GetCurrentUserAsync()` method can be used.
[!code-csharp[Get current user](samples/current_user.cs)]
> [!NOTE]
> Some properties might be `null` depending on which scopes users authorized your app with.
> For example: `email` scope is required to fetch current user's email address.
## Fetching current user's guilds
The `GetGuildSummariesAsync()` method is used to fetch current user's guilds. Since it returns an `IAsyncEnumerable` you need to call `FlattenAsync()` to get a plain `IEnumerable` containing [RestUserGuild] objects.
[!code-csharp[Get current user's guilds](samples/current_user_guilds.cs)]
> [!WARNING]
> This method requires `guilds` scope
## Fetching current user's guild member object
To fetch the current user's guild member object, the `GetCurrentUserGuildMemberAsync()` method can be used.
[!code-csharp[Get current user's guild member](samples/current_user_guild_member.cs)]
> [!WARNING]
> This method requires `guilds.members.read` scope
## Get user connections
The `GetConnectionsAsync` method can be used to fetch current user's connections to other platforms.
[!code-csharp[Get current user's connections](samples/current_user_connections.cs)]
> [!WARNING]
> This method requires `connections` scope
## Application role connection
In addition to previous features, Discord.Net supports fetching & updating user's application role connection metadata values. `GetUserApplicationRoleConnectionAsync()` returns a [RoleConnection] object of the current user for the given application id.
The `ModifyUserApplicationRoleConnectionAsync()` method is used to update current user's role connection metadata values. A new set of values can be created with [RoleConnectionProperties] object.
[!code-csharp[Get current user's connections](samples/app_role_connection.cs)]
> [!WARNING]
> This method requires `role_connections.write` scope
var result = await client.PostAsync($"{ApiUrl}/overrides/{id}/dependency", new StringContent($"{{ \"info\": \"{name}\"}}", Encoding.UTF8, "application/json"));
if (!result.IsSuccessStatusCode)
throw new Exception("Failed to get dependency");
using(var ms = new MemoryStream())
using(var ms = new MemoryStream())
{
var innerStream = await result.Content.ReadAsStreamAsync();
@@ -38,7 +38,7 @@ public class RoleConnectionProperties
get => _platformUsername;
set
{
if(value is not null)
if(value is not null)
Preconditions.AtMost(value.Length, MaxPlatformUsernameLength, nameof(PlatformUsername), $"Platform username length must be less or equal to {MaxPlatformUsernameLength}");
_platformUsername = value;
}
@@ -103,7 +103,7 @@ public class RoleConnectionProperties
Preconditions.AtMost(Metadata.Count + 1, MaxPlatformUsernameLength, nameof(Metadata), $"Metadata records count must be less or equal to {MaxMetadataRecords}");
_metadata[key] = value;
@@ -126,7 +126,7 @@ public class RoleConnectionProperties
/// <summary>
/// Initializes a new instance of <see cref="RoleConnectionProperties"/>.
/// </summary>
public RoleConnectionProperties() {}
public RoleConnectionProperties() {}
/// <summary>
/// Initializes a new <see cref="RoleConnectionProperties"/> with the data from provided <see cref="RoleConnection"/>.
if (value?.Length > MaxTitleLength) throw new ArgumentException(message: $"Title length must be less than or equal to {MaxTitleLength}.", paramName: nameof(Title));
if (value?.Length > MaxTitleLength)
throw new ArgumentException(message: $"Title length must be less than or equal to {MaxTitleLength}.", paramName: nameof(Title));
_title = value;
}
}
@@ -63,7 +64,8 @@ namespace Discord
get => _description;
set
{
if (value?.Length > MaxDescriptionLength) throw new ArgumentException(message: $"Description length must be less than or equal to {MaxDescriptionLength}.", paramName: nameof(Description));
if (value?.Length > MaxDescriptionLength)
throw new ArgumentException(message: $"Description length must be less than or equal to {MaxDescriptionLength}.", paramName: nameof(Description));
_description = value;
}
}
@@ -100,8 +102,10 @@ namespace Discord
get => _fields;
set
{
if (value == null) throw new ArgumentNullException(paramName: nameof(Fields), message: "Cannot set an embed builder's fields collection to null.");
if (value.Count > MaxFieldCount) throw new ArgumentException(message: $"Field count must be less than or equal to {MaxFieldCount}.", paramName: nameof(Fields));
if (value == null)
throw new ArgumentNullException(paramName: nameof(Fields), message: "Cannot set an embed builder's fields collection to null.");
if (value.Count > MaxFieldCount)
throw new ArgumentException(message: $"Field count must be less than or equal to {MaxFieldCount}.", paramName: nameof(Fields));
_fields = value;
}
}
@@ -470,7 +474,7 @@ namespace Discord
if (!string.IsNullOrEmpty(Author.IconUrl))
UrlValidation.Validate(Author.IconUrl, true);
}
if(Footer != null)
if(Footer != null)
{
if (!string.IsNullOrEmpty(Footer.IconUrl))
UrlValidation.Validate(Footer.IconUrl, true);
@@ -564,8 +568,10 @@ namespace Discord
get => _name;
set
{
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException(message: "Field name must not be null, empty or entirely whitespace.", paramName: nameof(Name));
if (value.Length > MaxFieldNameLength) throw new ArgumentException(message: $"Field name length must be less than or equal to {MaxFieldNameLength}.", paramName: nameof(Name));
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException(message: "Field name must not be null, empty or entirely whitespace.", paramName: nameof(Name));
if (value.Length > MaxFieldNameLength)
throw new ArgumentException(message: $"Field name length must be less than or equal to {MaxFieldNameLength}.", paramName: nameof(Name));
_name = value;
}
}
@@ -587,8 +593,10 @@ namespace Discord
set
{
var stringValue = value?.ToString();
if (string.IsNullOrWhiteSpace(stringValue)) throw new ArgumentException(message: "Field value must not be null or empty.", paramName: nameof(Value));
if (stringValue.Length > MaxFieldValueLength) throw new ArgumentException(message: $"Field value length must be less than or equal to {MaxFieldValueLength}.", paramName: nameof(Value));
if (string.IsNullOrWhiteSpace(stringValue))
throw new ArgumentException(message: "Field value must not be null or empty.", paramName: nameof(Value));
if (stringValue.Length > MaxFieldValueLength)
throw new ArgumentException(message: $"Field value length must be less than or equal to {MaxFieldValueLength}.", paramName: nameof(Value));
_value = stringValue;
}
}
@@ -704,7 +712,8 @@ namespace Discord
get => _name;
set
{
if (value?.Length > MaxAuthorNameLength) throw new ArgumentException(message: $"Author name length must be less than or equal to {MaxAuthorNameLength}.", paramName: nameof(Name));
if (value?.Length > MaxAuthorNameLength)
throw new ArgumentException(message: $"Author name length must be less than or equal to {MaxAuthorNameLength}.", paramName: nameof(Name));
_name = value;
}
}
@@ -836,7 +845,8 @@ namespace Discord
get => _text;
set
{
if (value?.Length > MaxFooterTextLength) throw new ArgumentException(message: $"Footer text length must be less than or equal to {MaxFooterTextLength}.", paramName: nameof(Text));
if (value?.Length > MaxFooterTextLength)
throw new ArgumentException(message: $"Footer text length must be less than or equal to {MaxFooterTextLength}.", paramName: nameof(Text));
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.