public virtual async Task SetModuleEnabledAsync(ulong guildId, string casedModuleName, bool enabled)
        {
            var moduleName = ModuleNames.FirstOrDefault(name => name.Equals(casedModuleName, StringComparison.OrdinalIgnoreCase));

            if (string.IsNullOrEmpty(moduleName))
            {
                throw new KeyNotFoundException("The module could not be found.");
            }

            using var scope = _provider.CreateScope();
            var repo = scope.ServiceProvider.GetService <IGuildModuleRepository>();

            if (repo == null)
            {
                throw new NotSupportedException($"There was no {nameof(IGuildModuleRepository)} registered. {nameof(SetModuleEnabledAsync)} is not available.");
            }

            var enabledGuildModule = await repo.GetAsync(guildId, moduleName);

            if (CoreModuleNames.Contains(moduleName))
            {
                throw new InvalidOperationException($"The module {moduleName} is a core module and cannot be disabled");
            }

            if (enabledGuildModule == null)
            {
                enabledGuildModule = new GuildModule
                {
                    GuildId = guildId,
                    Name    = moduleName,
                    Enabled = enabled
                };
                repo.Add(enabledGuildModule);
            }
            else
            {
                enabledGuildModule.Enabled = enabled;
                repo.Update(enabledGuildModule);
            }

            await repo.SaveChangesAsync();

            await _cache.RemoveAsync(CacheKey.GetEnabledModulesCacheKey(guildId));
        }
Exemple #2
0
 static ModuleType ModuleTypeFromString(string moduleName)
 {
     return(ModuleNames.FirstOrDefault(tuple => tuple.name == moduleName).module);
 }