diff --git a/ChaosBot/Database/Repository/CommandPermissionRepository.cs b/ChaosBot/Database/Repository/CommandPermissionRepository.cs new file mode 100644 index 0000000..2dd1cea --- /dev/null +++ b/ChaosBot/Database/Repository/CommandPermissionRepository.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using ChaosBot.Database.Entity; +using Microsoft.Data.Sqlite; + +namespace ChaosBot.Database.Repository +{ + public static class CommandPermissionRepository + { + private static readonly string Table = "CommandPermissions"; + + /// + /// Fetch all CommandPermissions filtered by guildId + /// + /// + /// List of Commands and Permissions + public static CommandPermissions[] All(long guildId) + { + var cmds = CommandPermissions.Query().Where("guildId", guildId).All(); + + return cmds.ToArray(); + } + + /// + /// Get all CommandPermissions for a command filtered by guild + /// + /// + /// + /// List of raffles + public static CommandPermissions[] getPerm(string cmd, long guildId) + { + List cmds = CommandPermissions.Query().Where("cmd", cmd).Where("guildId", guildId).Get(); + + if(cmds.Count != 0) return cmds.ToArray(); + + return null; + } + } +} \ No newline at end of file diff --git a/ChaosBot/Discord/Modules/AdminCommands.cs b/ChaosBot/Discord/Modules/AdminCommands.cs index 83d3f88..f8806ec 100644 --- a/ChaosBot/Discord/Modules/AdminCommands.cs +++ b/ChaosBot/Discord/Modules/AdminCommands.cs @@ -16,7 +16,8 @@ namespace ChaosBot.Discord.Modules [Command("clear")] [Alias("purge")] [RequireBotPermission(GuildPermission.ManageMessages)] - [RequireUserPermission(GuildPermission.ManageMessages)] + [RequireUserPermission(GuildPermission.ManageMessages)] + [CheckCommandPerm] public async Task ClearCommand(int msgtoDelete = 1) { try diff --git a/ChaosBot/Discord/PreConditions/CheckCommandPerm.cs b/ChaosBot/Discord/PreConditions/CheckCommandPerm.cs new file mode 100644 index 0000000..f37ed61 --- /dev/null +++ b/ChaosBot/Discord/PreConditions/CheckCommandPerm.cs @@ -0,0 +1,46 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using ChaosBot; +using ChaosBot.Database.Repository; +using Discord.Commands; +using Discord.WebSocket; + +public class CheckCommandPerm : PreconditionAttribute +{ + private string _cmd { get; set; } + + public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) + { + string requiredGroup = null; + + if (context.User is SocketGuildUser gUser) + { + var results = CommandPermissionRepository.getPerm(command.Name, Convert.ToInt64(context.Guild.Id)); + + Program._logger.Info(command.Name); + + if(results != null) + { + foreach (var perm in results) + { + if (perm.type == "role") + requiredGroup = ConfigurationRepository.GetValue($"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 + return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command.")); + } +} \ No newline at end of file