Enable the possibility to disable a module

This commit is contained in:
Daniel_I_Am 2020-08-13 21:11:06 +02:00
parent 1521df23fd
commit ff854df99d
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChaosBot.Models;
using ChaosBot.Repositories;
using Discord.Commands;
using Discord.WebSocket;
using NLog;
namespace ChaosBot.Discord.PreConditions
{
public class CheckModuleEnabled : PreconditionAttribute
{
private static ILogger _logger = Program.Logger;
private readonly string _moduleName;
public CheckModuleEnabled(string moduleName) => _moduleName = moduleName;
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
// Debug information
_logger.Trace($"CheckModuleEnabled.CheckPermissionsAsync|Checking module enabled for module: {_moduleName}");
if (context.Guild == null) return Task.FromResult(PreconditionResult.FromError("This must be run in a guild."));
// Check if module enabled in database
bool isModuleEnabled = GetResult(context, _moduleName);
if (isModuleEnabled)
return Task.FromResult(PreconditionResult.FromSuccess());
// Permission denied
return Task.FromResult(PreconditionResult.FromError($"You do not have access to this command."));
}
public static bool GetResult(ICommandContext context, string moduleName)
{
if (context.Guild == null) throw new Exception("This must be run in a guild");
// Check if module enabled in database
return Convert.ToBoolean(ConfigurationRepository.GetValue<string>($"Module:{moduleName}:Enabled",
context.Guild.Id, "true"));
}
}
}