| @@ -162,14 +162,14 @@ namespace Discord | |||
| } | |||
| /// <summary> Destroys the provided channel. </summary> | |||
| public async Task<Channel> DestroyChannel(Channel channel) | |||
| public async Task DestroyChannel(Channel channel) | |||
| { | |||
| if (channel == null) throw new ArgumentNullException(nameof(channel)); | |||
| CheckReady(); | |||
| try { await _api.DestroyChannel(channel.Id).ConfigureAwait(false); } | |||
| catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { } | |||
| return _channels.TryRemove(channel.Id); | |||
| //return _channels.TryRemove(channel.Id); | |||
| } | |||
| } | |||
| } | |||
| @@ -161,7 +161,7 @@ namespace Discord | |||
| /// <summary> Edits the provided message, changing only non-null attributes. </summary> | |||
| /// <remarks> While not required, it is recommended to include a mention reference in the text (see Mention.User). </remarks> | |||
| public async Task EditMessage(Message message, string text) | |||
| public Task EditMessage(Message message, string text) | |||
| { | |||
| if (message == null) throw new ArgumentNullException(nameof(message)); | |||
| CheckReady(); | |||
| @@ -169,8 +169,7 @@ namespace Discord | |||
| if (text != null && text.Length > MaxMessageSize) | |||
| text = text.Substring(0, MaxMessageSize); | |||
| var model = await _api.EditMessage(message.Id, message.Channel.Id, text, Mention.GetUserIds(text)).ConfigureAwait(false); | |||
| message.Update(model); | |||
| return _api.EditMessage(message.Id, message.Channel.Id, text, Mention.GetUserIds(text)); | |||
| } | |||
| /// <summary> Deletes the provided message. </summary> | |||
| @@ -219,7 +218,7 @@ namespace Discord | |||
| msg = _messages.GetOrAdd(x.Id, x.ChannelId, x.Author.Id); | |||
| else | |||
| msg = _messages[x.Id] ?? new Message(this, x.Id, x.ChannelId, x.Author.Id); | |||
| msg.Update(x); | |||
| //msg.Update(x); | |||
| if (Config.TrackActivity) | |||
| { | |||
| if (!channel.IsPrivate) | |||
| @@ -39,61 +39,8 @@ namespace Discord | |||
| return SetChannelPermissions(channel, role?.Id, PermissionTarget.Role, permissions?.Allow, permissions?.Deny); | |||
| } | |||
| private async Task SetChannelPermissions(Channel channel, string targetId, PermissionTarget targetType, ChannelPermissions allow = null, ChannelPermissions deny = null) | |||
| { | |||
| uint allowValue = allow?.RawValue ?? 0; | |||
| uint denyValue = deny?.RawValue ?? 0; | |||
| bool changed = false; | |||
| var perms = channel.PermissionOverwrites.Where(x => x.TargetType != targetType || x.TargetId != targetId).FirstOrDefault(); | |||
| if (allowValue != 0 || denyValue != 0) | |||
| { | |||
| await _api.SetChannelPermissions(channel.Id, targetId, targetType.Value, allowValue, denyValue).ConfigureAwait(false); | |||
| if (perms != null) | |||
| { | |||
| perms.Allow.SetRawValueInternal(allowValue); | |||
| perms.Deny.SetRawValueInternal(denyValue); | |||
| } | |||
| else | |||
| { | |||
| var oldPerms = channel.PermissionOverwrites.ToArray(); | |||
| var newPerms = new Channel.PermissionOverwrite[oldPerms.Length + 1]; | |||
| Array.Copy(oldPerms, newPerms, oldPerms.Length); | |||
| newPerms[oldPerms.Length] = new Channel.PermissionOverwrite(targetType, targetId, allowValue, denyValue); | |||
| channel.PermissionOverwrites = newPerms; | |||
| } | |||
| changed = true; | |||
| } | |||
| else | |||
| { | |||
| try | |||
| { | |||
| await _api.DeleteChannelPermissions(channel.Id, targetId).ConfigureAwait(false); | |||
| if (perms != null) | |||
| { | |||
| channel.PermissionOverwrites = channel.PermissionOverwrites.Where(x => x.TargetType != targetType || x.TargetId != targetId).ToArray(); | |||
| changed = true; | |||
| } | |||
| } | |||
| catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { } | |||
| } | |||
| if (changed) | |||
| { | |||
| if (targetType == PermissionTarget.Role) | |||
| { | |||
| var role = _roles[targetId]; | |||
| if (role != null) | |||
| channel.InvalidatePermissionsCache(role); | |||
| } | |||
| else if (targetType == PermissionTarget.User) | |||
| { | |||
| var user = _users[targetId, channel.Server?.Id]; | |||
| if (user != null) | |||
| channel.InvalidatePermissionsCache(user); | |||
| } | |||
| } | |||
| } | |||
| private Task SetChannelPermissions(Channel channel, string targetId, PermissionTarget targetType, ChannelPermissions allow = null, ChannelPermissions deny = null) | |||
| => _api.SetChannelPermissions(channel.Id, targetId, targetType.Value, allow?.RawValue ?? 0, deny?.RawValue ?? 0); | |||
| public Task RemoveChannelUserPermissions(Channel channel, User user) | |||
| { | |||
| @@ -117,22 +64,6 @@ namespace Discord | |||
| { | |||
| var perms = channel.PermissionOverwrites.Where(x => x.TargetType != targetType || x.TargetId != userOrRoleId).FirstOrDefault(); | |||
| await _api.DeleteChannelPermissions(channel.Id, userOrRoleId).ConfigureAwait(false); | |||
| if (perms != null) | |||
| { | |||
| channel.PermissionOverwrites.Where(x => x.TargetType != targetType || x.TargetId != userOrRoleId).ToArray(); | |||
| if (targetType == PermissionTarget.Role) | |||
| { | |||
| var role = _roles[userOrRoleId]; | |||
| channel.InvalidatePermissionsCache(role); | |||
| } | |||
| else if (targetType == PermissionTarget.User) | |||
| { | |||
| var user = _users[userOrRoleId, channel.Server?.Id]; | |||
| if (user != null) | |||
| channel.InvalidatePermissionsCache(user); | |||
| } | |||
| } | |||
| } | |||
| catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { } | |||
| } | |||
| @@ -1,6 +1,7 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Net; | |||
| using System.Threading.Tasks; | |||
| namespace Discord | |||
| @@ -62,17 +63,18 @@ namespace Discord | |||
| if (name == null) throw new ArgumentNullException(nameof(name)); | |||
| CheckReady(); | |||
| if (name.StartsWith("@")) | |||
| /*if (name.StartsWith("@")) | |||
| { | |||
| string name2 = name.Substring(1); | |||
| return _roles.Where(x => x.Server.Id == server.Id && | |||
| string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) || string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase)); | |||
| string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) || | |||
| string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase)); | |||
| } | |||
| else | |||
| { | |||
| {*/ | |||
| return _roles.Where(x => x.Server.Id == server.Id && | |||
| string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); | |||
| } | |||
| //} | |||
| } | |||
| /// <summary> Note: due to current API limitations, the created role cannot be returned. </summary> | |||
| @@ -84,10 +86,10 @@ namespace Discord | |||
| var response = await _api.CreateRole(server.Id).ConfigureAwait(false); | |||
| var role = _roles.GetOrAdd(response.Id, server.Id); | |||
| await _api.EditRole(server.Id, role.Id, name: name).ConfigureAwait(false); | |||
| response.Name = name; | |||
| role.Update(response); | |||
| await EditRole(role, name: name).ConfigureAwait(false); | |||
| return role; | |||
| } | |||
| @@ -128,13 +130,14 @@ namespace Discord | |||
| } | |||
| } | |||
| public Task DeleteRole(Role role) | |||
| public async Task DeleteRole(Role role) | |||
| { | |||
| if (role == null) throw new ArgumentNullException(nameof(role)); | |||
| CheckReady(); | |||
| return _api.DeleteRole(role.Server.Id, role.Id); | |||
| } | |||
| try { await _api.DeleteRole(role.Server.Id, role.Id); } | |||
| catch (HttpException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { } | |||
| } | |||
| public Task ReorderRoles(Server server, IEnumerable<Role> roles, int startPos = 0) | |||
| { | |||
| @@ -104,14 +104,14 @@ namespace Discord | |||
| } | |||
| /// <summary> Leaves the provided server, destroying it if you are the owner. </summary> | |||
| public async Task<Server> LeaveServer(Server server) | |||
| public async Task LeaveServer(Server server) | |||
| { | |||
| if (server == null) throw new ArgumentNullException(nameof(server)); | |||
| CheckReady(); | |||
| try { await _api.LeaveServer(server.Id).ConfigureAwait(false); } | |||
| catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { } | |||
| return _servers.TryRemove(server.Id); | |||
| //return _servers.TryRemove(server.Id); | |||
| } | |||
| } | |||
| } | |||
| @@ -159,9 +159,7 @@ namespace Discord | |||
| string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase)); | |||
| } | |||
| else | |||
| { | |||
| query = server.Members.Where(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); | |||
| } | |||
| if (discriminator != null) | |||
| query = query.Where(x => x.Discriminator == discriminator); | |||
| return query; | |||
| @@ -190,10 +188,10 @@ namespace Discord | |||
| public Task SetStatus(UserStatus status) | |||
| { | |||
| if (status == (string)null) throw new ArgumentNullException(nameof(status)); | |||
| CheckReady(); | |||
| if (status != UserStatus.Online && status != UserStatus.Idle) | |||
| throw new ArgumentException($"Invalid status, must be {UserStatus.Online} or {UserStatus.Idle}"); | |||
| throw new ArgumentException($"Invalid status, must be {UserStatus.Online} or {UserStatus.Idle}", nameof(status)); | |||
| CheckReady(); | |||
| _status = status; | |||
| return SendStatus(); | |||
| } | |||