public override async Task <TypeReaderResult> ReadAsync( ICommandContext context, string input, IServiceProvider services) { var result = await _reader.ReadAsync(context, input, services); return(result.IsSuccess || input.IsSkipped() ? TypeReaderResult.FromSuccess(result) : TypeReaderResult.FromError(result)); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (Globals.SPECIAL_ALIASES.TryGetValue(input, out Globals.SpecialType result)) { return(Task.FromResult(TypeReaderResult.FromSuccess(result))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input could not be parsed as a Special."))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (Enum.TryParse(input, out GlobalConfigItems result)) { return(Task.FromResult(TypeReaderResult.FromSuccess(result))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Invalid key from GlobalConfigItems. Get available keys from `globalConfig keys` command."))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (string.IsNullOrEmpty(input)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Null or empty input"))); } return(Task.FromResult(TypeReaderResult.FromSuccess(input.ConvertTypesetterToTypewriter().Trim()))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (string.IsNullOrWhiteSpace(input)) { return(Task.FromResult(TypeReaderResult.FromSuccess(new List <string>()))); } return(Task.FromResult(TypeReaderResult.FromSuccess(input.Split(',').Select(s => s.Trim()).ToList()))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { input = input.Trim(); var guilds = _client.Guilds; var guild = guilds.FirstOrDefault(g => g.Id.ToString().Equals(input, StringComparison.OrdinalIgnoreCase)) ?? guilds.FirstOrDefault(g => g.Name.Equals(input, StringComparison.OrdinalIgnoreCase)); return(Task.FromResult(guild != null ? TypeReaderResult.FromSuccess(guild) : TypeReaderResult.FromError(CommandError.ParseFailed, "No guild by that name or Id found"))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (TryParsePlaylist(input, out RLPlaylist? playlist)) { return(Task.FromResult(TypeReaderResult.FromSuccess(playlist.Value))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, InvalidPlaylistMessage()))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext ctx, string input, IServiceProvider provider) { if (ItemHelper.Exists(input)) { return(Task.FromResult(TypeReaderResult.FromSuccess(ItemHelper.GetItem(input)))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Could not find an Item with the specified ID."))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (DiceRoll.TryParse(input, out var result)) { return(Task.FromResult(TypeReaderResult.FromSuccess(result))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input could not be parsed as a DiceRoll."))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (!Colour.TryParse(input, out var colour)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Failed to parse a valid colour."))); } return(Task.FromResult(TypeReaderResult.FromSuccess(colour))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { var commandService = services.GetService <CommandService>(); var module = commandService.Modules.FirstOrDefault(x => x.Name.ToLower() == input.ToLower()); return(Task.FromResult(module != null ? TypeReaderResult.FromSuccess(module) : TypeReaderResult.FromError(CommandError.ParseFailed, "No module matches the input."))); }
/// <summary> /// Gets the best role result from the given results. /// </summary> /// <param name="result">The results of a parse.</param> /// <returns>The role result with the highest score or <c>null</c> if no results exist.</returns> public static T GetBestResult(TypeReaderResult result) { if (result.IsSuccess) { return(result.Values.OrderByDescending(v => v.Score).First().Value as T); } return(null); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { switch (input.ToLower()) { case "lvl": case "level": case "poziom": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.Level))); case "sc": case "funds": case "wallet": case "wallet sc": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.ScCnt))); case "tc": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.TcCnt))); case "posty": case "msg": case "wiadomosci": case "wiadomości": case "messages": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.Posts))); case "postym": case "msgm": case "wiadomoscim": case "wiadomościm": case "messagesm": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.PostsMonthly))); case "postyms": case "msgmavg": case "wiadomoscims": case "wiadomościms": case "messagesmabg": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.PostsMonthlyCharacter))); case "command": case "commands": case "polecenia": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.Commands))); case "karty": case "cards": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.Cards))); case "kartym": case "cardsp": return(Task.FromResult(TypeReaderResult.FromSuccess(TopType.CardsPower))); default: return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Nie rozpoznano typu topki!"))); } }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider provider) { var match = Regex.Match(input, Pattern); if (match is null) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Failed to parse TimeSpan"))); } if (!int.TryParse(match.Groups["years"].Value, out int years) && !string.IsNullOrEmpty(match.Groups["years"].Value)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Too large number given for years"))); } if (!int.TryParse(match.Groups["weeks"].Value, out int weeks) && !string.IsNullOrEmpty(match.Groups["weeks"].Value)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Too large number given for weeks"))); } if (!int.TryParse(match.Groups["days"].Value, out int days) && !string.IsNullOrEmpty(match.Groups["days"].Value)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Too large number given for days"))); } if (!int.TryParse(match.Groups["hours"].Value, out int hours) && !string.IsNullOrEmpty(match.Groups["hours"].Value)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Too large number given for hours"))); } if (!int.TryParse(match.Groups["minutes"].Value, out int minutes) && !string.IsNullOrEmpty(match.Groups["minutes"].Value)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Too large number given for minutes"))); } if (!int.TryParse(match.Groups["seconds"].Value, out int seconds) && !string.IsNullOrEmpty(match.Groups["seconds"].Value)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Too large number given for seconds"))); } var currentTime = DateTimeOffset.UtcNow; var dueTime = currentTime.AddYears(years) .AddDays(days + weeks * 7) .AddHours(hours) .AddMinutes(minutes) .AddSeconds(seconds); var timespan = dueTime - currentTime; if (timespan.TotalSeconds == 0) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.UnmetPrecondition, "The timespan has to be at least 1 second long!"))); } return(Task.FromResult(TypeReaderResult.FromSuccess(timespan))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext contextParam, string input, IServiceProvider services) { var context = contextParam as CustomCommandContext; var tag = context.Server.Tags.FirstOrDefault(x => x.TagName == input || x.Aliasses.Contains(input)); return(Task.FromResult(tag == null ? TypeReaderResult.FromError(CommandError.ObjectNotFound, "Tag doesn't exist") : TypeReaderResult.FromSuccess(tag))); }
public override Task <TypeReaderResult> Read(ICommandContext context, string input) { T value; if (tryParseFunc(input, out value)) { return(Task.FromResult(TypeReaderResult.FromSuccess(new Nullable <T>(value)))); } return(Task.FromResult(TypeReaderResult.FromSuccess(new Nullable <T>()))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (!SnippetRegex.IsMatch(input)) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Invalid code snippet provided, please use a multiline, syntax-highlighted markdown code block."))); } Match match = SnippetRegex.Match(input); return(Task.FromResult(TypeReaderResult.FromSuccess(new CodeSnippet(match.Groups[1].Value, match.Groups[2].Value)))); }
public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { var dateTimeParsed = await DehumanizeTimespanAsync(input).ConfigureAwait(false); return(dateTimeParsed == TimeSpan.Zero ? TypeReaderResult.FromError(CommandError.ParseFailed, @"Invalid time format. Format example: `4d3h2m1s` (4 days 3 hours 2 minutes 1 second).") : TypeReaderResult.FromSuccess(dateTimeParsed)); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider _) { var module = _cmds.Modules.Where(m => !m.IsSubmodule).FirstOrDefault(m => m.Name.Equals(input, StringComparison.OrdinalIgnoreCase)); return(module != null || input.Equals("actualcustomreactions", StringComparison.OrdinalIgnoreCase) ? Task.FromResult(TypeReaderResult.FromSuccess(new ModuleOrCrInfo { Name = input, })) : Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "No such module found."))); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { BigInteger result; if (BigInteger.TryParse(input, out result)) { return(Task.FromResult(TypeReaderResult.FromSuccess(result))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Failed to Parse Input as Integer"))); }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public override async Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { if (Uri.TryCreate(input, UriKind.Absolute, out Uri result)) { return(TypeReaderResult.FromSuccess(result)); } return(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Invalid Uri.")); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (Int32.TryParse(input, out var result)) { return(Task.FromResult(TypeReaderResult.FromSuccess(result))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "I don't know how to read that number, look how big it. Definitely larger than 32 bits..."))); }
public TypeReaderResult Read(ICommandContext context, string input, IServiceProvider services) { // prevent divisions being equated to numbers if (TryParseFriendly(input, out Division div)) { return(TypeReaderResult.FromSuccess(div)); } return(GenerateError()); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (Enum.TryParse(typeof(Drug), input, ignoreCase: true, out object Result)) { return(Task.FromResult(TypeReaderResult.FromSuccess(Result))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input could not be parsed as a fafa"))); }
public static Discord.Commands.TypeReaderResult AttemptParseInput(string input, Type desired) { var type = typeof(Discord.Commands.CommandService); var thing = type.GetField("_defaultTypeReaders", BindingFlags.NonPublic | BindingFlags.Instance); var defaultTypeReaders = thing.GetValue(Program.Commands) as IDictionary <Type, Discord.Commands.TypeReader>; var thing2 = type.GetField("_typeReaders", BindingFlags.NonPublic | BindingFlags.Instance); var ownTypeReaders = thing2.GetValue(Program.Commands) as System.Collections.Concurrent.ConcurrentDictionary <System.Type, System.Collections.Concurrent.ConcurrentDictionary <System.Type, Discord.Commands.TypeReader> >; Dictionary <Type, Discord.Commands.TypeReader> combined = new Dictionary <Type, Discord.Commands.TypeReader>(); foreach (var keypair in defaultTypeReaders) { combined.Add(keypair.Key, keypair.Value); } foreach (var keypair in ownTypeReaders) { combined[keypair.Key] = keypair.Value.Values.First(); } if (desired.BaseType == typeof(Array)) { // we'll try to split the input, then parse each individual item. var typeOfElement = desired.GetMethod("Get").ReturnType; var elements = input.Split(','); dynamic array = Activator.CreateInstance(desired, elements.Length); for (int i = 0; i < elements.Length; i++) { var inner = AttemptParseInput(elements[i], typeOfElement); if (!inner.IsSuccess) { return(TypeReaderResult.FromError(CommandError.ParseFailed, $"Attempts to parse element of {typeOfElement.Name}[] failed: {inner.ErrorReason}")); } array[i] = (dynamic)inner.BestMatch; } return(TypeReaderResult.FromSuccess(array)); } var reader = combined[desired]; if (reader == null) { return(Discord.Commands.TypeReaderResult.FromError( Discord.Commands.CommandError.Exception, $"Parser for {desired.Name} unavailabe")); } var result = reader.ReadAsync(null, input, Program.Services).Result; if (result.IsSuccess) { return(Discord.Commands.TypeReaderResult.FromSuccess(result.BestMatch)); } else { return(Discord.Commands.TypeReaderResult.FromError( Discord.Commands.CommandError.ParseFailed, result.ErrorReason)); } }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { BuffType result; if (Enum.TryParse(input, true, out result)) { return(Task.FromResult(TypeReaderResult.FromSuccess(result))); } return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input could not be parsed as a buff type."))); }
public override Task <TypeReaderResult> Read(ICommandContext context, string input) { if (Uri.TryCreate(input, UriKind.Absolute, out Uri uri)) { return(Task.FromResult(TypeReaderResult.FromSuccess(uri))); } else { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, $"`{input}` is not a valid url."))); } }
public TypeReaderResult Read(ICommandContext context, string input, IServiceProvider services) { DivisionWithCategory result; if (DivisionWithCategory.TryParse(input, out result) && (!result.Category.HasValue || result.Division == Division.AllService)) { return(TypeReaderResult.FromSuccess(result)); } return(ErrorReturn); }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { var gdt = Parse(services, context.Guild.Id, input); if (gdt == null) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input string is in an incorrect format."))); } return(Task.FromResult(TypeReaderResult.FromSuccess(gdt))); }
public override Task <TypeReaderResult> Read(CommandContext context, string input) { bool result; if (bool.TryParse(input, out result)) { return(Task.FromResult(TypeReaderResult.FromSuccess(result))); } return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input could not be parsed as a boolean.")) }