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)
|
public IEnumerable<Channel> FindChannels(Server server, string name, ChannelType type = null, bool exactMatch = false)
|
||||||
{
|
{
|
||||||
if (server == null) throw new ArgumentNullException(nameof(server));
|
if (server == null) throw new ArgumentNullException(nameof(server));
|
||||||
|
if (name == null) throw new ArgumentNullException(nameof(name));
|
||||||
CheckReady();
|
CheckReady();
|
||||||
|
|
||||||
|
var query = server.Channels.Where(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
IEnumerable<Channel> result;
|
if (!exactMatch && name.Length >= 2)
|
||||||
if (!exactMatch && name.StartsWith("#"))
|
|
||||||
{
|
{
|
||||||
string name2 = name.Substring(1);
|
if (name[0] == '<' && name[1] == '#' && name[name.Length - 1] == '>') //Parse mention
|
||||||
result = _channels.Where(x => x.Server == server &&
|
{
|
||||||
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) ||
|
string id = name.Substring(2, name.Length - 3);
|
||||||
string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase));
|
var channel = _channels[id];
|
||||||
}
|
if (channel != null)
|
||||||
else
|
query = query.Concat(new Channel[] { channel });
|
||||||
{
|
}
|
||||||
result = _channels.Where(x => x.Server == server &&
|
else if (name[0] == '#') //If we somehow get text starting with # but isn't a mention
|
||||||
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
|
{
|
||||||
|
string name2 = name.Substring(1);
|
||||||
|
query = query.Concat(server.Channels.Where(x => string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type != (string)null)
|
if (type != (string)null)
|
||||||
result = result.Where(x => x.Type == type);
|
query = query.Where(x => x.Type == type);
|
||||||
|
return query;
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Creates a new channel with the provided name and type. </summary>
|
/// <summary> Creates a new channel with the provided name and type. </summary>
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ namespace Discord
|
|||||||
return _users[user?.Id, server.Id];
|
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>
|
/// <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)
|
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));
|
if (name == null) throw new ArgumentNullException(nameof(name));
|
||||||
CheckReady();
|
CheckReady();
|
||||||
|
|
||||||
IEnumerable<User> query;
|
return FindUsers(server.Members, server.Id, name, discriminator, exactMatch);
|
||||||
if (!exactMatch && name.StartsWith("@"))
|
}
|
||||||
|
/// <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);
|
if (name[0] == '<' && name[1] == '@' && name[name.Length - 1] == '>') //Parse mention
|
||||||
query = server.Members.Where(x =>
|
{
|
||||||
string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) ||
|
string id = name.Substring(2, name.Length - 3);
|
||||||
string.Equals(x.Name, name2, StringComparison.OrdinalIgnoreCase));
|
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)
|
if (discriminator != null)
|
||||||
query = query.Where(x => x.Discriminator == discriminator);
|
query = query.Where(x => x.Discriminator == discriminator);
|
||||||
return query;
|
return query;
|
||||||
|
|||||||
Reference in New Issue
Block a user