From 5094bf736d69ef08b5922214b81f7464d2a5659b Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Thu, 25 Aug 2022 02:57:56 +0200 Subject: [PATCH] add vanity uid bot command --- .../Discord/DiscordBot.cs | 52 +++++++++++++++++++ .../MareSynchronosServices/appsettings.json | 3 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs index eef290b..65fe504 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs @@ -99,6 +99,14 @@ public class DiscordBot : IHostedService verificationQueue.Enqueue(arg); } } + else if (arg.Data.Name == "setvanityuid") + { + EmbedBuilder eb = new(); + var newUid = (string)arg.Data.Options.First(f => f.Name == "vanity_uid").Value; + eb = await HandleVanityUid(eb, arg.User.Id, newUid); + + await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true).ConfigureAwait(false); + } else { await arg.RespondAsync("idk what you did to get here to start, just follow the instructions as provided.", ephemeral: true).ConfigureAwait(false); @@ -110,6 +118,44 @@ public class DiscordBot : IHostedService } } + private async Task HandleVanityUid(EmbedBuilder eb, ulong id, string newUid) + { + using var scope = services.CreateScope(); + using var db = scope.ServiceProvider.GetService(); + var lodestoneUser = await db.LodeStoneAuth.Include("User").SingleOrDefaultAsync(u => u.DiscordId == id).ConfigureAwait(false); + if (lodestoneUser == null) + { + eb.WithTitle("Failed to set Vanity UID"); + eb.WithDescription("You do not have a registered account on this server."); + return eb; + } + + var uidExists = await db.Users.AnyAsync(u => u.UID == newUid || u.Alias == newUid).ConfigureAwait(false); + if (uidExists) + { + eb.WithTitle("Failed to set Vanity UID"); + eb.WithDescription("This UID is already taken."); + return eb; + } + + Regex rgx = new("[A-Z0-9]{10}", RegexOptions.ECMAScript); + if (!rgx.Match(newUid).Success || newUid.Length != 10) + { + eb.WithTitle("Failed to set Vanity UID"); + eb.WithDescription("The Vanity UID must be 10 characters long and only contain uppercase letters A-Z and numbers 0-9."); + return eb; + } + + var user = lodestoneUser.User; + user.Alias = newUid; + db.Update(user); + await db.SaveChangesAsync(); + + eb.WithTitle("Vanity UID set"); + eb.WithDescription("Your Vanity UID was set to **" + newUid + "**." + Environment.NewLine + "For those changes to apply you will have to reconnect to Mare."); + return eb; + } + private async Task DeletePreviousUserAccount(ulong id) { using var scope = services.CreateScope(); @@ -347,10 +393,16 @@ public class DiscordBot : IHostedService verify.WithName("verify"); verify.WithDescription("Finishes the registration process for the Mare Synchronos server of this Discord"); + var vanityuid = new SlashCommandBuilder(); + vanityuid.WithName("setvanityuid"); + vanityuid.WithDescription("Sets your Vanity UID."); + vanityuid.AddOption("vanity_uid", ApplicationCommandOptionType.String, "Desired Vanity UID", isRequired: true); + try { await discordClient.CreateGlobalApplicationCommandAsync(register.Build()).ConfigureAwait(false); await discordClient.CreateGlobalApplicationCommandAsync(verify.Build()).ConfigureAwait(false); + await discordClient.CreateGlobalApplicationCommandAsync(vanityuid.Build()).ConfigureAwait(false); } catch (Exception ex) { diff --git a/MareSynchronosServer/MareSynchronosServices/appsettings.json b/MareSynchronosServer/MareSynchronosServices/appsettings.json index 88eee64..cfaeb25 100644 --- a/MareSynchronosServer/MareSynchronosServices/appsettings.json +++ b/MareSynchronosServer/MareSynchronosServices/appsettings.json @@ -22,7 +22,8 @@ "PurgeUnusedAccounts": true, "PurgeUnusedAccountsPeriodInDays": 14, "FailedAuthForTempBan": 5, - "TempBanDurationInMinutes": 30 + "TempBanDurationInMinutes": 30, + "VanitySupportedRoles": [] }, "AllowedHosts": "*" }