From 29846603895b157cfd05e1d451d583c5a5669597 Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Tue, 2 Aug 2022 21:48:10 +0200 Subject: [PATCH] add /register forced --- .../Discord/DiscordBot.cs | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosServer/Discord/DiscordBot.cs b/MareSynchronosServer/MareSynchronosServer/Discord/DiscordBot.cs index 522f4ef..e24133e 100644 --- a/MareSynchronosServer/MareSynchronosServer/Discord/DiscordBot.cs +++ b/MareSynchronosServer/MareSynchronosServer/Discord/DiscordBot.cs @@ -30,7 +30,7 @@ namespace MareSynchronosServer.Discord DiscordSocketClient discordClient; ConcurrentDictionary DiscordLodestoneMapping = new(); private Timer _timer; - private readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "na" }; + private readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" }; public DiscordBot(IServiceProvider services, IConfiguration configuration, ILogger logger) { @@ -38,6 +38,8 @@ namespace MareSynchronosServer.Discord this.configuration = configuration; this.logger = logger; + authToken = configuration.GetValue("DiscordBotToken"); + discordClient = new(new DiscordSocketConfig() { DefaultRetryMode = RetryMode.AlwaysRetry @@ -50,6 +52,11 @@ namespace MareSynchronosServer.Discord { if (arg.Data.Name == "register") { + if (arg.Data.Options.FirstOrDefault(f => f.Name == "forced") != null) + { + await DeletePreviousUserAccount(arg.User.Id); + } + var modal = new ModalBuilder(); modal.WithTitle("Verify with Lodestone"); modal.WithCustomId("register_modal"); @@ -64,6 +71,20 @@ namespace MareSynchronosServer.Discord } } + private async Task DeletePreviousUserAccount(ulong id) + { + using var scope = services.CreateScope(); + using var db = scope.ServiceProvider.GetService(); + var discordAuthedUser = await db.LodeStoneAuth.Include(u => u.User).FirstOrDefaultAsync(u => u.DiscordId == id); + if (discordAuthedUser != null && discordAuthedUser.User != null) + { + logger.LogInformation("User will be purged on next round of deletions: " + discordAuthedUser.User); + discordAuthedUser.User.LastLoggedIn = new DateTime(1900, 0, 0); + db.Remove(discordAuthedUser); + await db.SaveChangesAsync(); + } + } + private async Task DiscordClient_ModalSubmitted(SocketModal arg) { if (arg.Data.CustomId == "register_modal") @@ -145,7 +166,7 @@ namespace MareSynchronosServer.Discord lodestoneAuth.LodestoneAuthString = null; embedBuilder.WithTitle("Registration successful"); - embedBuilder.WithDescription("This is your private secret key. Do not share this private secret key with anyone. If you lose it, it is irrevocably lost." + embedBuilder.WithDescription("This is your private secret key. Do not share this private secret key with anyone. **If you lose it, it is irrevocably lost.**" + Environment.NewLine + Environment.NewLine + $"**{computedHash}**" + Environment.NewLine + Environment.NewLine @@ -158,7 +179,9 @@ namespace MareSynchronosServer.Discord else { embedBuilder.WithTitle("Failed to verify your character"); - embedBuilder.WithDescription("Did not find requested authentication key on your profile. Start over with **/register**"); + embedBuilder.WithDescription("Did not find requested authentication key on your profile. Make sure you have saved *twice*, then do **/verify** again."); + DiscordLodestoneMapping.TryRemove(id, out _); + db.Remove(lodestoneAuth); } } else @@ -271,6 +294,7 @@ namespace MareSynchronosServer.Discord var cb = new SlashCommandBuilder(); cb.WithName("register"); cb.WithDescription("Starts the registration process for the Mare Synchronos server of this Discord"); + cb.AddOption("forced", ApplicationCommandOptionType.Boolean, "Will forcefully overwrite your current character on the service, if present", false, false); var cb2 = new SlashCommandBuilder(); cb2.WithName("verify"); @@ -296,20 +320,30 @@ namespace MareSynchronosServer.Discord public async Task StartAsync(CancellationToken cancellationToken) { - authToken = configuration.GetValue("DiscordBotToken"); if (!string.IsNullOrEmpty(authToken)) { + authToken = configuration.GetValue("DiscordBotToken"); + await discordClient.LoginAsync(TokenType.Bot, authToken); await discordClient.StartAsync(); discordClient.Ready += DiscordClient_Ready; discordClient.SlashCommandExecuted += DiscordClient_SlashCommandExecuted; discordClient.ModalSubmitted += DiscordClient_ModalSubmitted; + discordClient.Disconnected += DiscordClient_Disconnected; _timer = new Timer(UpdateStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(15)); } } + private async Task DiscordClient_Disconnected(Exception arg) + { + authToken = configuration.GetValue("DiscordBotToken"); + + await discordClient.LoginAsync(TokenType.Bot, authToken); + await discordClient.StartAsync(); + } + private void UpdateStatus(object state) { using var scope = services.CreateScope();