Skip to content

Commit

Permalink
feat: add methods to add a user to a guild with an access token
Browse files Browse the repository at this point in the history
DiscordUser.OAuth2AddToGuildAsync
DiscordOAuth2Client.AddCurrentUserToGuildAsync
  • Loading branch information
Lulalaby committed Jan 21, 2024
1 parent f3042a4 commit 932a181
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 111 deletions.
29 changes: 29 additions & 0 deletions DisCatSharp/Clients/DiscordOAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,35 @@ public Task<IReadOnlyList<DiscordGuild>> GetCurrentUserGuildsAsync(DiscordAccess
public Task<DiscordMember> GetCurrentUserGuildMemberAsync(DiscordAccessToken accessToken, ulong guildId)
=> accessToken.Scope.Split(' ').Any(x => x == "guilds.members.read") ? this.ApiClient.GetCurrentUserGuildMemberAsync(accessToken.AccessToken, guildId) : throw new AccessViolationException("Access token does not include guilds.members.read scope");

/// <summary>
/// <para>Adds the current user to the given <paramref name="guildId"/>.</para>
/// <para>Some parameters might need additional permissions for the bot on the target guild. See https://discord.com/developers/docs/resources/guild#add-guild-member for details.</para>
/// <para>This methods invokes a sub-request to <see cref="GetCurrentUserAsync"/>.</para>
/// </summary>
/// <param name="accessToken">The discord access token.</param>
/// <param name="guildId">The guild id to add the member to.</param>
/// <param name="nickname">The new nickname.</param>
/// <param name="roles">The new roles.</param>
/// <param name="muted">Whether this user has to be muted.</param>
/// <param name="deafened">Whether this user has to be deafened.</param>
public async Task<DiscordMember> AddCurrentUserToGuildAsync(DiscordAccessToken accessToken, ulong guildId, string? nickname = null, IEnumerable<DiscordRole>? roles = null, bool? muted = null, bool? deafened = null)
=> accessToken.Scope.Split(' ').Any(x => x == "guilds.join") ? await this.ApiClient.AddGuildMemberAsync(guildId, (await this.GetCurrentUserAsync(accessToken)).Id, accessToken.AccessToken, nickname, roles, muted, deafened) : throw new AccessViolationException("Access token does not include guilds.join scope");

/// <summary>
/// <para>Adds the given <paramref name="userId"/> to the given <paramref name="guildId"/>.</para>
/// <para>Some parameters might need additional permissions for the bot on the target guild. See https://discord.com/developers/docs/resources/guild#add-guild-member for details.</para>
/// <para>This methods does not invoke a sub-request to <see cref="GetCurrentUserAsync"/>.</para>
/// </summary>
/// <param name="accessToken">The discord access token.</param>
/// <param name="userId">The user id to add.</param>
/// <param name="guildId">The guild id to add the member to.</param>
/// <param name="nickname">The new nickname.</param>
/// <param name="roles">The new roles.</param>
/// <param name="muted">Whether this user has to be muted.</param>
/// <param name="deafened">Whether this user has to be deafened.</param>
public async Task<DiscordMember> AddCurrentUserToGuildAsync(DiscordAccessToken accessToken, ulong userId, ulong guildId, string? nickname = null, IEnumerable<DiscordRole>? roles = null, bool? muted = null, bool? deafened = null)
=> accessToken.Scope.Split(' ').Any(x => x == "guilds.join") ? await this.ApiClient.AddGuildMemberAsync(guildId, userId, accessToken.AccessToken, nickname, roles, muted, deafened) : throw new AccessViolationException("Access token does not include guilds.join scope");

/// <summary>
/// Gets the current user's application role connection.
/// </summary>
Expand Down
20 changes: 10 additions & 10 deletions DisCatSharp/Entities/Guild/DiscordGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,23 +749,23 @@ public Task<IReadOnlyList<DiscordMember>> SearchMembersAsync(string name, int? l
/// <summary>
/// Adds a new member to this guild
/// </summary>
/// <param name="user">User to add</param>
/// <param name="accessToken">User's access token (OAuth2)</param>
/// <param name="nickname">new nickname</param>
/// <param name="roles">new roles</param>
/// <param name="muted">whether this user has to be muted</param>
/// <param name="deaf">whether this user has to be deafened</param>
/// <param name="user">The user to add.</param>
/// <param name="accessToken">The user's access token (OAuth2).</param>
/// <param name="nickname">The new nickname.</param>
/// <param name="roles">The new roles.</param>
/// <param name="muted">Whether this user has to be muted.</param>
/// <param name="deaf">Whether this user has to be deafened.</param>
/// <exception cref="UnauthorizedException">Thrown when the client does not have the <see cref="Permissions.CreateInstantInvite" /> permission.</exception>
/// <exception cref="NotFoundException">Thrown when the <paramref name="user"/> or <paramref name="accessToken"/> is not found.</exception>
/// <exception cref="BadRequestException">Thrown when an invalid parameter was provided.</exception>
/// <exception cref="ServerErrorException">Thrown when Discord is unable to process the request.</exception>
public Task AddMemberAsync(
DiscordUser user,
string accessToken,
string nickname = null,
IEnumerable<DiscordRole> roles = null,
bool muted = false,
bool deaf = false
string? nickname = null,
IEnumerable<DiscordRole>? roles = null,
bool? muted = null,
bool? deaf = null
)
=> this.Discord.ApiClient.AddGuildMemberAsync(this.Id, user.Id, accessToken, nickname, roles, muted, deaf);

Expand Down
Loading

0 comments on commit 932a181

Please sign in to comment.