Fixed User TypeReader not resolving in DMs

This commit is contained in:
RogueException
2016-06-29 05:27:18 -03:00
parent 142e8484ea
commit 5c5a9c8d6f

View File

@@ -9,48 +9,44 @@ namespace Discord.Commands
{ {
public override async Task<TypeReaderResult> Read(IMessage context, string input) public override async Task<TypeReaderResult> Read(IMessage context, string input)
{ {
IGuildChannel guildChannel = context.Channel as IGuildChannel;
IUser result = null; IUser result = null;
if (guildChannel != null) //By Id
ulong id;
if (MentionUtils.TryParseUser(input, out id) || ulong.TryParse(input, out id))
{ {
//By Id var user = await context.Channel.GetUserAsync(id).ConfigureAwait(false);
ulong id; if (user != null)
if (MentionUtils.TryParseUser(input, out id) || ulong.TryParse(input, out id)) result = user;
{ }
var user = await guildChannel.Guild.GetUserAsync(id).ConfigureAwait(false);
if (user != null)
result = user;
}
//By Username + Discriminator //By Username + Discriminator
if (result == null) if (result == null)
{
int index = input.LastIndexOf('#');
if (index >= 0)
{ {
int index = input.LastIndexOf('#'); string username = input.Substring(0, index);
if (index >= 0) ushort discriminator;
if (ushort.TryParse(input.Substring(index + 1), out discriminator))
{ {
string username = input.Substring(0, index); var users = await context.Channel.GetUsersAsync().ConfigureAwait(false);
ushort discriminator; result = users.Where(x =>
if (ushort.TryParse(input.Substring(index + 1), out discriminator)) x.DiscriminatorValue == discriminator &&
{ string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
var users = await guildChannel.Guild.GetUsersAsync().ConfigureAwait(false);
result = users.Where(x =>
x.DiscriminatorValue == discriminator &&
string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
}
} }
} }
}
//By Username //By Username
if (result == null) if (result == null)
{ {
var users = await guildChannel.Guild.GetUsersAsync().ConfigureAwait(false); var users = await context.Channel.GetUsersAsync().ConfigureAwait(false);
var filteredUsers = users.Where(x => string.Equals(input, x.Username, StringComparison.OrdinalIgnoreCase)).ToArray(); var filteredUsers = users.Where(x => string.Equals(input, x.Username, StringComparison.OrdinalIgnoreCase)).ToArray();
if (filteredUsers.Length > 1) if (filteredUsers.Length > 1)
return TypeReaderResult.FromError(CommandError.MultipleMatches, "Multiple users found."); return TypeReaderResult.FromError(CommandError.MultipleMatches, "Multiple users found.");
else if (filteredUsers.Length == 1) else if (filteredUsers.Length == 1)
result = filteredUsers[0]; result = filteredUsers[0];
}
} }
if (result == null) if (result == null)