Add CheckCommandPerm to correct namespace

This commit is contained in:
Daniel_I_Am 2020-06-17 12:55:22 +02:00
parent dade5809fd
commit 9fba590893
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
3 changed files with 30 additions and 26 deletions

View File

@ -3,6 +3,7 @@ using Discord;
using Discord.Commands; using Discord.Commands;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using ChaosBot.Discord.PreConditions;
using NLog; using NLog;

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using NLog; using NLog;
using System.Text; using System.Text;
using ChaosBot.Database.Repository; using ChaosBot.Database.Repository;
using ChaosBot.Discord.PreConditions;
namespace ChaosBot.Discord.Modules namespace ChaosBot.Discord.Modules
{ {

View File

@ -1,46 +1,48 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot;
using ChaosBot.Database.Repository; using ChaosBot.Database.Repository;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
public class CheckCommandPerm : PreconditionAttribute namespace ChaosBot.Discord.PreConditions
{ {
private string _cmd { get; set; } public class CheckCommandPerm : PreconditionAttribute
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{ {
string requiredGroup = null; private string _cmd { get; set; }
if (context.User is SocketGuildUser gUser) public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{ {
var results = CommandPermissionRepository.getPerm(command.Name, Convert.ToInt64(context.Guild.Id)); string requiredGroup = null;
Program._logger.Info(command.Name); if (context.User is SocketGuildUser gUser)
if(results != null)
{ {
foreach (var perm in results) var results = CommandPermissionRepository.getPerm(command.Name, Convert.ToInt64(context.Guild.Id));
{
if (perm.type == "role")
requiredGroup = ConfigurationRepository.GetValue<string>($"Role:{perm.value}", context.Guild.Id) ?? perm.value;
else
requiredGroup = perm.value;
if (gUser.Roles.Any(r => r.Name == requiredGroup)) Program._logger.Info(command.Name);
return Task.FromResult(PreconditionResult.FromSuccess());
if(results != null)
{
foreach (var perm in results)
{
if (perm.type == "role")
requiredGroup = ConfigurationRepository.GetValue<string>($"Role:{perm.value}", context.Guild.Id) ?? perm.value;
else
requiredGroup = perm.value;
if (gUser.Roles.Any(r => r.Name == requiredGroup))
return Task.FromResult(PreconditionResult.FromSuccess());
}
} }
else
{
Program._logger.Info($"Value: Null Value");
}
return Task.FromResult(PreconditionResult.FromError($"You do not have access to this command."));
} }
else else
{ return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
Program._logger.Info($"Value: Null Value");
}
return Task.FromResult(PreconditionResult.FromError($"You do not have access to this command."));
} }
else
return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
} }
} }