Browse Source

docs

pull/2574/head
Misha133 2 years ago
parent
commit
e509372c72
8 changed files with 101 additions and 0 deletions
  1. +68
    -0
      docs/guides/bearer_token/bearer_token_guide.md
  2. +11
    -0
      docs/guides/bearer_token/samples/app_role_connection.cs
  3. +5
    -0
      docs/guides/bearer_token/samples/current_user.cs
  4. +2
    -0
      docs/guides/bearer_token/samples/current_user_connections.cs
  5. +6
    -0
      docs/guides/bearer_token/samples/current_user_guild_member.cs
  6. +2
    -0
      docs/guides/bearer_token/samples/current_user_guilds.cs
  7. +5
    -0
      docs/guides/bearer_token/samples/rest_client_init.cs
  8. +2
    -0
      docs/guides/toc.yml

+ 68
- 0
docs/guides/bearer_token/bearer_token_guide.md View File

@@ -0,0 +1,68 @@
---
uid: Guides.BearerToken
title: Working with Bearer token
---

# Working with Bearer token

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 endopoints using [DiscordRestClient].

## Initializing a new instance of the client
[!code-csharp[Initialize DiscordRestClient](samples/rest_client_init.cs)]

## Getting current user

[DiscordRestClient] gets the current user when `LoginAsync()` is called. The user object can be found in `CurrentUser` property.

If you need to fetch the user again `GetGetCurrentUserAsync()` method can be used.

[!code-csharp[Get current user](samples/current_user.cs)]

> [!NOTE]
> Some properies might be `null` depending on which scopes user authed your app with.
> For example: `email` scope is required to fetch current user's email address.

## Fetching current user's guilds

`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

`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 conection metadata values. `GetUserApplicationRoleConnectionAsync()` returns a [RoleConnection] object of the current user for the given application id.

`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



[DiscordRestClient]: xref:Discord.Rest.DiscordRestClient
[RestUserGuild]: xref:Discord.Rest.RestUserGuild
[RoleConnection]: xref:Discord.RoleConnection
[RoleConnectionProperties]: xref:Discord.RoleConnectionProperties

+ 11
- 0
docs/guides/bearer_token/samples/app_role_connection.cs View File

@@ -0,0 +1,11 @@
// 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);

+ 5
- 0
docs/guides/bearer_token/samples/current_user.cs View File

@@ -0,0 +1,5 @@
// gets the user object stored in DiscordRestClient.
var user = client.CurrentUser;

// fetches the current user with a REST call & updates the CurrentUser property.
var refreshedUser = await client.GetCurrentUserAsync();

+ 2
- 0
docs/guides/bearer_token/samples/current_user_connections.cs View File

@@ -0,0 +1,2 @@
// fetches current user's connections.
var connections = await client.GetConnectionsAsync();

+ 6
- 0
docs/guides/bearer_token/samples/current_user_guild_member.cs View File

@@ -0,0 +1,6 @@
// 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();

+ 2
- 0
docs/guides/bearer_token/samples/current_user_guilds.cs View File

@@ -0,0 +1,2 @@
// fetches the guilds the current user participate in.
var guilds = await client.GetGuildSummariesAsync().FlattenAsync();

+ 5
- 0
docs/guides/bearer_token/samples/rest_client_init.cs View File

@@ -0,0 +1,5 @@
using Discord;
using Discord.Rest;

await using var client = new DiscordRestClient();
await client.LoginAsync(TokenType.Bearer, "bearer token obtained through oauth2 flow");

+ 2
- 0
docs/guides/toc.yml View File

@@ -130,3 +130,5 @@
topicUid: Guides.Voice.SendingVoice topicUid: Guides.Voice.SendingVoice
- name: Deployment - name: Deployment
topicUid: Guides.Deployment topicUid: Guides.Deployment
- name: Working with Bearer token
topicUid: Guides.BearerToken

Loading…
Cancel
Save