public override async ValueTask <TypeReaderResponse> Read(IMessageContext context, string input) { if (context.Guild != null) { var results = new Dictionary <ulong, TypeReaderValue>(); var channels = await context.Guild.GetChannelsAsync(); ulong id; //By Mention (1.0) if (MentionUtils.TryParseChannel(input, out id)) { AddResult(results, channels.Where(c => c.Id == id).FirstOrDefault() as T, 1.0f); } //By Id (0.9) if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) { AddResult(results, channels.Where(c => c.Id == id).FirstOrDefault() as T, 0.90f); } //By Name (0.7-0.8) foreach (var channel in channels.Where(x => string.Equals(input, x.Name, StringComparison.OrdinalIgnoreCase))) { AddResult(results, channel as T, channel.Name == input ? 0.80f : 0.70f); } if (results.Count > 0) { return(TypeReaderResponse.FromSuccess(results.Values)); } } return(TypeReaderResponse.FromError(TYPEREADER_ENTITY_NOTFOUND, input, typeof(T))); }
public override ValueTask <TypeReaderResponse> Read(IMessageContext context, string input) { if (context.Guild != null) { var results = new Dictionary <ulong, TypeReaderValue>(); var roles = context.Guild.Roles; //By Mention (1.0) if (MentionUtils.TryParseRole(input, out ulong id)) { AddResult(results, context.Guild.GetRole(id) as T, 1.00f); } //By Id (0.9) if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) { AddResult(results, context.Guild.GetRole(id) as T, 0.90f); } //By Name (0.7-0.8) foreach (var role in roles.Where(x => string.Equals(input, x.Name, StringComparison.OrdinalIgnoreCase))) { AddResult(results, role as T, role.Name == input ? 0.80f : 0.70f); } if (results.Count > 0) { return(ValueTask.FromResult(TypeReaderResponse.FromSuccess(results.Values))); } } return(ValueTask.FromResult(TypeReaderResponse.FromError(TYPEREADER_ENTITY_NOTFOUND, input, typeof(T)))); }
public override ValueTask <TypeReaderResponse> Read(IMessageContext context, string value) { if (context.TextManager.GetLanguageCoverage(value) > 0) { return(ValueTask.FromResult(TypeReaderResponse.FromSuccess((Locale)value))); } return(ValueTask.FromResult(TypeReaderResponse.FromError(TYPEREADER_UNABLETOREAD, value, typeof(Locale)))); }
public override ValueTask <TypeReaderResponse> Read(IMessageContext context, string value) { foreach (var format in context.Formatter.KnownFormats) { if (format.Localise(context.TextResource).ToUpper() == value.ToUpper()) { return(ValueTask.FromResult(TypeReaderResponse.FromSuccess(format))); } } return(ValueTask.FromResult(TypeReaderResponse.FromError(TYPEREADER_UNABLETOREAD, value, typeof(FormatType)))); }
public override async ValueTask <TypeReaderResponse> Read(IMessageContext context, string value) { if (ulong.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out ulong id)) { if (await context.Channel.GetMessageAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) is T msg) { return(TypeReaderResponse.FromSuccess(msg)); } } return(TypeReaderResponse.FromError(TYPEREADER_ENTITY_NOTFOUND, value, typeof(T))); }
public override async ValueTask <TypeReaderResponse> Read(IMessageContext context, string input) { var results = new Dictionary <ulong, TypeReaderValue>(); IReadOnlyCollection <IUser> channelUsers = (await context.Channel.GetUsersAsync(CacheMode.CacheOnly).Flatten().ConfigureAwait(false)).ToArray(); //TODO: must be a better way? IReadOnlyCollection <IGuildUser> guildUsers = ImmutableArray.Create <IGuildUser>(); if (context.Guild != null) { guildUsers = await context.Guild.GetUsersAsync(); } //By Mention (1.0) if (MentionUtils.TryParseUser(input, out ulong id)) { if (context.Guild != null) { AddResult(results, guildUsers.FirstOrDefault(u => u.Id == id) as T, 1.00f); } else { AddResult(results, await context.Channel.GetUserAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 1.00f); } AddResult(results, context.Client.GetUser(id) as T, 0.2f); } //By Id (0.9) if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) { if (context.Guild != null) { AddResult(results, guildUsers.FirstOrDefault(u => u.Id == id) as T, 0.90f); } else { AddResult(results, await context.Channel.GetUserAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 0.90f); } AddResult(results, context.Client.GetUser(id) as T, 0.1f); } //By Username + Discriminator (0.7-0.85) int index = input.LastIndexOf('#'); if (index >= 0) { string username = input.Substring(0, index); ushort discriminator; if (ushort.TryParse(input.Substring(index + 1), out discriminator)) { var channelUser = channelUsers.FirstOrDefault(x => x.DiscriminatorValue == discriminator && string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)); AddResult(results, channelUser as T, channelUser?.Username == username ? 0.85f : 0.75f); var guildUser = guildUsers.FirstOrDefault(x => x.DiscriminatorValue == discriminator && string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)); AddResult(results, guildUser as T, guildUser?.Username == username ? 0.80f : 0.70f); } } //By Username (0.5-0.6) { foreach (var channelUser in channelUsers.Where(x => string.Equals(input, x.Username, StringComparison.OrdinalIgnoreCase))) { AddResult(results, channelUser as T, channelUser.Username == input ? 0.65f : 0.55f); } foreach (var guildUser in guildUsers.Where(x => string.Equals(input, x.Username, StringComparison.OrdinalIgnoreCase))) { AddResult(results, guildUser as T, guildUser.Username == input ? 0.60f : 0.50f); } } //By Nickname (0.5-0.6) { foreach (var channelUser in channelUsers.Where(x => string.Equals(input, (x as IGuildUser)?.Nickname, StringComparison.OrdinalIgnoreCase))) { AddResult(results, channelUser as T, (channelUser as IGuildUser).Nickname == input ? 0.65f : 0.55f); } foreach (var guildUser in guildUsers.Where(x => string.Equals(input, (x as IGuildUser)?.Nickname, StringComparison.OrdinalIgnoreCase))) { AddResult(results, guildUser as T, (guildUser as IGuildUser).Nickname == input ? 0.60f : 0.50f); } } if (results.Count > 0) { return(TypeReaderResponse.FromSuccess(results.Values.ToImmutableArray())); } return(TypeReaderResponse.FromError(TYPEREADER_ENTITY_NOTFOUND, input, typeof(T))); }
public override ValueTask <TypeReaderResponse> Read(IMessageContext context, string value) { return(ValueTask.FromResult(TypeReaderResponse.FromError(null, null, null))); }
public override ValueTask <TypeReaderResponse> Read(IMessageContext context, string input) { var days = 0; var hours = 0; var minutes = 0; var seconds = 0; var match = false; var colon = Regex.Match(input, @"(\d{1,2}([:\.]\d{1,2}){1,4}|^\d+$)"); if (colon.Success) { var matchedString = colon.Value.Split(new char[] { ':', '.' }); match = true; switch (matchedString.Length) { case 1: minutes = int.Parse(matchedString[0]); break; case 2: hours = int.Parse(matchedString[0]); minutes = int.Parse(matchedString[1]); break; case 3: hours = int.Parse(matchedString[0]); minutes = int.Parse(matchedString[1]); seconds = int.Parse(matchedString[2]); break; case 4: days = int.Parse(matchedString[0]); hours = int.Parse(matchedString[1]); minutes = int.Parse(matchedString[2]); seconds = int.Parse(matchedString[3]); break; case 5: days = int.Parse(matchedString[0]); hours = int.Parse(matchedString[1]); minutes = int.Parse(matchedString[2]); seconds = int.Parse(matchedString[3]); break; } } else { var capture = Regex.Match(input, @"\d{1,2} ?(?=\s*(days|day|d))", RegexOptions.IgnoreCase); if (capture.Success) { match = true; days = int.Parse(capture.Value); } capture = Regex.Match(input, @"\d{1,2} ?(?=\s*(hours|hour|hrs|hr|h))", RegexOptions.IgnoreCase); if (capture.Success) { match = true; hours = int.Parse(capture.Value); } capture = Regex.Match(input, @"\d{1,2} ?(?=\s*(minutes|minute|mins|min|m))", RegexOptions.IgnoreCase); if (capture.Success) { match = true; minutes = int.Parse(capture.Value); } capture = Regex.Match(input, @"\d{1,2} ?(?=\s*(seconds|second|secs|sec|s))", RegexOptions.IgnoreCase); if (capture.Success) { match = true; seconds = int.Parse(capture.Value); } } if (match) { return(ValueTask.FromResult(TypeReaderResponse.FromSuccess(new TimeSpan(days, hours, minutes, seconds)))); } else { return(ValueTask.FromResult(TypeReaderResponse.FromError(TYPEREADER_UNABLETOREAD, input, typeof(TimeSpan)))); } }