public override Task <TypeReaderResult> Read(ICommandContext context, string input) { ulong id; if (context.Guild != null) { var results = new Dictionary <ulong, TypeReaderValue>(); var roles = context.Guild.Roles; //By Mention (1.0) if (MentionUtils.TryParseRole(input, out 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(Task.FromResult(TypeReaderResult.FromSuccess(results.Values.ToReadOnlyCollection()))); } } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Role not found."))); }
/// <inheritdoc /> public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (string.IsNullOrEmpty(input)) { throw new ArgumentException(message: $"{nameof(input)} must not be null or empty.", paramName: nameof(input)); } var isNegative = input[0] == '-'; // Char for CultureInfo.InvariantCulture.NumberFormat.NegativeSign if (isNegative) { input = input.Substring(1); } if (TimeSpan.TryParseExact(input.ToLowerInvariant(), Formats, CultureInfo.InvariantCulture, out var timeSpan)) { return(isNegative ? Task.FromResult(TypeReaderResult.FromSuccess(-timeSpan)) : Task.FromResult(TypeReaderResult.FromSuccess(timeSpan))); } else { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Failed to parse TimeSpan"))); } }
public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (context.Guild != null) { var results = new Dictionary <ulong, TypeReaderValue>(); var channels = await context.Guild.GetChannelsAsync(CacheMode.CacheOnly).ConfigureAwait(false); ulong id; //By Mention (1.0) if (MentionUtils.TryParseChannel(input, out id)) { AddResult(results, await context.Guild.GetChannelAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 1.00f); } //By Id (0.9) if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) { AddResult(results, await context.Guild.GetChannelAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) 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(TypeReaderResult.FromSuccess(results.Values.ToReadOnlyCollection())); } } return(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Channel not found.")); }
public override Task <TypeReaderResult> Read(IUserMessage context, string input) { T value; if (_tryParse(input, out value)) { return(Task.FromResult(TypeReaderResult.FromSuccess(value))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, $"Failed to parse {typeof(T).Name}"))); }
/// <inheritdoc /> public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (TimeSpan.TryParse(input, out var time)) { return(Task.FromResult(TypeReaderResult.FromSuccess(time))); } return((TimeSpan.TryParseExact(input.ToLowerInvariant(), Formats, CultureInfo.InvariantCulture, out var timeSpan)) ? Task.FromResult(TypeReaderResult.FromSuccess(timeSpan)) : Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Failed to parse TimeSpan"))); }
/// <inheritdoc /> public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { //By Id (1.0) if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out ulong id)) { if (await context.Channel.GetMessageAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) is T msg) { return(TypeReaderResult.FromSuccess(msg)); } } return(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Message not found.")); }
public override async Task <TypeReaderResult> ReadAsync( ICommandContext context, string input, IServiceProvider services) { var result = await base.ReadAsync(context, input, services); if (result.IsSuccess) { return(result); } else { DiscordRestClient restClient = (context.Client as DiscordSocketClient).Rest; if (MentionUtils.TryParseUser(input, out var id)) { RestUser user = await restClient.GetUserAsync(id); if (user != null) { return(TypeReaderResult.FromSuccess(user)); } } if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) { RestUser user = await restClient.GetUserAsync(id); if (user != null) { return(TypeReaderResult.FromSuccess(user)); } } return(TypeReaderResult.FromError(CommandError.ObjectNotFound, "User not found.")); } /* * if (svc != null) { * var game = svc.GetGameFromChannel(context.Channel); * if (game != null) { * var player = game.Players.SingleOrDefault(p => p.User.Id == user.Id); * return (player != null) * ? TypeReaderResult.FromSuccess(player) * : TypeReaderResult.FromError(CommandError.ObjectNotFound, "Specified user not a player in this game."); * } * return TypeReaderResult.FromError(CommandError.ObjectNotFound, "No game going on."); * } * return TypeReaderResult.FromError(CommandError.ObjectNotFound, "Game service not found.");*/ }
public override Task <TypeReaderResult> Read(IUserMessage context, string input) { ulong id; //By Id (1.0) if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) { var msg = context.Channel.GetCachedMessage(id) as T; if (msg != null) { return(Task.FromResult(TypeReaderResult.FromSuccess(msg))); } } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Message not found."))); }
public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { string regex = @"<(a?):(\w+):(\d+)>"; Match match = Regex.Match(input, regex); //Check if it's custom discord emoji if (match.Success) { return(await Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "This is a custom emoji not a normal one, if you beleive they should work on this command make an issue on the GitHub over at !help"))); } Emoji emoji = new Emoji(input); try { await context.Message.AddReactionAsync(emoji); await context.Message.RemoveReactionAsync(emoji, context.Client.CurrentUser); } catch { return(await Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "That is not a valid emoji"))); } return(await Task.FromResult(TypeReaderResult.FromSuccess(emoji))); }
/// <inheritdoc /> public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { var results = new Dictionary <ulong, TypeReaderValue>(); IAsyncEnumerable <IUser> channelUsers = context.Channel.GetUsersAsync(CacheMode.CacheOnly).Flatten(); // it's better IReadOnlyCollection <IGuildUser> guildUsers = ImmutableArray.Create <IGuildUser>(); if (context.Guild != null) { guildUsers = await context.Guild.GetUsersAsync(CacheMode.CacheOnly).ConfigureAwait(false); } //By Mention (1.0) if (MentionUtils.TryParseUser(input, out var id)) { if (context.Guild != null) { AddResult(results, await context.Guild.GetUserAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 1.00f); } else { AddResult(results, await context.Channel.GetUserAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 1.00f); } } //By Id (0.9) if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) { if (context.Guild != null) { AddResult(results, await context.Guild.GetUserAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 0.90f); } else { AddResult(results, await context.Channel.GetUserAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 0.90f); } } //By Username + Discriminator (0.7-0.85) int index = input.LastIndexOf('#'); if (index >= 0) { string username = input.Substring(0, index); if (ushort.TryParse(input.Substring(index + 1), out ushort discriminator)) { var channelUser = await channelUsers.FirstOrDefaultAsync(x => x.DiscriminatorValue == discriminator && string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)).ConfigureAwait(false); 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) { await channelUsers .Where(x => string.Equals(input, x.Username, StringComparison.OrdinalIgnoreCase)) .ForEachAsync(channelUser => AddResult(results, channelUser as T, channelUser.Username == input ? 0.65f : 0.55f)) .ConfigureAwait(false); 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) { await channelUsers .Where(x => string.Equals(input, (x as IGuildUser)?.Nickname, StringComparison.OrdinalIgnoreCase)) .ForEachAsync(channelUser => AddResult(results, channelUser as T, (channelUser as IGuildUser).Nickname == input ? 0.65f : 0.55f)) .ConfigureAwait(false); foreach (var guildUser in guildUsers.Where(x => string.Equals(input, x.Nickname, StringComparison.OrdinalIgnoreCase))) { AddResult(results, guildUser as T, guildUser.Nickname == input ? 0.60f : 0.50f); } } if (results.Count > 0) { return(TypeReaderResult.FromSuccess(results.Values.ToImmutableArray())); } return(TypeReaderResult.FromError(CommandError.ObjectNotFound, "User not found.")); }
public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { T result = new T(); ReadState state = ReadState.LookingForParameter; int beginRead = 0, currentRead = 0; while (state != ReadState.End) { try { PropertyInfo prop = Read(out string arg); object propVal = await ReadArgumentAsync(prop, arg).ConfigureAwait(false); if (propVal != null) { prop.SetMethod.Invoke(result, new[] { propVal }); } else { return(TypeReaderResult.FromError(CommandError.ParseFailed, $"Could not parse the argument for the parameter '{prop.Name}' as type '{prop.PropertyType}'.")); } } catch (Exception ex) { return(TypeReaderResult.FromError(ex)); } } return(TypeReaderResult.FromSuccess(result)); PropertyInfo Read(out string arg) { string currentParam = null; char match = '\0'; for (; currentRead < input.Length; currentRead++) { char currentChar = input[currentRead]; switch (state) { case ReadState.LookingForParameter: if (Char.IsWhiteSpace(currentChar)) { continue; } else { beginRead = currentRead; state = ReadState.InParameter; } break; case ReadState.InParameter: if (currentChar != ':') { continue; } else { currentParam = input.Substring(beginRead, currentRead - beginRead); state = ReadState.LookingForArgument; } break; case ReadState.LookingForArgument: if (Char.IsWhiteSpace(currentChar)) { continue; } else { beginRead = currentRead; state = (QuotationAliasUtils.GetDefaultAliasMap.TryGetValue(currentChar, out match)) ? ReadState.InQuotedArgument : ReadState.InArgument; } break; case ReadState.InArgument: if (!Char.IsWhiteSpace(currentChar)) { continue; } else { return(GetPropAndValue(out arg)); } case ReadState.InQuotedArgument: if (currentChar != match) { continue; } else { return(GetPropAndValue(out arg)); } } } if (currentParam == null) { throw new InvalidOperationException("No parameter name was read."); } return(GetPropAndValue(out arg)); PropertyInfo GetPropAndValue(out string argv) { bool quoted = state == ReadState.InQuotedArgument; state = (currentRead == (quoted ? input.Length - 1 : input.Length)) ? ReadState.End : ReadState.LookingForParameter; if (quoted) { argv = input.Substring(beginRead + 1, currentRead - beginRead - 1).Trim(); currentRead++; } else { argv = input.Substring(beginRead, currentRead - beginRead); } return(_tProps[currentParam]); } } async Task <object> ReadArgumentAsync(PropertyInfo prop, string arg) { Type elemType = prop.PropertyType; bool isCollection = false; if (elemType.GetTypeInfo().IsGenericType&& elemType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { elemType = prop.PropertyType.GenericTypeArguments[0]; isCollection = true; } OverrideTypeReaderAttribute overridden = prop.GetCustomAttribute <OverrideTypeReaderAttribute>(); TypeReader reader = (overridden != null) ? ModuleClassBuilder.GetTypeReader(_commands, elemType, overridden.TypeReader, services) : (_commands.GetDefaultTypeReader(elemType) ?? _commands.GetTypeReaders(elemType).FirstOrDefault().Value); if (reader != null) { if (isCollection) { MethodInfo method = _readMultipleMethod.MakeGenericMethod(elemType); Task <IEnumerable> task = (Task <IEnumerable>)method.Invoke(null, new object[] { reader, context, arg.Split(','), services }); return(await task.ConfigureAwait(false)); } else { return(await ReadSingle(reader, context, arg, services).ConfigureAwait(false)); } } return(null); } }
public override async Task <TypeReaderResult> Read(IUserMessage context, string input) { var results = new Dictionary <ulong, TypeReaderValue>(); var guild = (context.Channel as IGuildChannel)?.Guild; IReadOnlyCollection <IUser> channelUsers = await context.Channel.GetUsersAsync().ConfigureAwait(false); IReadOnlyCollection <IGuildUser> guildUsers = null; ulong id; if (guild != null) { guildUsers = await guild.GetUsersAsync().ConfigureAwait(false); } //By Mention (1.0) if (MentionUtils.TryParseUser(input, out id)) { if (guild != null) { AddResult(results, await guild.GetUserAsync(id).ConfigureAwait(false) as T, 1.00f); } else { AddResult(results, await context.Channel.GetUserAsync(id).ConfigureAwait(false) as T, 1.00f); } } //By Id (0.9) if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) { if (guild != null) { AddResult(results, await guild.GetUserAsync(id).ConfigureAwait(false) as T, 0.90f); } else { AddResult(results, await context.Channel.GetUserAsync(id).ConfigureAwait(false) as T, 0.90f); } } //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.Where(x => x.DiscriminatorValue == discriminator && string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); AddResult(results, channelUser as T, channelUser.Username == username ? 0.85f : 0.75f); var guildUser = channelUsers.Where(x => x.DiscriminatorValue == discriminator && string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); 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(TypeReaderResult.FromSuccess(results.Values.ToArray())); } return(TypeReaderResult.FromError(CommandError.ObjectNotFound, "User not found.")); }