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); }
/// <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); }
/// <summary> /// Get all of the valid user-defined slash command modules /// </summary> public static async Task <IReadOnlyList <TypeInfo> > GetValidModuleClasses(Assembly assembly, SlashCommandService service) { var result = new List <TypeInfo>(); foreach (var typeInfo in assembly.DefinedTypes) { if (!IsValidModuleDefinition(typeInfo)) { continue; } // To simplify our lives, we need the modules to be public. if (typeInfo.IsPublic || typeInfo.IsNestedPublic) { result.Add(typeInfo); } else { await service.Logger.WarningAsync($"Found class {typeInfo.FullName} as a valid SlashCommand Module, but it's not public!"); } } return(result); }
public static void CreateSubCommandInfos(Dictionary <string, SlashCommandInfo> result, List <SlashModuleInfo> subCommandGroups, SlashCommandService slashCommandService) { foreach (var subCommandGroup in subCommandGroups) { // Create the commands that is on the same hierarchical level as this ... var commandInfos = CreateSameLevelCommands(result, subCommandGroup.ModuleType, subCommandGroup); subCommandGroup.SetCommands(commandInfos); // ... and continue with the lower sub command groups. CreateSubCommandInfos(result, subCommandGroup.commandGroups, slashCommandService); } }
/// <summary> /// Prepare all of the commands and register them internally. /// </summary> public static Dictionary <string, SlashCommandInfo> CreateCommandInfos(IReadOnlyList <TypeInfo> types, Dictionary <Type, SlashModuleInfo> moduleDefs, SlashCommandService slashCommandService) { // Create the resulting dictionary ahead of time var result = new Dictionary <string, SlashCommandInfo>(); // For each user-defined module ... foreach (var userModule in types) { // Get its associated information. If there isn't any it means something went wrong, but it's not a critical error. if (!moduleDefs.TryGetValue(userModule, out var moduleInfo)) { continue; } // Create the root-level commands var commandInfos = CreateSameLevelCommands(result, userModule, moduleInfo); moduleInfo.SetCommands(commandInfos); // Then create all of the command groups it has. CreateSubCommandInfos(result, moduleInfo.commandGroups, slashCommandService); } return(result); }
public SlashModuleInfo(SlashCommandService service, IServiceProvider services) { Service = service; ServiceProvider = services; }