コード例 #1
0
        private static Dictionary <Option, string?> ParseOptions(string text)
        {
            var dictionary      = DefaultOptions();
            var combinedOptions = CliHelper.CombineOption(text.Split(' ', StringSplitOptions.RemoveEmptyEntries), ' ');
            var queue           = new Queue <string?>(combinedOptions);

            while (queue.Count > 0)
            {
                var entry = queue.Dequeue();
                if (entry == "-t" || entry == "--type")
                {
                    dictionary[Option.Type] = queue.Dequeue();
                }
                else if (entry == "-a" || entry == "--alias")
                {
                    dictionary[Option.Alias] = queue.Dequeue();
                }
                else if (entry == "-m" || entry == "--method")
                {
                    dictionary[Option.Method] = queue.Dequeue();
                }
                else if (entry == "-c" || entry == "--content")
                {
                    dictionary[Option.Content] = queue.Dequeue();
                }
                else if (entry == "-p" || entry == "--path")
                {
                    dictionary[Option.Path] = queue.Dequeue();
                }
            }
            return(dictionary);
        }
コード例 #2
0
        private async Task <IAction?> ProcessDynamicCommand(EventData data, CustomCommand command)
        {
            if (!DynamicCommand.TryParse(command.Parameter !, out var dynaCmd))
            {
                return(NewMessageAction("Corrupted dynamic command"));
            }

            var param = HttpUtility.HtmlDecode(data.CommandParameters ?? "");
            var args  = param?.Split(' ', StringSplitOptions.RemoveEmptyEntries);

            args = CliHelper.CombineOption(args, ' ').ToArray();
            var expectedLength = command.ExpectedDynamicCommandArgs;

            if (args.Length != expectedLength)
            {
                return(NewMessageAction($"Expected {expectedLength} args, found {args.Length} for command '{command.Name}'"));
            }

            var timeout = TimeSpan.FromSeconds(10);

            try
            {
                var api = Uri.UnescapeDataString(dynaCmd !.ApiAddress.AbsoluteUri);
                args = args.Select(HttpUtility.UrlEncode).ToArray();
                api  = HttpUtility.HtmlDecode(string.Format(api, args));
                if (dynaCmd.Method == Method.Get && dynaCmd.ResponseType == ResponseType.Image)
                {
                    var alias = dynaCmd.Alias?.Trim();
                    // TODO Discord doesn't have a hyperlink markdown yet unfortunately.
                    // but we can use this: https://leovoel.github.io/embed-visualizer/
                    api = string.IsNullOrEmpty(alias) ? api : $"[{alias}]({api})";
                    return(NewMessageAction(api));
                }
                var cts         = new CancellationTokenSource(timeout);
                var apiResponse = await Fetch(api, dynaCmd.Method, dynaCmd.ContentType, cts.Token);

                var stringContent = apiResponse.ToString() ?? "{}";
                if (string.IsNullOrEmpty(dynaCmd.JsonPath))
                {
                    return(NewMessageAction(stringContent !));
                }
                var obj      = JObject.Parse(stringContent);
                var response = obj.SelectToken(dynaCmd.JsonPath)?.ToString() ?? stringContent;
                return(NewMessageAction(response));
            }
            catch (OperationCanceledException)
            {
                return(NewMessageAction($"I can't wait for longer than {timeout:s} and the API is slow."));
            }
        }