Refactor all the things!

This commit is contained in:
RogueException
2015-09-13 11:46:26 -03:00
parent 1d920a7e6a
commit 4529ad3d4a
70 changed files with 3105 additions and 3094 deletions

View File

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Discord.Collections
{
public sealed class Users : AsyncCollection<User>
{
internal Users(DiscordClient client)
: base(client) { }
internal User GetOrAdd(string id) => GetOrAdd(id, () => new User(_client, id));
internal new User TryRemove(string id) => base.TryRemove(id);
protected override void OnCreated(User item) { }
protected override void OnRemoved(User item) { }
public User this[string id] => Get(id);
public User this[string name, string discriminator]
{
get
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (discriminator == null) throw new ArgumentNullException(nameof(discriminator));
if (name.StartsWith("@"))
name = name.Substring(1);
return this.Where(x =>
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) &&
x.Discriminator == discriminator
)
.FirstOrDefault();
}
}
public IEnumerable<User> Find(string name)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (name.StartsWith("@"))
{
string name2 = name.Substring(1);
return this.Where(x =>
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) || string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase));
}
else
{
return this.Where(x =>
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
}
}
}
}