diff --git a/src/Kook.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Kook.Net.Rest/Entities/Channels/ChannelHelper.cs index afe2c0d8..475b0b08 100644 --- a/src/Kook.Net.Rest/Entities/Channels/ChannelHelper.cs +++ b/src/Kook.Net.Rest/Entities/Channels/ChannelHelper.cs @@ -514,7 +514,9 @@ public static async Task ModifyPermissionOverwriteAsync IGuildUser user, Func func, RequestOptions options) { OverwritePermissions? perms = channel.UserPermissionOverwrites.SingleOrDefault(x => x.Target.Id == user.Id)?.Permissions; - if (!perms.HasValue) return null; + if (!perms.HasValue) + throw new ArgumentNullException(nameof(user), + "The user does not have any permission overwrites on this channel."); perms = func(perms.Value); ModifyChannelPermissionOverwriteParams args = new(channel.Id, PermissionOverwriteTargetType.User, @@ -530,7 +532,9 @@ public static async Task ModifyPermissionOverwriteAsync IRole role, Func func, RequestOptions options) { OverwritePermissions? perms = channel.RolePermissionOverwrites.SingleOrDefault(x => x.Target == role.Id)?.Permissions; - if (!perms.HasValue) return null; + if (!perms.HasValue) + throw new ArgumentNullException(nameof(role), + "The role does not have any permission overwrites on this channel."); perms = func(perms.Value); ModifyChannelPermissionOverwriteParams args = new(channel.Id, PermissionOverwriteTargetType.Role, diff --git a/src/Kook.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs b/src/Kook.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs index 3cb41443..cae91948 100644 --- a/src/Kook.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs +++ b/src/Kook.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs @@ -170,8 +170,11 @@ public Task DeleteAsync(RequestOptions options = null) /// /// A task representing the asynchronous permission operation for adding the specified permissions to the channel. /// - public async Task AddPermissionOverwriteAsync(IGuildUser user, RequestOptions options = null) => - await ChannelHelper.AddPermissionOverwriteAsync(this, Kook, user, options).ConfigureAwait(false); + public async Task AddPermissionOverwriteAsync(IGuildUser user, RequestOptions options = null) + { + UserPermissionOverwrite perms = await ChannelHelper.AddPermissionOverwriteAsync(this, Kook, user, options).ConfigureAwait(false); + _userPermissionOverwrites = _userPermissionOverwrites.Add(perms); + } /// /// Adds or updates the permission overwrite for the given role. @@ -181,8 +184,11 @@ public async Task AddPermissionOverwriteAsync(IGuildUser user, RequestOptions op /// /// A task representing the asynchronous permission operation for adding the specified permissions to the channel. /// - public async Task AddPermissionOverwriteAsync(IRole role, RequestOptions options = null) => - await ChannelHelper.AddPermissionOverwriteAsync(this, Kook, role, options).ConfigureAwait(false); + public async Task AddPermissionOverwriteAsync(IRole role, RequestOptions options = null) + { + RolePermissionOverwrite perms = await ChannelHelper.AddPermissionOverwriteAsync(this, Kook, role, options).ConfigureAwait(false); + _rolePermissionOverwrites = _rolePermissionOverwrites.Add(perms); + } /// /// Removes the permission overwrite for the given user, if one exists. @@ -192,9 +198,18 @@ public async Task AddPermissionOverwriteAsync(IRole role, RequestOptions options /// /// A task representing the asynchronous operation for removing the specified permissions from the channel. /// - public async Task RemovePermissionOverwriteAsync(IGuildUser user, RequestOptions options = null) => + public async Task RemovePermissionOverwriteAsync(IGuildUser user, RequestOptions options = null) + { await ChannelHelper.RemovePermissionOverwriteAsync(this, Kook, user, options).ConfigureAwait(false); + for (int i = 0; i < _userPermissionOverwrites.Length; i++) + if (_userPermissionOverwrites[i].Target.Id == user.Id) + { + _userPermissionOverwrites = _userPermissionOverwrites.RemoveAt(i); + return; + } + } + /// /// Removes the permission overwrite for the given role, if one exists. /// @@ -203,9 +218,18 @@ public async Task RemovePermissionOverwriteAsync(IGuildUser user, RequestOptions /// /// A task representing the asynchronous operation for removing the specified permissions from the channel. /// - public async Task RemovePermissionOverwriteAsync(IRole role, RequestOptions options = null) => + public async Task RemovePermissionOverwriteAsync(IRole role, RequestOptions options = null) + { await ChannelHelper.RemovePermissionOverwriteAsync(this, Kook, role, options).ConfigureAwait(false); + for (int i = 0; i < _rolePermissionOverwrites.Length; i++) + if (_rolePermissionOverwrites[i].Target == role.Id) + { + _rolePermissionOverwrites = _rolePermissionOverwrites.RemoveAt(i); + return; + } + } + /// /// Updates the permission overwrite for the given user, if one exists. /// @@ -215,9 +239,18 @@ public async Task RemovePermissionOverwriteAsync(IRole role, RequestOptions opti /// /// A task representing the asynchronous operation for removing the specified permissions from the channel. /// - public async Task ModifyPermissionOverwriteAsync(IGuildUser user, Func func, - RequestOptions options = null) => - await ChannelHelper.ModifyPermissionOverwriteAsync(this, Kook, user, func, options).ConfigureAwait(false); + public async Task ModifyPermissionOverwriteAsync(IGuildUser user, Func func, RequestOptions options = null) + { + UserPermissionOverwrite perms = await ChannelHelper.ModifyPermissionOverwriteAsync(this, Kook, user, func, options).ConfigureAwait(false); + + for (int i = 0; i < _userPermissionOverwrites.Length; i++) + if (_userPermissionOverwrites[i].Target.Id == user.Id) + { + _userPermissionOverwrites = _userPermissionOverwrites.RemoveAt(i); + _userPermissionOverwrites = _userPermissionOverwrites.Add(perms); + return; + } + } /// /// Updates the permission overwrite for the given role, if one exists. @@ -228,9 +261,18 @@ public async Task ModifyPermissionOverwriteAsync(IGuildUser user, Func /// A task representing the asynchronous operation for removing the specified permissions from the channel. /// - public async Task - ModifyPermissionOverwriteAsync(IRole role, Func func, RequestOptions options = null) => - await ChannelHelper.ModifyPermissionOverwriteAsync(this, Kook, role, func, options).ConfigureAwait(false); + public async Task ModifyPermissionOverwriteAsync(IRole role, Func func, RequestOptions options = null) + { + RolePermissionOverwrite perms = await ChannelHelper.ModifyPermissionOverwriteAsync(this, Kook, role, func, options).ConfigureAwait(false); + + for (int i = 0; i < _rolePermissionOverwrites.Length; i++) + if (_rolePermissionOverwrites[i].Target == role.Id) + { + _rolePermissionOverwrites = _rolePermissionOverwrites.RemoveAt(i); + _rolePermissionOverwrites = _rolePermissionOverwrites.Add(perms); + return; + } + } /// /// Gets a from this channel.