Testing user info part 2
This commit is contained in:
@@ -45,9 +45,33 @@ public class LightlessModule : InteractionModuleBase
|
||||
{
|
||||
EmbedBuilder eb = new();
|
||||
|
||||
eb = await HandleUserInfo(eb, Context.User.Id, secondaryUid, discordUser?.Id ?? null, uid);
|
||||
using var scope = _services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<LightlessDbContext>();
|
||||
await using (db.ConfigureAwait(false))
|
||||
{
|
||||
eb = await HandleUserInfo(eb, db, Context.User.Id, secondaryUid, discordUser?.Id ?? null, uid);
|
||||
|
||||
await RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true).ConfigureAwait(false);
|
||||
if(eb.Fields.Any(f => string.Equals(f.Name, "Profile Description", StringComparison.Ordinal)))
|
||||
{
|
||||
string uidToGet = await GetUserUID(db, secondaryUid, discordUser?.Id ?? null, uid).ConfigureAwait(false);
|
||||
byte[] profileImage = await GetProfileImage(db, uid).ConfigureAwait(false);
|
||||
using (MemoryStream ms = new(profileImage))
|
||||
{
|
||||
eb.WithImageUrl("attachment://profileimage.png");
|
||||
await RespondWithFileAsync(
|
||||
ms,
|
||||
"profileimage.png",
|
||||
embeds: new[] { eb.Build() },
|
||||
ephemeral: true).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await RespondAsync(
|
||||
embeds: new[] { eb.Build() },
|
||||
ephemeral: true).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -423,12 +447,20 @@ public class LightlessModule : InteractionModuleBase
|
||||
|
||||
return embed.Build();
|
||||
}
|
||||
|
||||
private async Task<byte[]> GetProfileImage(LightlessDbContext db, string uid)
|
||||
{
|
||||
var profile = await db.UserProfileData.Where(u => u.UserUID == uid).SingleOrDefaultAsync().ConfigureAwait(false);
|
||||
if (profile != null && profile.Base64ProfileImage != null && profile.Base64ProfileImage.Length > 0)
|
||||
{
|
||||
return Convert.FromBase64String(profile.Base64ProfileImage);
|
||||
}
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
private async Task<EmbedBuilder> HandleUserInfo(EmbedBuilder eb, ulong id, string? secondaryUserUid = null, ulong? optionalUser = null, string? uid = null)
|
||||
private async Task<EmbedBuilder> HandleUserInfo(EmbedBuilder eb, LightlessDbContext db, ulong id, string? secondaryUserUid = null, ulong? optionalUser = null, string? uid = null)
|
||||
{
|
||||
bool showForSecondaryUser = secondaryUserUid != null;
|
||||
using var scope = _services.CreateScope();
|
||||
await using var db = scope.ServiceProvider.GetRequiredService<LightlessDbContext>();
|
||||
|
||||
var primaryUser = await db.LodeStoneAuth.Include(u => u.User).SingleOrDefaultAsync(u => u.DiscordId == id).ConfigureAwait(false);
|
||||
|
||||
@@ -540,4 +572,39 @@ public class LightlessModule : InteractionModuleBase
|
||||
|
||||
return eb;
|
||||
}
|
||||
|
||||
private async Task<string> GetUserUID(LightlessDbContext db, string? secondaryUserUid = null, ulong? optionalUser = null, string? uid = null)
|
||||
{
|
||||
var primaryUser = await db.LodeStoneAuth.Include(u => u.User).SingleOrDefaultAsync(u => u.DiscordId == Context.User.Id).ConfigureAwait(false);
|
||||
ulong userToCheckForDiscordId = Context.User.Id;
|
||||
|
||||
if ((optionalUser != null || uid != null))
|
||||
{
|
||||
LodeStoneAuth userInDb = null;
|
||||
if (optionalUser != null)
|
||||
{
|
||||
userInDb = await db.LodeStoneAuth.Include(u => u.User).SingleOrDefaultAsync(u => u.DiscordId == optionalUser).ConfigureAwait(false);
|
||||
}
|
||||
else if (uid != null)
|
||||
{
|
||||
userInDb = await db.LodeStoneAuth.Include(u => u.User).SingleOrDefaultAsync(u => u.User.UID == uid || u.User.Alias == uid).ConfigureAwait(false);
|
||||
}
|
||||
if (userInDb == null)
|
||||
{
|
||||
throw new Exception("The Discord user has no valid Lightless account");
|
||||
}
|
||||
userToCheckForDiscordId = userInDb.DiscordId;
|
||||
}
|
||||
var lodestoneUser = await db.LodeStoneAuth.Include(u => u.User).SingleOrDefaultAsync(u => u.DiscordId == userToCheckForDiscordId).ConfigureAwait(false);
|
||||
var dbUser = lodestoneUser.User;
|
||||
if (secondaryUserUid != null)
|
||||
{
|
||||
dbUser = (await db.Auth.Include(u => u.User).SingleOrDefaultAsync(u => u.PrimaryUserUID == dbUser.UID && u.UserUID == secondaryUserUid))?.User;
|
||||
if (dbUser == null)
|
||||
{
|
||||
throw new Exception($"A secondary UID {secondaryUserUid} was not found attached to your primary UID {primaryUser.User.UID}.");
|
||||
}
|
||||
}
|
||||
return dbUser.UID;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user