コード例 #1
0
        private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service)
        {
            var attributes = typeInfo.GetCustomAttributes();

            foreach (var attribute in attributes)
            {
                switch (attribute)
                {
                case NameAttribute name:
                    builder.Name = name.Text;
                    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 GroupAttribute group:
                    builder.Name = builder.Name ?? group.Prefix;
                    builder.AddAliases(group.Prefix);
                    break;

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

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

            //Check for unspecified info
            if (builder.Aliases.Count == 0)
            {
                builder.AddAliases("");
            }
            if (builder.Name == null)
            {
                builder.Name = typeInfo.Name;
            }

            var validCommands = typeInfo.DeclaredMethods.Where(x => IsValidCommandDefinition(x));

            foreach (var method in validCommands)
            {
                builder.AddCommand((command) =>
                {
                    BuildCommand(command, typeInfo, method, service);
                });
            }
        }
コード例 #2
0
        private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service)
        {
            //gotta do this until i have the time to move everything from static fields/constructors over to
            // a service. This will ensure static constructors are ran >.<
            var instance = ReflectionUtils.CreateBuilder <IModuleBase>(typeInfo, service).Invoke(DependencyMap.Empty);

            var attributes = typeInfo.GetCustomAttributes();

            foreach (var attribute in attributes)
            {
                // TODO: C#7 type switch
                if (attribute is NameAttribute)
                {
                    builder.Prefix = (attribute as NameAttribute).Text;
                }
                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 GroupAttribute)
                {
                    var groupAttr = attribute as GroupAttribute;
                    builder.Name = builder.Name ?? groupAttr.Name;
                    builder.AddAliases(groupAttr.Prefix);
                }
                else if (attribute is PreconditionAttribute)
                {
                    builder.AddPrecondition(attribute as PreconditionAttribute);
                }
            }

            //Check for unspecified info
            if (builder.Aliases.Count == 0)
            {
                builder.AddAliases("");
            }
            if (builder.Name == null)
            {
                builder.Name = typeInfo.Name;
            }

            var validCommands = typeInfo.DeclaredMethods.Where(x => IsValidCommandDefinition(x));

            foreach (var method in validCommands)
            {
                builder.AddCommand((command) =>
                {
                    BuildCommand(command, typeInfo, method, service);
                });
            }
        }
コード例 #3
0
        private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service)
        {
            var attributes = typeInfo.GetCustomAttributes();

            foreach (var attribute in attributes)
            {
                // TODO: C#7 type switch
                if (attribute is NameAttribute)
                {
                    builder.Name = (attribute as NameAttribute).Text;
                }
                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 GroupAttribute)
                {
                    var groupAttr = attribute as GroupAttribute;
                    builder.Name = builder.Name ?? groupAttr.Prefix;
                    builder.AddAliases(groupAttr.Prefix);
                }
                else if (attribute is PreconditionAttribute)
                {
                    builder.AddPrecondition(attribute as PreconditionAttribute);
                }
            }

            //Check for unspecified info
            if (builder.Aliases.Count == 0)
            {
                builder.AddAliases("");
            }
            if (builder.Name == null)
            {
                builder.Name = typeInfo.Name;
            }

            var validCommands = typeInfo.DeclaredMethods.Where(x => IsValidCommandDefinition(x));

            foreach (var method in validCommands)
            {
                builder.AddCommand((command) =>
                {
                    BuildCommand(command, typeInfo, method, service);
                });
            }
        }
コード例 #4
0
ファイル: CommandHandler.cs プロジェクト: popalex/SaltyEmu
        public async Task RemoveCommandAsync(Command command)
        {
            var builder = new ModuleBuilder();

            var oldModule = command.Module;

            builder.AddAliases(oldModule.Aliases?.ToArray())
            .AddAttributes(oldModule.Attributes?.ToArray())
            .AddChecks(oldModule.Checks?.ToArray())
            .WithDescription(oldModule.Description)
            .WithIgnoreExtraArguments(oldModule.IgnoreExtraArguments)
            .WithName(oldModule.Name)
            .WithRemarks(oldModule.Remarks)
            .WithRunMode(oldModule.RunMode);

            foreach (var cmd in oldModule.Commands)
            {
                if (cmd.FullAliases[0] == command.FullAliases[0])
                {
                    continue;
                }

                var cmdBuilder = new CommandBuilder()
                                 .AddAliases(cmd.Aliases?.ToArray())
                                 .AddAttributes(cmd.Attributes?.ToArray())
                                 .AddChecks(cmd.Checks?.ToArray())
                                 .AddCooldowns(cmd.Cooldowns?.ToArray())
                                 .WithCallback(cmd.Callback)
                                 .WithDescription(cmd.Description)
                                 .WithIgnoreExtraArguments(cmd.IgnoreExtraArguments)
                                 .WithName(cmd.Name)
                                 .WithPriority(cmd.Priority)
                                 .WithRemarks(cmd.Remarks)
                                 .WithRunMode(cmd.RunMode);

                foreach (var param in cmd.Parameters)
                {
                    var paramBuilder = new ParameterBuilder()
                                       .AddAttributes(param.Attributes?.ToArray())
                                       .AddChecks(param.Checks?.ToArray())
                                       .WithCustomTypeParserType(param.CustomTypeParserType)
                                       .WithDefaultValue(param.DefaultValue)
                                       .WithDescription(param.Description)
                                       .WithIsMultiple(param.IsMultiple)
                                       .WithIsOptional(param.IsOptional)
                                       .WithIsRemainder(param.IsRemainder)
                                       .WithName(param.Name)
                                       .WithRemarks(param.Remarks)
                                       .WithType(param.Type);

                    cmdBuilder.AddParameter(paramBuilder);
                }

                builder.AddCommand(cmdBuilder);
            }

            await _commands.RemoveModuleAsync(oldModule);

            await _commands.AddModuleAsync(builder);
        }
