//public SlashCommandInfo(SlashModuleInfo module, string name, string description,List<SlashParameterInfo> parameters , Delegate userMethod , bool isGlobal = false)
        public SlashCommandInfo(SlashModuleInfo module, string name, string description, List <SlashParameterInfo> parameters, MethodInfo methodInfo, bool isGlobal = false)
        {
            Module      = module;
            Name        = name;
            Description = description;
            Parameters  = parameters;
            //this.userMethod = userMethod;
            IsGlobal   = isGlobal;
            MethodInfo = methodInfo;
            //this.callback = new Func<object[], Task<IResult>>(async (args) =>
            //{
            //    // Try-catch it and see what we get - error or success
            //    try
            //    {
            //        await Task.Run(() =>
            //        {
            //            userMethod.DynamicInvoke(args);
            //        }).ConfigureAwait(false);
            //    }
            //    catch(Exception e)
            //    {
            //        return ExecuteResult.FromError(e);
            //    }
            //    return ExecuteResult.FromSuccess();

            //});
        }
Example #2
0
        public static List <SlashModuleInfo> InstantiateSubModules(Type rootModule, SlashModuleInfo rootModuleInfo, SlashCommandService slashCommandService, IServiceProvider services)
        {
            // Instantiate all of the nested modules.
            var commandGroups = new List <SlashModuleInfo>();

            foreach (var commandGroupType in rootModule.GetNestedTypes())
            {
                if (!TryGetCommandGroupAttribute(commandGroupType, out var commandGroup))
                {
                    continue;
                }
                //var commandGroupTypeInfo = commandGroupType.GetTypeInfo();

                var groupInfo = new SlashModuleInfo(slashCommandService, services);
                groupInfo.SetModuleType(commandGroupType);

                //var instance = ReflectionUtils.CreateObject<ISlashCommandModule>(commandGroupTypeInfo, slashCommandService, services);
                //object instance = commandGroupType.GetConstructor(Type.EmptyTypes).Invoke(null);
                //groupInfo.SetCommandModule(instance);

                groupInfo.MakeCommandGroup(commandGroup, rootModuleInfo);
                groupInfo.MakePath();
                groupInfo.isGlobal = IsCommandModuleGlobal(commandGroupType);

                groupInfo.SetSubCommandGroups(InstantiateSubModules(commandGroupType, groupInfo, slashCommandService, services));
                commandGroups.Add(groupInfo);
            }

            return(commandGroups);
        }
Example #3
0
        /// <summary>
        /// Create an instance of each user-defined module
        /// </summary>
        public static Dictionary <Type, SlashModuleInfo> InstantiateModules(IReadOnlyList <TypeInfo> types, SlashCommandService slashCommandService, IServiceProvider services)
        {
            var result = new Dictionary <Type, SlashModuleInfo>();

            // Here we get all modules thate are NOT sub command groups and instantiate them.
            foreach (var userModuleType in types)
            {
                //var userModuleTypeInfo = userModuleType.GetTypeInfo();

                var moduleInfo = new SlashModuleInfo(slashCommandService, services);
                moduleInfo.SetModuleType(userModuleType);

                // If they want a constructor with different parameters, this is the place to add them.
                //var instance = ReflectionUtils.CreateObject<ISlashCommandModule>(userModuleTypeInfo, slashCommandService, services);
                //object instance = userModuleType.GetConstructor(Type.EmptyTypes).Invoke(null);
                //moduleInfo.SetCommandModule(instance);
                moduleInfo.isGlobal = IsCommandModuleGlobal(userModuleType);

                moduleInfo.SetSubCommandGroups(InstantiateSubModules(userModuleType, moduleInfo, slashCommandService, services));
                result.Add(userModuleType, moduleInfo);
            }

            return(result);
        }
Example #4
0
        private static List <SlashCommandInfo> CreateSameLevelCommands(Dictionary <string, SlashCommandInfo> result, Type userModule, SlashModuleInfo moduleInfo)
        {
            var commandMethods = userModule.GetMethods();
            var commandInfos   = new List <SlashCommandInfo>();

            foreach (var commandMethod in commandMethods)
            {
                // Get the SlashCommand attribute
                if (!IsValidSlashCommand(commandMethod, out var slashCommand))
                {
                    continue;
                }

                // Create the delegate for the method we want to call once the user interacts with the bot.
                // We use a delegate because of the unknown number and type of parameters we will have.
                //Delegate delegateMethod = CreateDelegate(commandMethod, moduleInfo.userCommandModule);
                var commandInfo = new SlashCommandInfo(
                    moduleInfo,
                    slashCommand.Name,
                    slashCommand.Description,
                    // Generate the parameters. Due to it's complicated way the algorithm has been moved to its own function.
                    ConstructCommandParameters(commandMethod),
                    //userMethod: delegateMethod,
                    commandMethod,
                    IsCommandGlobal(commandMethod)
                    );

                result.Add(commandInfo.Module.Path + SlashModuleInfo.PathSeperator + commandInfo.Name, commandInfo);
                commandInfos.Add(commandInfo);
            }

            return(commandInfos);
        }
Example #5
0
 public void MakeCommandGroup(CommandGroup commandGroupInfo, SlashModuleInfo parent)
 {
     isCommandGroup        = true;
     this.commandGroupInfo = commandGroupInfo;
     this.parent           = parent;
 }