public static async Task <(bool success, string message)> SetUserPermission(VCommandContext context, ulong userId, string permInput, bool givePerm) { if (!MatchModuleOrCommand(context.Bot, permInput, out string perm)) { return(false, $"Permission '{permInput}' is not valid"); } if (await context.Guild.FetchMemberAsync(userId) is not IMember user) { return(false, "User not found"); } ConcurrentSet <string> userPerms = context.Bot.Config .GetOrAddGuild(context.Guild.Id) .GetOrAddUserPerms(userId); // Give permission if (givePerm) { if (userPerms.Contains(perm)) { return(false, $"User {user.Name}#{user.Discriminator} already has permission '{perm}'"); } userPerms.Add(perm); return(true, $"Given permission '{perm}' to user {user.Name}#{user.Discriminator}"); } // Revoke permission if (!userPerms.Contains(perm)) { return(false, $"User {user.Name}#{user.Discriminator} does not have permission '{perm}'"); } userPerms.Remove(perm); return(true, $"Revoked permission '{perm}' from user {user.Name}#{user.Discriminator}"); }
public static async Task <(bool success, string message)> SetRolePermission(VCommandContext context, ulong roleId, string permInput, bool givePerm) { if (!MatchModuleOrCommand(context.Bot, permInput, out string perm)) { return(false, $"Permission '{permInput}' is not valid"); } if (await context.Bot.GetRoleAsync(context.GuildId, roleId) is not IRole role) { return(false, "Role not found"); } ConcurrentSet <string> rolePerms = context.Bot.Config .GetOrAddGuild(context.Guild.Id) .GetOrAddRolePerms(roleId); // Give permission if (givePerm) { if (rolePerms.Contains(perm)) { return(false, $"Role '{role.Name}' already has permission '{perm}'"); } rolePerms.Add(perm); return(true, $"Given permission '{perm}' to role '{role.Name}'"); } // Revoke permission if (!rolePerms.Contains(perm)) { return(false, $"Role '{role.Name}' does not have permission '{perm}'"); } rolePerms.Remove(perm); return(true, $"Revoked permission '{perm}' from role '{role.Name}'"); }
public static bool UserHasPermission(VCommandContext context) { ConfigGuild guild = context.Bot.Config.GetOrAddGuild(context.Guild.Id); // Check user perms ConcurrentSet <string> perms = guild.GetOrAddUserPerms(context.Author.Id); if (perms.Contains(context.Command.Module.Name) || perms.Contains(context.Command.Name)) { return(true); } // Check role perms foreach (ulong roleId in context.Author.RoleIds) { perms = guild.GetOrAddRolePerms(roleId); if (perms.Contains(context.Command.Module.Name) || perms.Contains(context.Command.Name)) { return(true); } } return(false); }