diff --git a/src/Discord.Net/DiscordClient.API.cs b/src/Discord.Net/DiscordClient.API.cs index a4a100614..c110ed5ce 100644 --- a/src/Discord.Net/DiscordClient.API.cs +++ b/src/Discord.Net/DiscordClient.API.cs @@ -423,6 +423,67 @@ namespace Discord return null; } + //Permissions + public Task SetChannelUserPermissions(Channel channel, User user, PackedPermissions allow, PackedPermissions deny) + => SetChannelPermissions(channel?.Id, user?.Id, "member", allow, deny); + public Task SetChannelUserPermissions(string channelId, User user, PackedPermissions allow, PackedPermissions deny) + => SetChannelPermissions(channelId, user?.Id, "member", allow, deny); + public Task SetChannelUserPermissions(Channel channel, string userId, PackedPermissions allow, PackedPermissions deny) + => SetChannelPermissions(channel?.Id, userId, "member", allow, deny); + public Task SetChannelUserPermissions(string channelId, string userId, PackedPermissions allow, PackedPermissions deny) + => SetChannelPermissions(channelId, userId, "member", allow, deny); + + public Task SetChannelRolePermissions(Channel channel, Role role, PackedPermissions allow, PackedPermissions deny) + => SetChannelPermissions(channel?.Id, role?.Id, "role", allow, deny); + public Task SetChannelRolePermissions(string channelId, Role role, PackedPermissions allow, PackedPermissions deny) + => SetChannelPermissions(channelId, role?.Id, "role", allow, deny); + public Task SetChannelRolePermissions(Channel channel, string userId, PackedPermissions allow, PackedPermissions deny) + => SetChannelPermissions(channel?.Id, userId, "role", allow, deny); + public Task SetChannelRolePermissions(string channelId, string userId, PackedPermissions allow, PackedPermissions deny) + => SetChannelPermissions(channelId, userId, "role", allow, deny); + + private Task SetChannelPermissions(string channelId, string userOrRoleId, string idType, PackedPermissions allow, PackedPermissions deny) + { + CheckReady(); + if (channelId == null) throw new NullReferenceException(nameof(channelId)); + if (userOrRoleId == null) throw new NullReferenceException(nameof(userOrRoleId)); + + return _api.SetChannelPermissions(channelId, userOrRoleId, idType, allow, deny); + //TODO: Remove permission from cache + } + + public Task RemoveChannelUserPermissions(Channel channel, User user) + => RemoveChannelPermissions(channel?.Id, user?.Id); + public Task RemoveChannelUserPermissions(string channelId, User user) + => RemoveChannelPermissions(channelId, user?.Id); + public Task RemoveChannelPermissions(Channel channel, string userId) + => RemoveChannelUserPermissions(channel?.Id, userId); + public Task RemoveChannelUserPermissions(string channelId, string userId) + => RemoveChannelPermissions(channelId, userId); + + public Task RemoveChannelRolePermissions(Channel channel, Role role) + => RemoveChannelPermissions(channel?.Id, role?.Id); + public Task RemoveChannelRolePermissions(string channelId, Role role) + => RemoveChannelPermissions(channelId, role?.Id); + public Task RemoveChannelRolePermissions(Channel channel, string roleId) + => RemoveChannelUserPermissions(channel?.Id, roleId); + public Task RemoveChannelRolePermissions(string channelId, string roleId) + => RemoveChannelPermissions(channelId, roleId); + + private async Task RemoveChannelPermissions(string channelId, string userOrRoleId) + { + CheckReady(); + if (channelId == null) throw new NullReferenceException(nameof(channelId)); + if (userOrRoleId == null) throw new NullReferenceException(nameof(userOrRoleId)); + + try + { + await _api.DeleteChannelPermissions(channelId, userOrRoleId).ConfigureAwait(false); + //TODO: Remove permission from cache + } + catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { } + } + //Voice /// Mutes a user on the provided server. public Task Mute(Server server, User user) diff --git a/src/Discord.Net/Net/API/DiscordAPIClient.cs b/src/Discord.Net/Net/API/DiscordAPIClient.cs index 3e73ce40e..63991c872 100644 --- a/src/Discord.Net/Net/API/DiscordAPIClient.cs +++ b/src/Discord.Net/Net/API/DiscordAPIClient.cs @@ -154,5 +154,16 @@ namespace Discord.Net.API var request = new Requests.ChangeAvatar { Avatar = $"data:{type},/9j/{base64}", CurrentEmail = currentEmail, CurrentPassword = currentPassword }; return _rest.Patch(Endpoints.UserMe, request); } + + //Permissions + public Task SetChannelPermissions(string channelId, string userOrRoleId, string idType, PackedPermissions allow, PackedPermissions deny) + { + var request = new Requests.SetChannelPermissions { Id = userOrRoleId, Type = idType, Allow = allow.RawValue, Deny = deny.RawValue }; + return _rest.Put(Endpoints.ChannelPermission(channelId, userOrRoleId), request); + } + public Task DeleteChannelPermissions(string channelId, string userOrRoleId) + { + return _rest.Delete(Endpoints.ChannelPermission(channelId, userOrRoleId), null); + } } } diff --git a/src/Discord.Net/Net/API/Endpoints.cs b/src/Discord.Net/Net/API/Endpoints.cs index eab668d0d..1861d0fbf 100644 --- a/src/Discord.Net/Net/API/Endpoints.cs +++ b/src/Discord.Net/Net/API/Endpoints.cs @@ -19,7 +19,8 @@ public static string ChannelMessages(string channelId, int limit) => $"channels/{channelId}/messages?limit={limit}"; public static string ChannelMessage(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}"; public static string ChannelInvites(string channelId) => $"channels/{channelId}/invites"; - + public static string ChannelPermission(string channelId, string userOrRoleId) => $"channels/{channelId}/permissions/{userOrRoleId}"; + public const string Servers = "guilds"; public static string Server(string serverId) => $"guilds/{serverId}"; public static string ServerChannels(string serverId) => $"guilds/{serverId}/channels"; diff --git a/src/Discord.Net/Net/API/Requests.cs b/src/Discord.Net/Net/API/Requests.cs index 47d798084..a9f75bfa1 100644 --- a/src/Discord.Net/Net/API/Requests.cs +++ b/src/Discord.Net/Net/API/Requests.cs @@ -125,5 +125,18 @@ namespace Discord.Net.API [JsonProperty(PropertyName = "avatar")] public string Avatar; } + + //Permissions + public sealed class SetChannelPermissions + { + [JsonProperty(PropertyName = "id")] + public string Id; + [JsonProperty(PropertyName = "type")] + public string Type; + [JsonProperty(PropertyName = "allow")] + public uint Allow; + [JsonProperty(PropertyName = "deny")] + public uint Deny; + } } }