/// <summary> /// Run init on all modules and create ConfigFiles. /// </summary> /// <returns></returns> internal async Task InitializeModules() { for (int i = 0; i < LoadedModules.Count; i++) { YModule module = LoadedModules[i]; RequiredModule requiredModule = module.GetType().GetCustomAttribute <RequiredModule>(); if (requiredModule != null) { for (int a = 0; a < requiredModule.Types.Count; a++) { Type type = requiredModule.Types[a]; if (!LoadedModules.Exists(m => m.GetType() == type)) { await Bot.LoggingManager.LogMessage(LogLevel.Warning, $"Unable to initalize module {module.Name} it requires {type.Name} wich is not loaded.", "ModuleManager").ConfigureAwait(false); return; } } } module.SetContext(new MethodContext(Client.Guilds.FirstOrDefault(), null, null)); await module.InitModule().ConfigureAwait(false); } for (int i = 0; i < LoadedModules.Count; i++) { YModule module = LoadedModules[i]; await module.ModuleDone(); } }
/// <summary> /// Run a method on all modules /// </summary> /// <param name="name"></param> /// <param name="parameters"></param> /// <returns></returns> internal async Task RunMethod(string name, Func <YModule, bool> validate, params object[] parameters) { IGuild guild = null; ISocketMessageChannel channel = null; IMessage message = null; foreach (object item in parameters) { if (item is IGuild) { guild = item as IGuild; } if (item is ISocketMessageChannel) { channel = item as ISocketMessageChannel; } if (item is IMessage) { message = item as IMessage; channel = message?.Channel as ISocketMessageChannel; } } MethodContext context = new MethodContext(guild, channel, message); for (int i = 0; i < LoadedModules.Count; i++) { YModule module = LoadedModules[i]; if (!validate(module)) { continue; } try { module.SetContext(context); await module.RunCommand(name, parameters.Length, parameters).ConfigureAwait(false); } catch (Exception ex) { await Bot.LoggingManager.LogMessage(LogLevel.Error, $"Unable to run method {name}:", "ModuleManager").ConfigureAwait(false); await Bot.LoggingManager.LogMessage(ex, "ModuleManager").ConfigureAwait(false); } } }