/// <summary> /// Shut down all modules. /// </summary> /// <returns></returns> internal async Task ShutdownModules() { for (int i = 0; i < LoadedModules.Count; i++) { YModule module = LoadedModules[i]; await Task.Run(() => module.RunCommand("Shutdown", 0)).ConfigureAwait(false); // Make sure one slow shutdown stops every other module from getting the call } }
/// <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); } } }