handle forbidden

This commit is contained in:
Stanley Dimant
2024-08-20 21:42:15 +02:00
parent fd95aafb46
commit 7b37be5264
2 changed files with 19 additions and 7 deletions

View File

@@ -145,6 +145,7 @@ public partial class MareWizardModule
eb.WithTitle("Failed to verify registration"); eb.WithTitle("Failed to verify registration");
eb.WithDescription("The bot was not able to find the required verification code on your Lodestone profile." + Environment.NewLine + Environment.NewLine eb.WithDescription("The bot was not able to find the required verification code on your Lodestone profile." + Environment.NewLine + Environment.NewLine
+ "Please restart your verification process, make sure to save your profile _twice_ for it to be properly saved." + Environment.NewLine + Environment.NewLine + "Please restart your verification process, make sure to save your profile _twice_ for it to be properly saved." + Environment.NewLine + Environment.NewLine
+ "**Make sure your profile is set to public (All Users) for your character. The bot cannot read logged in or private profiles." + Environment.NewLine + Environment.NewLine
+ "The code the bot is looking for is" + Environment.NewLine + Environment.NewLine + "The code the bot is looking for is" + Environment.NewLine + Environment.NewLine
+ "**" + verificationCode + "**"); + "**" + verificationCode + "**");
cb.WithButton("Cancel", "wizard-register", emote: new Emoji("❌")); cb.WithButton("Cancel", "wizard-register", emote: new Emoji("❌"));
@@ -208,16 +209,16 @@ public partial class MareWizardModule
private async Task HandleVerifyAsync(ulong userid, string authString, DiscordBotServices services) private async Task HandleVerifyAsync(ulong userid, string authString, DiscordBotServices services)
{ {
var req = new HttpClient(); using var req = new HttpClient();
services.DiscordVerifiedUsers.Remove(userid, out _); services.DiscordVerifiedUsers.Remove(userid, out _);
if (services.DiscordLodestoneMapping.ContainsKey(userid)) if (services.DiscordLodestoneMapping.ContainsKey(userid))
{ {
var randomServer = services.LodestoneServers[random.Next(services.LodestoneServers.Length)]; var randomServer = services.LodestoneServers[random.Next(services.LodestoneServers.Length)];
var url = $"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{services.DiscordLodestoneMapping[userid]}"; var url = $"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{services.DiscordLodestoneMapping[userid]}";
var response = await req.GetAsync(url).ConfigureAwait(false); using var response = await req.GetAsync(url).ConfigureAwait(false);
_logger.LogInformation("Verifying {userid} with URL {url}", userid, url); _logger.LogInformation("Verifying {userid} with URL {url}", userid, url);
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{ {
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (content.Contains(authString)) if (content.Contains(authString))
@@ -229,7 +230,8 @@ public partial class MareWizardModule
else else
{ {
services.DiscordVerifiedUsers[userid] = false; services.DiscordVerifiedUsers[userid] = false;
_logger.LogInformation("Could not verify {userid} from lodestone {lodestone}, did not find authString: {authString}", userid, services.DiscordLodestoneMapping[userid], authString); _logger.LogInformation("Could not verify {userid} from lodestone {lodestone}, did not find authString: {authString}, status code was: {code}",
userid, services.DiscordLodestoneMapping[userid], authString, response.StatusCode);
} }
} }
else else

View File

@@ -4,6 +4,7 @@ using MareSynchronosShared.Data;
using MareSynchronosShared.Utils; using MareSynchronosShared.Utils;
using MareSynchronosShared.Models; using MareSynchronosShared.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Security.Policy;
namespace MareSynchronosServices.Discord; namespace MareSynchronosServices.Discord;
@@ -202,26 +203,35 @@ public partial class MareWizardModule
private async Task HandleVerifyRelinkAsync(ulong userid, string authString, DiscordBotServices services) private async Task HandleVerifyRelinkAsync(ulong userid, string authString, DiscordBotServices services)
{ {
var req = new HttpClient(); using var req = new HttpClient();
services.DiscordVerifiedUsers.Remove(userid, out _); services.DiscordVerifiedUsers.Remove(userid, out _);
if (services.DiscordRelinkLodestoneMapping.ContainsKey(userid)) if (services.DiscordRelinkLodestoneMapping.ContainsKey(userid))
{ {
var randomServer = services.LodestoneServers[random.Next(services.LodestoneServers.Length)]; var randomServer = services.LodestoneServers[random.Next(services.LodestoneServers.Length)];
var response = await req.GetAsync($"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{services.DiscordRelinkLodestoneMapping[userid]}").ConfigureAwait(false); var url = $"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{services.DiscordLodestoneMapping[userid]}";
if (response.IsSuccessStatusCode) _logger.LogInformation("Verifying {userid} with URL {url}", userid, url);
using var response = await req.GetAsync(url).ConfigureAwait(false);
if (response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{ {
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (content.Contains(authString)) if (content.Contains(authString))
{ {
services.DiscordVerifiedUsers[userid] = true; services.DiscordVerifiedUsers[userid] = true;
_logger.LogInformation("Verified {userid} from lodestone {lodestone}", userid, services.DiscordLodestoneMapping[userid]);
services.DiscordRelinkLodestoneMapping.TryRemove(userid, out _); services.DiscordRelinkLodestoneMapping.TryRemove(userid, out _);
} }
else else
{ {
services.DiscordVerifiedUsers[userid] = false; services.DiscordVerifiedUsers[userid] = false;
_logger.LogInformation("Could not verify {userid} from lodestone {lodestone}, did not find authString: {authString}, status code was: {code}",
userid, services.DiscordLodestoneMapping[userid], authString, response.StatusCode);
} }
} }
else
{
_logger.LogWarning("Could not verify {userid}, HttpStatusCode: {code}", userid, response.StatusCode);
}
} }
} }