Skip to content

Commit

Permalink
Fix SocketCategoryChannel properties (discord-net#945)
Browse files Browse the repository at this point in the history
* Update Users property for category channels

* Wrong property being used for Channels property

CategoryId is the category that "owns" this channel. That is actually impossible right now for category channels, so it returns null and get all channels wrongly.

* Resolve permissions for category

* Remove spaces

* Small fix for IChannel.GetUsersAsync
  • Loading branch information
SubZero0 authored and FiniteReality committed May 5, 2018
1 parent ca98836 commit bd30494
Showing 1 changed file with 23 additions and 7 deletions.
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
Expand All @@ -15,10 +15,12 @@ namespace Discord.WebSocket
public class SocketCategoryChannel : SocketGuildChannel, ICategoryChannel
{
public override IReadOnlyCollection<SocketGuildUser> Users
=> Guild.Users.Where(x => x.VoiceChannel?.Id == Id).ToImmutableArray();
=> Guild.Users.Where(x => Permissions.GetValue(
Permissions.ResolveChannel(Guild, x, this, Permissions.ResolveGuild(Guild, x)),
ChannelPermission.ViewChannel)).ToImmutableArray();

public IReadOnlyCollection<SocketGuildChannel> Channels
=> Guild.Channels.Where(x => x.CategoryId == CategoryId).ToImmutableArray();
=> Guild.Channels.Where(x => x.CategoryId == Id).ToImmutableArray();

internal SocketCategoryChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
: base(discord, id, guild)
Expand All @@ -31,23 +33,37 @@ internal new static SocketCategoryChannel Create(SocketGuild guild, ClientState
return entity;
}

//Users
public override SocketGuildUser GetUser(ulong id)
{
var user = Guild.GetUser(id);
if (user != null)
{
var guildPerms = Permissions.ResolveGuild(Guild, user);
var channelPerms = Permissions.ResolveChannel(Guild, user, this, guildPerms);
if (Permissions.GetValue(channelPerms, ChannelPermission.ViewChannel))
return user;
}
return null;
}

private string DebuggerDisplay => $"{Name} ({Id}, Category)";
internal new SocketCategoryChannel Clone() => MemberwiseClone() as SocketCategoryChannel;

// IGuildChannel
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
=> Task.FromResult<IGuildUser>(GetUser(id));
Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options)
=> throw new NotSupportedException();
Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options)
=> throw new NotSupportedException();

//IChannel
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable();
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
=> Task.FromResult<IUser>(GetUser(id));
}
}

0 comments on commit bd30494

Please sign in to comment.