Adding Dynamic Permission PreCondition node
This commit is contained in:
parent
843e9f4808
commit
d7c997f0cf
@ -2,7 +2,6 @@
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using NLog;
|
||||
using System.Text;
|
||||
using ChaosBot.Database.Repository;
|
||||
@ -15,8 +14,7 @@ namespace ChaosBot.Discord.Modules
|
||||
private static readonly string _prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
|
||||
|
||||
[Command("config")]
|
||||
[RequireBotPermission(GuildPermission.ManageGuild)]
|
||||
[RequireUserPermission(GuildPermission.ManageGuild)]
|
||||
[RequireRole("Admin")]
|
||||
public async Task setConfig(string configFlag = null, string value = null)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
42
ChaosBot/Discord/Services/RequireRole.cs
Normal file
42
ChaosBot/Discord/Services/RequireRole.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using ChaosBot;
|
||||
using ChaosBot.Database.Repository;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
public class RequireRole : PreconditionAttribute
|
||||
{
|
||||
private string _name { get; set; }
|
||||
|
||||
public RequireRole(string name) => _name = name;
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||
{
|
||||
if (context.User is SocketGuildUser gUser)
|
||||
{
|
||||
switch (_name)
|
||||
{
|
||||
case "Admin":
|
||||
_name = ConfigurationRepository.GetValue<string>("Role:Admin", context.Guild.Id);
|
||||
break;
|
||||
case "Officer":
|
||||
_name = ConfigurationRepository.GetValue<string>("Role:Officer", context.Guild.Id);
|
||||
break;
|
||||
case "Member":
|
||||
_name = ConfigurationRepository.GetValue<string>("Role:Member", context.Guild.Id);
|
||||
break;
|
||||
default:
|
||||
return Task.FromResult(PreconditionResult.FromError($"{_name} is not a valid Permission Node."));
|
||||
}
|
||||
|
||||
if (gUser.Roles.Any(r => r.Name == _name))
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
else
|
||||
return Task.FromResult(PreconditionResult.FromError($"You must have a role named {_name} to run this command."));
|
||||
}
|
||||
else
|
||||
return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user