FindUsers and FindChannels support mentions, FindUsers has a channel overload, and less bugs.
This commit is contained in:
@@ -84,26 +84,30 @@ namespace Discord
|
||||
public IEnumerable<Channel> FindChannels(Server server, string name, ChannelType type = null, bool exactMatch = false)
|
||||
{
|
||||
if (server == null) throw new ArgumentNullException(nameof(server));
|
||||
if (name == null) throw new ArgumentNullException(nameof(name));
|
||||
CheckReady();
|
||||
|
||||
var query = server.Channels.Where(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
IEnumerable<Channel> result;
|
||||
if (!exactMatch && name.StartsWith("#"))
|
||||
if (!exactMatch && name.Length >= 2)
|
||||
{
|
||||
string name2 = name.Substring(1);
|
||||
result = _channels.Where(x => x.Server == server &&
|
||||
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
else
|
||||
{
|
||||
result = _channels.Where(x => x.Server == server &&
|
||||
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
|
||||
if (name[0] == '<' && name[1] == '#' && name[name.Length - 1] == '>') //Parse mention
|
||||
{
|
||||
string id = name.Substring(2, name.Length - 3);
|
||||
var channel = _channels[id];
|
||||
if (channel != null)
|
||||
query = query.Concat(new Channel[] { channel });
|
||||
}
|
||||
else if (name[0] == '#') //If we somehow get text starting with # but isn't a mention
|
||||
{
|
||||
string name2 = name.Substring(1);
|
||||
query = query.Concat(server.Channels.Where(x => string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
}
|
||||
|
||||
if (type != (string)null)
|
||||
result = result.Where(x => x.Type == type);
|
||||
|
||||
return result;
|
||||
query = query.Where(x => x.Type == type);
|
||||
return query;
|
||||
}
|
||||
|
||||
/// <summary> Creates a new channel with the provided name and type. </summary>
|
||||
|
||||
@@ -141,7 +141,7 @@ namespace Discord
|
||||
return _users[user?.Id, server.Id];
|
||||
}
|
||||
|
||||
/// <summary> Returns all users in with the specified server and name, along with their server-specific data. </summary>
|
||||
/// <summary> Returns all users with the specified server and name, along with their server-specific data. </summary>
|
||||
/// <remarks> Name formats supported: Name and @Name. Search is case-insensitive.</remarks>
|
||||
public IEnumerable<User> FindUsers(Server server, string name, string discriminator = null, bool exactMatch = false)
|
||||
{
|
||||
@@ -149,16 +149,39 @@ namespace Discord
|
||||
if (name == null) throw new ArgumentNullException(nameof(name));
|
||||
CheckReady();
|
||||
|
||||
IEnumerable<User> query;
|
||||
if (!exactMatch && name.StartsWith("@"))
|
||||
return FindUsers(server.Members, server.Id, name, discriminator, exactMatch);
|
||||
}
|
||||
/// <summary> Returns all users with the specified channel and name, along with their server-specific data. </summary>
|
||||
/// <remarks> Name formats supported: Name and @Name. Search is case-insensitive.</remarks>
|
||||
public IEnumerable<User> FindUsers(Channel channel, string name, string discriminator = null, bool exactMatch = false)
|
||||
{
|
||||
if (channel == null) throw new ArgumentNullException(nameof(channel));
|
||||
if (name == null) throw new ArgumentNullException(nameof(name));
|
||||
CheckReady();
|
||||
|
||||
return FindUsers(channel.Members, channel.IsPrivate ? null : channel.Server.Id, name, discriminator, exactMatch);
|
||||
}
|
||||
|
||||
private IEnumerable<User> FindUsers(IEnumerable<User> users, string serverId, string name, string discriminator = null, bool exactMatch = false)
|
||||
{
|
||||
var query = users.Where(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (!exactMatch && name.Length >= 2)
|
||||
{
|
||||
string name2 = name.Substring(1);
|
||||
query = server.Members.Where(x =>
|
||||
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase));
|
||||
if (name[0] == '<' && name[1] == '@' && name[name.Length - 1] == '>') //Parse mention
|
||||
{
|
||||
string id = name.Substring(2, name.Length - 3);
|
||||
var channel = _users[id, serverId];
|
||||
if (channel != null)
|
||||
query = query.Concat(new User[] { channel });
|
||||
}
|
||||
else if (name[0] == '@') //If we somehow get text starting with @ but isn't a mention
|
||||
{
|
||||
string name2 = name.Substring(1);
|
||||
query = query.Concat(users.Where(x => 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;
|
||||
|
||||
Reference in New Issue
Block a user