コード例 #5
0
        private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service, IServiceProvider services)
        {
            var attributes = typeInfo.GetCustomAttributes();

            builder.TypeInfo = typeInfo;

            foreach (var attribute in attributes)
            {
                switch (attribute)
                {
                case NameAttribute name:
                    builder.Name = name.Text;
                    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 GroupAttribute group:
                    builder.Name  = builder.Name ?? group.Prefix;
                    builder.Group = group.Prefix;
                    builder.AddAliases(group.Prefix);
                    break;

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

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

            //Check for unspecified info
            if (builder.Aliases.Count == 0)
            {
                builder.AddAliases("");
            }
            if (builder.Name == null)
            {
                builder.Name = typeInfo.Name;
            }

            // Get all methods (including from inherited members), that are valid commands
            var validCommands = typeInfo.GetMethods().Where(IsValidCommandDefinition);

            foreach (var method in validCommands)
            {
                builder.AddCommand((command) =>
                {
                    BuildCommand(command, typeInfo, method, service, services);
                });
            }
        }
コード例 #6
0
        protected override void OnModuleBuilding(CommandService commandService, ModuleBuilder builder)
        {
            base.OnModuleBuilding(commandService, builder);

            using var scope = ServiceProvider.CreateScope();
            ActionService   = ServiceProvider.GetRequiredService <ActionService>();
            //Get phrases from items as aliases for the referenced command.
            foreach (var action in ActionService.GetAll().OfType <BotCommandAction>()
                     .Where(s => s.TextCommandProperties != null))
            {
                foreach (var textProperties in action.TextCommandProperties)
                {
                    builder.AddCommand(textProperties.Name, RunActionFromTextCommand,
                                       builder =>
                    {
                        if (textProperties.Aliases != null)
                        {
                            builder.AddAliases(textProperties.Aliases.ToArray());
                        }
                        builder.Summary = textProperties.Summary;
                        if (action.GuildsOnly)
                        {
                            builder.AddPrecondition(new RequireContextAttribute(ContextType.Guild));
                        }
                        if (action.RequiredPermissions.HasValue)
                        {
                            var requiredPermissions = action.RequiredPermissions.Value.ToList();
                            if (!requiredPermissions.Any())
                            {
                                builder.AddPrecondition(new RequireOwnerAttribute());
                            }
                            else
                            {
                                GuildPermission permissions = 0;
                                foreach (var permission in requiredPermissions)
                                {
                                    permissions |= permission;
                                }

                                builder.AddPrecondition(new RequireUserPermissionAttribute(permissions));
                            }
                        }

                        if (textProperties.Priority.HasValue)
                        {
                            builder.Priority = textProperties.Priority.Value;
                        }

                        var parameters = action.GetParameters <ActionParameterTextAttribute>()
                                         ?.Where(p =>
                                                 p.Attribute.FilterCommandNames == null ||
                                                 p.Attribute.FilterCommandNames.Contains(textProperties.Name))
                                         .OrderBy(p => p.Attribute.Order);
                        if (parameters != null)
                        {
                            foreach (var p in parameters)
                            {
                                builder.AddParameter(p.Attribute.Name, p.Attribute.ParameterType, pb =>
                                {
                                    pb.Summary      = p.Attribute.Description;
                                    pb.IsMultiple   = p.Attribute.IsMultiple;
                                    pb.IsRemainder  = p.Attribute.IsRemainder;
                                    pb.DefaultValue = p.Attribute.DefaultValue;
                                    pb.IsOptional   = !p.Attribute.Required;
                                });
                            }
                        }

                        textProperties.ModifyBuilder?.Invoke(scope.ServiceProvider, builder);
                    }
                                       );
                }
            }
        }