added needed rights for slash commands

added functions for account select in the bot
This commit is contained in:
Sarah Faey 2022-11-29 00:04:22 +01:00
parent 62bacb5ad7
commit dc2563e16c
11 changed files with 876 additions and 13 deletions

View file

@ -117,6 +117,7 @@ namespace Lieb.Data
if (signUpType != SignUpType.Flex && signUps.Where(r => r.SignUpType != SignUpType.Flex).Any())
{
await ChangeSignUpType(raidId, liebUserId, plannedRoleId, signUpType);
await ChangeAccount(raidId, liebUserId, guildWars2AccountId);
}
else if (!signUps.Where(r => r.RaidRoleId == plannedRoleId).Any())
{
@ -417,6 +418,23 @@ namespace Lieb.Data
return true;
}
public bool IsRaidSlashCommandAllowed(ulong liebUserId, out string errorMessage)
{
errorMessage = string.Empty;
using var context = _contextFactory.CreateDbContext();
LiebUser user = context.LiebUsers
.Include(u => u.RoleAssignments)
.ThenInclude(a => a.LiebRole)
.FirstOrDefault(u => u.Id == liebUserId);
if (user != null && user.RoleAssignments.Max(a => a.LiebRole.Level) >= Constants.Roles.RaidLead.PowerLevel)
{
return true;
}
errorMessage = "insufficient permissions";
return false;
}
private async Task LogSignUp(RaidSignUp signUp, string userName, ulong signedUpBy = 0)
{
ulong userId = signedUpBy > 0 ? signedUpBy : signUp.LiebUserId.Value;

View file

@ -1,5 +1,6 @@
using Lieb.Models;
using Lieb.Models.GuildWars2;
using Lieb.Models.GuildWars2.Raid;
using Microsoft.EntityFrameworkCore;
namespace Lieb.Data
@ -185,5 +186,66 @@ namespace Lieb.Data
await context.SaveChangesAsync();
}
}
public List<GuildWars2Account> GetAllUsableAccounts(ulong userId, RaidType raidType)
{
LiebUser user = GetLiebUserGW2AccountOnly(userId);
return GetAllUsableAccounts(user, raidType);
}
public List<GuildWars2Account> GetAllUsableAccounts(LiebUser user, RaidType raidType)
{
if (raidType == RaidType.Planned)
{
return user.GuildWars2Accounts.ToList();
}
else
{
return user.GuildWars2Accounts.Where(a => a.EquippedBuilds.Count > 0).ToList();
}
}
public List<GuildWars2Account> GetDiscordSignUpAccounts(ulong userId, int raidId)
{
using var context = _contextFactory.CreateDbContext();
LiebUser user = GetLiebUserGW2AccountOnly(userId);
Raid raid = context.Raids
.ToList()
.FirstOrDefault(r => r.RaidId == raidId, new Raid());
List<GuildWars2Account> accounts = GetAllUsableAccounts(user, raid.RaidType);
if(user.AlwaysSignUpWithMainAccount && accounts.Where(a => a.GuildWars2AccountId == user.MainGW2Account).Any())
{
return accounts.Where(a => a.GuildWars2AccountId == user.MainGW2Account).ToList();
}
else
{
return accounts;
}
}
public int GetSignUpAccount(ulong userId, int raidId, int plannedAccountId)
{
using var context = _contextFactory.CreateDbContext();
LiebUser user = GetLiebUserGW2AccountOnly(userId);
Raid raid = context.Raids
.ToList()
.FirstOrDefault(r => r.RaidId == raidId, new Raid());
List<GuildWars2Account> usableAccounts = GetAllUsableAccounts(user, raid.RaidType);
if(usableAccounts.Where(a => a.GuildWars2AccountId == plannedAccountId).Any())
{
return plannedAccountId;
}
if(usableAccounts.Where(a => a.GuildWars2AccountId == user.MainGW2Account).Any())
{
return user.MainGW2Account;
}
else
{
return usableAccounts.First().GuildWars2AccountId;
}
}
}
}