Skip to content

Commit

Permalink
feature: Add AddGuildUserAsync guild ID extension method. (#1240)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntiTcb authored and foxbot committed May 18, 2019
1 parent 655a006 commit 1356ea9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,34 @@ public static async Task<RestGuildUser> AddGuildUserAsync(IGuild guild, BaseDisc

return model is null ? null : RestGuildUser.Create(client, guild, model);
}

public static async Task AddGuildUserAsync(ulong guildId, BaseDiscordClient client, ulong userId, string accessToken,
Action<AddGuildUserProperties> func, RequestOptions options)
{
var args = new AddGuildUserProperties();
func?.Invoke(args);

if (args.Roles.IsSpecified)
{
var ids = args.Roles.Value.Select(r => r.Id);

if (args.RoleIds.IsSpecified)
args.RoleIds.Value.Concat(ids);
else
args.RoleIds = Optional.Create(ids);
}
var apiArgs = new AddGuildMemberParams
{
AccessToken = accessToken,
Nickname = args.Nickname,
IsDeafened = args.Deaf,
IsMuted = args.Mute,
RoleIds = args.RoleIds.IsSpecified ? args.RoleIds.Value.Distinct().ToArray() : Optional.Create<ulong[]>()
};

await client.ApiClient.AddGuildMemberAsync(guildId, userId, apiArgs, options);
}

public static async Task<RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
ulong id, RequestOptions options)
{
Expand Down
23 changes: 23 additions & 0 deletions src/Discord.Net.Rest/Extensions/ClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;

namespace Discord.Rest.Extensions
{
public static class ClientExtensions
{
/// <summary>
/// Adds a user to the specified guild.
/// </summary>
/// <remarks>
/// This method requires you have an OAuth2 access token for the user, requested with the guilds.join scope, and that the bot have the MANAGE_INVITES permission in the guild.
/// </remarks>
/// <param name="client">The Discord client object.</param>
/// <param name="guildId">The snowflake identifier of the guild.</param>
/// <param name="userId">The snowflake identifier of the user.</param>
/// <param name="accessToken">The OAuth2 access token for the user, requested with the guilds.join scope.</param>
/// <param name="func">The delegate containing the properties to be applied to the user upon being added to the guild.</param>
/// <param name="options">The options to be used when sending the request.</param>
public static Task AddGuildUserAsync(this BaseDiscordClient client, ulong guildId, ulong userId, string accessToken, Action<AddGuildUserProperties> func = null, RequestOptions options = null)
=> GuildHelper.AddGuildUserAsync(guildId, client, userId, accessToken, func, options);
}
}

0 comments on commit 1356ea9

Please sign in to comment.