Adding new DB Command and functions
This commit is contained in:
parent
703e5ae411
commit
7f0524b947
41
ChaosBot/Database/Repository/CommandPermissionRepository.cs
Normal file
41
ChaosBot/Database/Repository/CommandPermissionRepository.cs
Normal file
@ -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";
|
||||
|
||||
/// <summary>
|
||||
/// Fetch all <c>CommandPermission</c>s filtered by guildId
|
||||
/// </summary>
|
||||
/// <param name="guildId"></param>
|
||||
/// <returns>List of Commands and Permissions</returns>
|
||||
public static CommandPermissions[] All(long guildId)
|
||||
{
|
||||
var cmds = CommandPermissions.Query().Where("guildId", guildId).All();
|
||||
|
||||
return cmds.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all <c>CommandPermission</c>s for a command filtered by guild
|
||||
/// </summary>
|
||||
/// <param name="cmd"></param>
|
||||
/// <param name="guildId"></param>
|
||||
/// <returns>List of raffles</returns>
|
||||
public static CommandPermissions[] getPerm(string cmd, long guildId)
|
||||
{
|
||||
List<CommandPermissions> cmds = CommandPermissions.Query().Where("cmd", cmd).Where("guildId", guildId).Get();
|
||||
|
||||
if(cmds.Count != 0) return cmds.ToArray();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
46
ChaosBot/Discord/PreConditions/CheckCommandPerm.cs
Normal file
46
ChaosBot/Discord/PreConditions/CheckCommandPerm.cs
Normal file
@ -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<PreconditionResult> 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<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
|
||||
return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user