From 19595e52bf64e94660ae198604f2609866697d74 Mon Sep 17 00:00:00 2001 From: Sarah Faey Date: Sat, 3 Dec 2022 12:27:51 +0100 Subject: [PATCH] reworked sign up workflow --- DiscordBot/CommandHandlers/ButtonHandler.cs | 2 +- .../CommandHandlers/HandlerFunctions.cs | 39 +++++++++++-------- .../CommandHandlers/SelectMenuHandler.cs | 6 ++- DiscordBot/Messages/RoleSelectionMessage.cs | 24 +++++++++++- 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/DiscordBot/CommandHandlers/ButtonHandler.cs b/DiscordBot/CommandHandlers/ButtonHandler.cs index 38e4931..206cf01 100644 --- a/DiscordBot/CommandHandlers/ButtonHandler.cs +++ b/DiscordBot/CommandHandlers/ButtonHandler.cs @@ -42,7 +42,7 @@ namespace DiscordBot.CommandHandlers userId = component.User.Id }; await _httpService.SignOff(signOff); - await _handlerFunctions.Respond(component); + await component.RespondAsync("Signed Off", ephemeral: true); break; } } diff --git a/DiscordBot/CommandHandlers/HandlerFunctions.cs b/DiscordBot/CommandHandlers/HandlerFunctions.cs index 974294c..9a54833 100644 --- a/DiscordBot/CommandHandlers/HandlerFunctions.cs +++ b/DiscordBot/CommandHandlers/HandlerFunctions.cs @@ -79,14 +79,15 @@ namespace DiscordBot.CommandHandlers if(await IsRaidSignUpAllowed(component, raidId, buttonType)) { List roles = new List(); - roles = await _httpService.GetRoles(raidId, userIdToSignUp); + roles = await _httpService.GetRoles(raidId, userIdToSignUp); + ApiRaid raid = await _httpService.GetRaid(raidId); if(roles.Count > 1) { - await component.RespondAsync("Please choose a role.", components: RoleSelectionMessage.buildMessage(roles, raidId, buttonType, allRoles, userIdToSignUp, signedUpByUserId) , ephemeral: true); + await component.RespondAsync($"{raid.Title}: Please choose a role.", components: RoleSelectionMessage.buildMessage(roles, raidId, buttonType, allRoles, userIdToSignUp, signedUpByUserId) , ephemeral: true); } else if(roles.Count == 1) { - await SelectAccount(buttonType, raidId, component, roles.First().roleId, userIdToSignUp, signedUpByUserId); + await SelectAccount(buttonType, raidId, component, roles.First().roleId, userIdToSignUp, signedUpByUserId, false); } else { @@ -95,7 +96,7 @@ namespace DiscordBot.CommandHandlers } } - public async Task SelectAccount(string buttonType, int raidId, SocketInteraction component, int roleId, ulong userIdToSignUp, ulong signedUpByUserId) + public async Task SelectAccount(string buttonType, int raidId, SocketInteraction component, int roleId, ulong userIdToSignUp, ulong signedUpByUserId, bool roleMessageExists = true) { List accounts = await _httpService.GetSignUpAccounts(userIdToSignUp, raidId); @@ -103,15 +104,15 @@ namespace DiscordBot.CommandHandlers { ApiGuildWars2Account account = accounts.First(); await SignUp(buttonType, raidId, roleId, userIdToSignUp, account.GuildWars2AccountId, signedUpByUserId); - await component.RespondAsync("successfully signed up", ephemeral: true); + await UpdateOrRespond(component, "successfully signed up", null, roleMessageExists); } else if(accounts.Count > 1) { - await component.RespondAsync("Please choose an account.", components: AccountSelectionMessage.buildMessage(accounts, raidId, buttonType, roleId, userIdToSignUp, signedUpByUserId) , ephemeral: true); + await UpdateOrRespond(component, "Please choose an account.", AccountSelectionMessage.buildMessage(accounts, raidId, buttonType, roleId, userIdToSignUp, signedUpByUserId), roleMessageExists); } else { - await component.RespondAsync("no suitable Guild Wars 2 account found.", ephemeral: true); + await UpdateOrRespond(component, "no suitable Guild Wars 2 account found.", null, roleMessageExists); } } @@ -142,17 +143,21 @@ namespace DiscordBot.CommandHandlers break; } } - - //to avoid error messages because of no response... - public async Task Respond(SocketInteraction component) - { - try - { - await component.RespondAsync(); - } - catch(Discord.Net.HttpException e) - { + public async Task UpdateOrRespond(SocketInteraction component, string messageText, MessageComponent messageComponent, bool editMessage) + { + if(editMessage && component is SocketMessageComponent) + { + SocketMessageComponent socketComponent = component as SocketMessageComponent; + await socketComponent.UpdateAsync(x => + { + x.Content = messageText; + x.Components = messageComponent; + }); + } + else + { + await component.RespondAsync(messageText, components: messageComponent , ephemeral: true); } } } diff --git a/DiscordBot/CommandHandlers/SelectMenuHandler.cs b/DiscordBot/CommandHandlers/SelectMenuHandler.cs index ab7da5d..b1eba7e 100644 --- a/DiscordBot/CommandHandlers/SelectMenuHandler.cs +++ b/DiscordBot/CommandHandlers/SelectMenuHandler.cs @@ -37,7 +37,11 @@ namespace DiscordBot.CommandHandlers AccountSelectionMessage.Parameters accountParameters = AccountSelectionMessage.ParseId(component.Data.CustomId); int accountId = int.Parse(component.Data.Values.First()); await _handlerFunctions.SignUp(accountParameters.ButtonType, accountParameters.RaidId, accountParameters.RoleId, accountParameters.UserIdToSignUp, accountId, accountParameters.SignedUpByUserId); - await component.RespondAsync("successfully signed up", ephemeral: true); + await component.UpdateAsync(x => + { + x.Content = "successfully signed up"; + x.Components = null; + }); break; } } diff --git a/DiscordBot/Messages/RoleSelectionMessage.cs b/DiscordBot/Messages/RoleSelectionMessage.cs index 0f722c5..3e84314 100644 --- a/DiscordBot/Messages/RoleSelectionMessage.cs +++ b/DiscordBot/Messages/RoleSelectionMessage.cs @@ -8,7 +8,7 @@ namespace DiscordBot.Messages public static MessageComponent buildMessage(List roles, int raidId, string buttonType, bool allRoles, ulong userIdToSignUp, ulong signedUpByUserId) { var signUpSelect = new SelectMenuBuilder() - .WithPlaceholder($"Select a role") + .WithPlaceholder($"Select a role - {GetButtonName(buttonType)}") .WithCustomId($"{Constants.ComponentIds.ROLE_SELECT_DROP_DOWN}-{raidId}-{buttonType}-{userIdToSignUp}-{signedUpByUserId}") .WithMinValues(1) .WithMaxValues(1); @@ -25,6 +25,28 @@ namespace DiscordBot.Messages return builder.Build(); } + private static string GetButtonName(string buttonType) + { + switch(buttonType) + { + case Constants.ComponentIds.SIGN_UP_BUTTON: + return "Sign Up"; + break; + case Constants.ComponentIds.MAYBE_BUTTON: + return "Maybe"; + break; + case Constants.ComponentIds.BACKUP_BUTTON: + return "Backup"; + break; + case Constants.ComponentIds.FLEX_BUTTON: + return "Flex"; + break; + default: + return string.Empty; + break; + } + } + public static Parameters ParseId(string customId) { Parameters parameters = new Parameters();