Exemplo n.º 1
0
        static void BuildChildCommandsForCommand(ChatCommand command, Type type)
        {
            foreach (var nestedType in type.GetNestedTypes(BindingFlags.NonPublic))
            {
                var groupAttribute = nestedType.GetCustomAttribute <CommandGroupAttribute>(true);
                if (groupAttribute == null)
                {
                    continue;
                }

                ChatCommand childCommand = new(groupAttribute);
                BuildChildCommandsForCommand(childCommand, nestedType);
                command.AddChildCommand(childCommand);
            }

            foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic))
            {
                CommandAttribute commandAttribute = method.GetCustomAttribute <CommandAttribute>(false);
                if (commandAttribute == null)
                {
                    continue;
                }

                if (commandAttribute.GetType() == typeof(CommandNonGroupAttribute))
                {
                    continue;
                }

                command.AddChildCommand(new ChatCommand(commandAttribute, method));
            }

            command.SortChildCommands();
        }
Exemplo n.º 2
0
        public ChatCommand(Type type, CommandAttribute attribute)
        {
            Name         = attribute.Name;
            Permission   = attribute.RBAC;
            AllowConsole = attribute.AllowConsole;
            Help         = attribute.Help;

            foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic))
            {
                CommandAttribute commandAttribute = method.GetCustomAttribute <CommandAttribute>(false);
                if (commandAttribute == null)
                {
                    continue;
                }

                if (commandAttribute.GetType() == typeof(CommandNonGroupAttribute))
                {
                    continue;
                }

                ChildCommands.Add(new ChatCommand(commandAttribute, (HandleCommandDelegate)method.CreateDelegate(typeof(HandleCommandDelegate))));
            }

            foreach (var nestedType in type.GetNestedTypes(BindingFlags.NonPublic))
            {
                var groupAttribute = nestedType.GetCustomAttribute <CommandGroupAttribute>(true);
                if (groupAttribute == null)
                {
                    continue;
                }

                ChildCommands.Add(new ChatCommand(nestedType, groupAttribute));
            }

            ChildCommands = ChildCommands.OrderBy(p => string.IsNullOrEmpty(p.Name)).ThenBy(p => p.Name).ToList();
        }