diff --git a/src/Discord.Net/DiscordClient.Channels.cs b/src/Discord.Net/DiscordClient.Channels.cs index e2ff289da..52ce846ec 100644 --- a/src/Discord.Net/DiscordClient.Channels.cs +++ b/src/Discord.Net/DiscordClient.Channels.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net; @@ -8,8 +9,28 @@ namespace Discord { internal sealed class Channels : AsyncCollection { + public IEnumerable PrivateChannels => _privateChannels.Select(x => x.Value); + private ConcurrentDictionary _privateChannels; + public Channels(DiscordClient client, object writerLock) - : base(client, writerLock) { } + : base(client, writerLock) + { + _privateChannels = new ConcurrentDictionary(); + ItemCreated += (s, e) => + { + if (e.Item.IsPrivate) + _privateChannels.TryAdd(e.Item.Id, e.Item); + }; + ItemDestroyed += (s, e) => + { + if (e.Item.IsPrivate) + { + Channel ignored; + _privateChannels.TryRemove(e.Item.Id, out ignored); + } + }; + Cleared += (s, e) => _privateChannels.Clear(); + } public Channel GetOrAdd(string id, string serverId, string recipientId = null) => GetOrAdd(id, () => new Channel(_client, id, serverId, recipientId)); @@ -25,9 +46,6 @@ namespace Discord public partial class DiscordClient { - internal Channels Channels => _channels; - private readonly Channels _channels; - public event EventHandler ChannelCreated; private void RaiseChannelCreated(Channel channel) { @@ -47,6 +65,11 @@ namespace Discord RaiseEvent(nameof(ChannelUpdated), () => ChannelUpdated(this, new ChannelEventArgs(channel))); } + /// Returns a collection of all servers this client is a member of. + public IEnumerable PrivateChannels => _channels.PrivateChannels; + internal Channels Channels => _channels; + private readonly Channels _channels; + /// Returns the channel with the specified id, or null if none was found. public Channel GetChannel(string id) {