reworked sign up workflow

This commit is contained in:
Sarah Faey 2022-12-03 12:27:51 +01:00
parent 2a9cd89783
commit 19595e52bf
4 changed files with 51 additions and 20 deletions

View file

@ -42,7 +42,7 @@ namespace DiscordBot.CommandHandlers
userId = component.User.Id userId = component.User.Id
}; };
await _httpService.SignOff(signOff); await _httpService.SignOff(signOff);
await _handlerFunctions.Respond(component); await component.RespondAsync("Signed Off", ephemeral: true);
break; break;
} }
} }

View file

@ -80,13 +80,14 @@ namespace DiscordBot.CommandHandlers
{ {
List<ApiRole> roles = new List<ApiRole>(); List<ApiRole> roles = new List<ApiRole>();
roles = await _httpService.GetRoles(raidId, userIdToSignUp); roles = await _httpService.GetRoles(raidId, userIdToSignUp);
ApiRaid raid = await _httpService.GetRaid(raidId);
if(roles.Count > 1) 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) 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 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<ApiGuildWars2Account> accounts = await _httpService.GetSignUpAccounts(userIdToSignUp, raidId); List<ApiGuildWars2Account> accounts = await _httpService.GetSignUpAccounts(userIdToSignUp, raidId);
@ -103,15 +104,15 @@ namespace DiscordBot.CommandHandlers
{ {
ApiGuildWars2Account account = accounts.First(); ApiGuildWars2Account account = accounts.First();
await SignUp(buttonType, raidId, roleId, userIdToSignUp, account.GuildWars2AccountId, signedUpByUserId); 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) 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 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);
} }
} }
@ -143,16 +144,20 @@ namespace DiscordBot.CommandHandlers
} }
} }
//to avoid error messages because of no response... public async Task UpdateOrRespond(SocketInteraction component, string messageText, MessageComponent messageComponent, bool editMessage)
public async Task Respond(SocketInteraction component)
{ {
try if(editMessage && component is SocketMessageComponent)
{ {
await component.RespondAsync(); SocketMessageComponent socketComponent = component as SocketMessageComponent;
await socketComponent.UpdateAsync(x =>
{
x.Content = messageText;
x.Components = messageComponent;
});
} }
catch(Discord.Net.HttpException e) else
{ {
await component.RespondAsync(messageText, components: messageComponent , ephemeral: true);
} }
} }
} }

View file

@ -37,7 +37,11 @@ namespace DiscordBot.CommandHandlers
AccountSelectionMessage.Parameters accountParameters = AccountSelectionMessage.ParseId(component.Data.CustomId); AccountSelectionMessage.Parameters accountParameters = AccountSelectionMessage.ParseId(component.Data.CustomId);
int accountId = int.Parse(component.Data.Values.First()); int accountId = int.Parse(component.Data.Values.First());
await _handlerFunctions.SignUp(accountParameters.ButtonType, accountParameters.RaidId, accountParameters.RoleId, accountParameters.UserIdToSignUp, accountId, accountParameters.SignedUpByUserId); 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; break;
} }
} }

View file

@ -8,7 +8,7 @@ namespace DiscordBot.Messages
public static MessageComponent buildMessage(List<ApiRole> roles, int raidId, string buttonType, bool allRoles, ulong userIdToSignUp, ulong signedUpByUserId) public static MessageComponent buildMessage(List<ApiRole> roles, int raidId, string buttonType, bool allRoles, ulong userIdToSignUp, ulong signedUpByUserId)
{ {
var signUpSelect = new SelectMenuBuilder() 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}") .WithCustomId($"{Constants.ComponentIds.ROLE_SELECT_DROP_DOWN}-{raidId}-{buttonType}-{userIdToSignUp}-{signedUpByUserId}")
.WithMinValues(1) .WithMinValues(1)
.WithMaxValues(1); .WithMaxValues(1);
@ -25,6 +25,28 @@ namespace DiscordBot.Messages
return builder.Build(); 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) public static Parameters ParseId(string customId)
{ {
Parameters parameters = new Parameters(); Parameters parameters = new Parameters();