Working with Bearer token
Some endpoints in Discord API require a Bearer token, which can be obtained through OAuth2 flow. Discord.Net allows you to interact with these endpoints using the DiscordRestClient.
Initializing a new instance of the client
using Discord;
using Discord.Rest;
await using var client = new DiscordRestClient();
await client.LoginAsync(TokenType.Bearer, "bearer token obtained through oauth2 flow");
Getting current user
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.
// gets the user object stored in the DiscordRestClient.
var user = client.CurrentUser;
// fetches the current user with a REST call & updates the CurrentUser property.
var refreshedUser = await client.GetCurrentUserAsync();
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.
// fetches the guilds the current user participate in.
var guilds = await client.GetGuildSummariesAsync().FlattenAsync();
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.
// fetches the current user's guild member object in a guild with provided id.
var member = await client.GetCurrentUserGuildMemberAsync(guildId);
// fetches the current user's guild member object in a RestUserGuild.
var guild = await client.GetGuildSummariesAsync().FlattenAsync().First();
var member = await guild.GetCurrentUserGuildMemberAsync();
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.
// fetches the current user's connections.
var connections = await client.GetConnectionsAsync();
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.
// fetch application role connection of the current user for the app with provided id.
var roleConnection = await client.GetUserApplicationRoleConnectionAsync(applicationid);
// create a new role connection metadata properties object & set some values.
var properties = new RoleConnectionProperties("Discord.Net Docs", "Cool Coding Guy")
.WithNumber("eaten_cookies", 69)
.WithBool("loves_cookies", true)
.WithDate("last_eaten_cookie", DateTimeOffset.UtcNow);
// update current user's values with the given properties.
await client.ModifyUserApplicationRoleConnectionAsync(applicationId, properties);
Warning
This method requires role_connections.write
scope