コード例 #1
0
        public bool CheckCall(string command)
        {
            var args = command.Split(' ');

            if (args.Length == 0)
            {
                return(false);
            }

            foreach (var tuple in _textBindMethods)
            {
                var collection            = tuple.Item1;
                var method                = tuple.Item2;
                var textAttributes        = method.GetCustomAttributes <TextAttribute>();
                var conditionalAttributes = method.GetCustomAttributes <ConditionalAttribute>();

                foreach (var textAttribute in textAttributes)
                {
                    if (String.Compare(textAttribute.Command, args[0], true) == 0)
                    {
                        if (textAttribute.IsActive(Core) && ConditionalAttribute.CanExecute(conditionalAttributes))
                        {
                            var argsList = new List <string>(args.Skip(1));
                            if (argsList.Count >= textAttribute.ArgumentCount)
                            {
                                if (method.ReturnType == typeof(bool) || method.ReturnType == typeof(Task <bool>))
                                {
                                    if (textAttribute.IsAsync(method))
                                    {
                                        Task.Run(async() =>
                                        {
                                            var returnValue = await method.InvokeAsync <bool>(collection, new object[] { argsList.ToArray() });

                                            if (!returnValue)
                                            {
                                                ChatUtil.ShowUsage($"Benutzung: {args[0]} {textAttribute.Arguments}");
                                            }
                                        });
                                    }
                                    else
                                    {
                                        Task.Run(() =>
                                        {
                                            var returnValue = (bool)method.Invoke(collection, new object[] { argsList.ToArray() });

                                            if (!returnValue)
                                            {
                                                ChatUtil.ShowUsage($"Benutzung: {args[0]} {textAttribute.Arguments}");
                                            }
                                        });
                                    }
                                }
                                else
                                {
                                    if (textAttribute.IsAsync(method))
                                    {
                                        Task.Run(async() => await method.InvokeAsync(collection, new object[] { argsList.ToArray() }));
                                    }
                                    else
                                    {
                                        Task.Run(() => method.Invoke(collection, new object[] { argsList.ToArray() }));
                                    }
                                }
                            }
                            else
                            {
                                ChatUtil.ShowUsage($"Benutzung: {args[0]} {textAttribute.Arguments}");
                            }


                            return(true);
                        }
                    }
                }
            }

            return(false);
        }