Browse Source

Added Client.PrivateChannels

tags/docs-0.9
RogueException 9 years ago
parent
commit
d13afaca0e
1 changed files with 27 additions and 4 deletions
  1. +27
    -4
      src/Discord.Net/DiscordClient.Channels.cs

+ 27
- 4
src/Discord.Net/DiscordClient.Channels.cs View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@@ -8,8 +9,28 @@ namespace Discord
{ {
internal sealed class Channels : AsyncCollection<Channel> internal sealed class Channels : AsyncCollection<Channel>
{ {
public IEnumerable<Channel> PrivateChannels => _privateChannels.Select(x => x.Value);
private ConcurrentDictionary<string, Channel> _privateChannels;

public Channels(DiscordClient client, object writerLock) public Channels(DiscordClient client, object writerLock)
: base(client, writerLock) { }
: base(client, writerLock)
{
_privateChannels = new ConcurrentDictionary<string, Channel>();
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) public Channel GetOrAdd(string id, string serverId, string recipientId = null)
=> GetOrAdd(id, () => new Channel(_client, id, serverId, recipientId)); => GetOrAdd(id, () => new Channel(_client, id, serverId, recipientId));
@@ -25,9 +46,6 @@ namespace Discord


public partial class DiscordClient public partial class DiscordClient
{ {
internal Channels Channels => _channels;
private readonly Channels _channels;

public event EventHandler<ChannelEventArgs> ChannelCreated; public event EventHandler<ChannelEventArgs> ChannelCreated;
private void RaiseChannelCreated(Channel channel) private void RaiseChannelCreated(Channel channel)
{ {
@@ -47,6 +65,11 @@ namespace Discord
RaiseEvent(nameof(ChannelUpdated), () => ChannelUpdated(this, new ChannelEventArgs(channel))); RaiseEvent(nameof(ChannelUpdated), () => ChannelUpdated(this, new ChannelEventArgs(channel)));
} }


/// <summary> Returns a collection of all servers this client is a member of. </summary>
public IEnumerable<Channel> PrivateChannels => _channels.PrivateChannels;
internal Channels Channels => _channels;
private readonly Channels _channels;

/// <summary> Returns the channel with the specified id, or null if none was found. </summary> /// <summary> Returns the channel with the specified id, or null if none was found. </summary>
public Channel GetChannel(string id) public Channel GetChannel(string id)
{ {


Loading…
Cancel
Save