/// <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>
        /// Load all commands from module.
        /// </summary>
        /// <param name="module"></param>
        void AddModule(YModule module)
        {
            MethodInfo[] methods = module.GetType().GetMethods();
            for (int i = 0; i < methods.Length; i++)
            {
                MethodInfo method = methods[i];

                if (method.GetCustomAttribute <Command>() != null)
                {
                    Bot.CommandManager.AddCommand(module, method);
                }
            }

            LoadedModules.Add(module);
        }
Beispiel #3
0
        public async Task ModifyConfig(string moduleName, string property, string value, bool save = true)
        {
            YModule module = Bot.ModuleManager.LoadedModules.Find(a => a.Name == moduleName);

            if (module is null)
            {
                await Channel.SendMessageAsync($"Module '{moduleName} does not exist'");

                return;
            }

            Config config = module?.GetType().GetCustomAttribute <Config>(true);

            if (config is null)
            {
                await Channel.SendMessageAsync($"Module '{moduleName} does not have a config class'");

                return;
            }

            Type         configType = module.GetConfig().GetType();
            PropertyInfo pInfo      = configType.GetProperty(property);
            FieldInfo    fInfo      = configType.GetField(property);

            if (pInfo != null)
            {
                pInfo.SetValue(module.GetConfig(), ParseParameter(value, pInfo.PropertyType));
            }

            if (fInfo != null)
            {
                fInfo.SetValue(module.GetConfig(), ParseParameter(value, fInfo.FieldType));
            }


            await Channel.SendMessageAsync($"Config updated");

            if (save)
            {
                await Bot.ModuleManager.SaveConfig();

                await Channel.SendMessageAsync($"Config saved");
            }
        }
        /// <summary>
        /// Load or create config for module.
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        async Task <object> LoadConfig(YModule module)
        {
            Config configAttribute = module.GetType().GetCustomAttribute <Config>();

            if (configAttribute != null)
            {
                Directory.CreateDirectory("Config/Modules");

                string path = $"Config/Modules/{module.Name}.json";
                if (File.Exists(path))
                {
                    object config;
                    string json;
                    using (StreamReader reader = new StreamReader(path))
                    {
                        json = await reader.ReadToEndAsync().ConfigureAwait(false);

                        config = JToken.Parse(json).ToObject(configAttribute.Type);
                    }

                    // Update config file JSON in case there is a new parameter
                    json = JsonConvert.SerializeObject(config, Formatting.Indented);
                    await File.WriteAllTextAsync(path, json);

                    return(config);
                }
                else
                {
                    object instance = Activator.CreateInstance(configAttribute.Type);
                    string json     = JsonConvert.SerializeObject(instance, Formatting.Indented);
                    using (StreamWriter writer = File.CreateText(path))
                    {
                        await writer.WriteLineAsync(json).ConfigureAwait(false);
                    }

                    return(instance);
                }
            }

            return(null);
        }
Beispiel #5
0
        public async Task DisplayConfig(string moduleName)
        {
            YModule module = Bot.ModuleManager.LoadedModules.Find(a => a.Name == moduleName);

            if (module is null)
            {
                await Channel.SendMessageAsync($"Module '{moduleName} does not exist'");

                return;
            }

            Config config = module?.GetType().GetCustomAttribute <Config>(true);

            if (config is null)
            {
                await Channel.SendMessageAsync($"Module '{moduleName} does not have a config class'");

                return;
            }

            await Channel.SendMessageAsync($"```{DisplayConfig(module.GetConfig())}```");
        }