コード例 #1
0
ファイル: Conversations.cs プロジェクト: alpha-zero/NadekoBot
 private CommandBuilder AliasCommand(CommandBuilder cb, string txt) => cb.Alias(new string[] { "," + txt, "-" + txt });
コード例 #2
0
        private static void BuildCommand(CommandBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service)
        {
            var attributes = method.GetCustomAttributes();

            foreach (var attribute in attributes)
            {
                // TODO: C#7 type switch
                if (attribute is CommandAttribute)
                {
                    var cmdAttr = attribute as CommandAttribute;
                    builder.AddAliases(cmdAttr.Text);
                    builder.RunMode = cmdAttr.RunMode;
                    builder.Name    = builder.Name ?? cmdAttr.Text;
                }
                else if (attribute is NameAttribute)
                {
                    builder.Name = (attribute as NameAttribute).Text;
                }
                else if (attribute is PriorityAttribute)
                {
                    builder.Priority = (attribute as PriorityAttribute).Priority;
                }
                else if (attribute is SummaryAttribute)
                {
                    builder.Summary = (attribute as SummaryAttribute).Text;
                }
                else if (attribute is RemarksAttribute)
                {
                    builder.Remarks = (attribute as RemarksAttribute).Text;
                }
                else if (attribute is AliasAttribute)
                {
                    builder.AddAliases((attribute as AliasAttribute).Aliases);
                }
                else if (attribute is PreconditionAttribute)
                {
                    builder.AddPrecondition(attribute as PreconditionAttribute);
                }
            }

            if (builder.Name == null)
            {
                builder.Name = method.Name;
            }

            var parameters = method.GetParameters();
            int pos = 0, count = parameters.Length;

            foreach (var paramInfo in parameters)
            {
                builder.AddParameter((parameter) =>
                {
                    BuildParameter(parameter, paramInfo, pos++, count, service);
                });
            }

            var createInstance = ReflectionUtils.CreateBuilder <IModuleBase>(typeInfo, service);

            builder.Callback = (ctx, args, map) =>
            {
                var instance = createInstance(map);
                instance.SetContext(ctx);
                try
                {
                    instance.BeforeExecute();
                    return(method.Invoke(instance, args) as Task ?? Task.Delay(0));
                }
                finally
                {
                    instance.AfterExecute();
                    (instance as IDisposable)?.Dispose();
                }
            };
        }
コード例 #3
0
        public CommandBuilder CreateCommand(string cmd)
        {
            var command = new Command(CommandBuilder.AppendPrefix(_prefix, cmd));

            return(new CommandBuilder(_service, command, _prefix, _category, _checks));
        }
コード例 #4
0
        private static void BuildCommand(CommandBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service, IServiceProvider serviceprovider)
        {
            IEnumerable <Attribute> attributes = method.GetCustomAttributes();

            foreach (Attribute attribute in attributes)
            {
                switch (attribute)
                {
                case CommandAttribute command:
                    builder.AddAliases(command.Text);
                    builder.RunMode         = command.RunMode;
                    builder.Name            = builder.Name ?? command.Text;
                    builder.IgnoreExtraArgs = command.IgnoreExtraArgs ?? service._ignoreExtraArgs;
                    break;

                case NameAttribute name:
                    builder.Name = name.Text;
                    break;

                case PriorityAttribute priority:
                    builder.Priority = priority.Priority;
                    break;

                case SummaryAttribute summary:
                    builder.Summary = summary.Text;
                    break;

                case RemarksAttribute remarks:
                    builder.Remarks = remarks.Text;
                    break;

                case AliasAttribute alias:
                    builder.AddAliases(alias.Aliases);
                    break;

                case PreconditionAttribute precondition:
                    builder.AddPrecondition(precondition);
                    break;

                default:
                    builder.AddAttributes(attribute);
                    break;
                }
            }

            if (builder.Name == null)
            {
                builder.Name = method.Name;
            }

            System.Reflection.ParameterInfo[] parameters = method.GetParameters();
            int pos = 0, count = parameters.Length;

            foreach (System.Reflection.ParameterInfo paramInfo in parameters)
            {
                builder.AddParameter((parameter) =>
                {
                    BuildParameter(parameter, paramInfo, pos++, count, service, serviceprovider);
                });
            }

            Func <IServiceProvider, IModuleBase> createInstance = ReflectionUtils.CreateBuilder <IModuleBase>(typeInfo, service);

            async Task <IResult> ExecuteCallback(ICommandContext context, object[] args, IServiceProvider services, CommandInfo cmd)
            {
                IModuleBase instance = createInstance(services);

                instance.SetContext(context);

                try
                {
                    instance.BeforeExecute(cmd);

                    Task task = method.Invoke(instance, args) as Task ?? Task.Delay(0);
                    if (task is Task <RuntimeResult> resultTask)
                    {
                        return(await resultTask.ConfigureAwait(false));
                    }
                    else
                    {
                        await task.ConfigureAwait(false);

                        return(ExecuteResult.FromSuccess());
                    }
                }
                finally
                {
                    instance.AfterExecute(cmd);
                    (instance as IDisposable)?.Dispose();
                }
            }

            builder.Callback = ExecuteCallback;
        }
コード例 #5
0
 public CommandGroupBuilder CreateGroup(string cmd, Action <CommandGroupBuilder> config = null)
 {
     config(new CommandGroupBuilder(_service, CommandBuilder.AppendPrefix(_prefix, cmd), _checks));
     return(this);
 }