public ReflectionCommand(
            string id,
            IReadOnlyList <string> aliases,
            Type moduleType,
            MethodInfo method,
            IReadOnlyList <ICommandValueProvider> valueProviders)
        {
            var attribute = method.GetCustomAttribute <CommandAttribute>();

            Id              = id;
            _moduleType     = moduleType;
            _method         = method;
            _valueProviders = valueProviders;
            Aliases         = aliases;
            Module          = ModuleUtils.GetModuleName(moduleType);
            Permission      = ModuleUtils.GetPermissionName(moduleType, method);
            GuildOnly       = attribute.GuildOnly;
            PermissionGroup = attribute.PermissionGroup;
        }
Exemple #2
0
        /// <summary>
        ///     Create a class that implements <see cref="ICommand"/>.
        ///
        ///     The implementation will execute the given <see cref="method"/>.
        /// </summary>
        /// <param name="id">The unique identifier of the command for <see cref="ICommand.Id"/>.</param>
        /// <param name="className">The new class name.</param>
        /// <param name="moduleType">The type that contains the <see cref="method"/>.</param>
        /// <param name="method">The method that should be executed when <see cref="ICommand.ExecuteAsync"/> is called.</param>
        /// <returns>The <see cref="ClassDeclarationSyntax"/>.</returns>
        private ClassDeclarationSyntax CreateCommandClass(
            string id,
            IReadOnlyList <string> aliases,
            string className,
            Type moduleType,
            MethodInfo method)
        {
            var permission      = ModuleUtils.GetPermissionName(moduleType, method);
            var moduleName      = ModuleUtils.GetModuleName(moduleType);
            var attr            = method.GetCustomAttribute <CommandAttribute>();
            var guildOnly       = attr.GuildOnly;
            var permissionGroup = attr.PermissionGroup.ToString();

            return(S.ClassDeclaration(className)
                   .AddBaseListTypes(S.SimpleBaseType(CommandType))
                   .AddMembers(
                       // ICommand.Id
                       S.PropertyDeclaration(StringType, nameof(ICommand.Id))
                       .AddAccessorListAccessors(
                           S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                           .AddBodyStatements(S.ReturnStatement(S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(id))))
                           .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken))
                           )
                       .AddModifiers(S.Token(SyntaxKind.PublicKeyword)),

                       // ICommand.Aliases
                       S.PropertyDeclaration(ReadOnlyStringList, nameof(ICommand.Aliases))
                       .AddAccessorListAccessors(
                           S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                           .AddBodyStatements(S.ReturnStatement(S.ArrayCreationExpression(
                                                                    S.ArrayType(StringType)
                                                                    .WithRankSpecifiers(S.SingletonList(S.ArrayRankSpecifier(S.SingletonSeparatedList <ExpressionSyntax>(S.OmittedArraySizeExpression())))),
                                                                    S.InitializerExpression(SyntaxKind.ArrayInitializerExpression,
                                                                                            new SeparatedSyntaxList <ExpressionSyntax>()
                                                                                            .AddRange(aliases.Select(s => S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(s))))))))
                           .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken))
                           )
                       .AddModifiers(S.Token(SyntaxKind.PublicKeyword)),

                       // ICommand.Module
                       S.PropertyDeclaration(StringType, nameof(ICommand.Module))
                       .AddAccessorListAccessors(
                           S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                           .AddBodyStatements(S.ReturnStatement(S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(moduleName))))
                           .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken))
                           )
                       .AddModifiers(S.Token(SyntaxKind.PublicKeyword)),

                       // ICommand.Permission
                       S.PropertyDeclaration(StringType, nameof(ICommand.Permission))
                       .AddAccessorListAccessors(
                           S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                           .AddBodyStatements(S.ReturnStatement(S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(permission))))
                           .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken))
                           )
                       .AddModifiers(S.Token(SyntaxKind.PublicKeyword)),

                       // ICommand.PermissionGroup
                       S.PropertyDeclaration(S.ParseName(nameof(PermissionGroup)), nameof(ICommand.PermissionGroup))
                       .AddAccessorListAccessors(
                           S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                           .AddBodyStatements(S.ReturnStatement(S.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, S.IdentifierName(nameof(PermissionGroup)), S.IdentifierName(permissionGroup))))
                           .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken))
                           )
                       .AddModifiers(S.Token(SyntaxKind.PublicKeyword)),

                       // ICommand.GuildOnly
                       S.PropertyDeclaration(BoolType, nameof(ICommand.GuildOnly))
                       .AddAccessorListAccessors(
                           S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                           .AddBodyStatements(S.ReturnStatement(S.LiteralExpression(guildOnly ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression)))
                           .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken))
                           )
                       .AddModifiers(S.Token(SyntaxKind.PublicKeyword)),

                       // ICommand.ExecuteAsync
                       S.MethodDeclaration(TaskType, nameof(ICommand.ExecuteAsync))
                       .AddModifiers(S.Token(SyntaxKind.PublicKeyword), S.Token(SyntaxKind.AsyncKeyword))
                       .AddParameterListParameters(Parameter(ArgumentContext, MessageContextType))
                       .AddBodyStatements(
                           S.ExpressionStatement(InvokeCommand(moduleType, method))
                           )
                       ));
        }