diff --git a/DiscordBot/CommandHandlers/ButtonHandler.cs b/DiscordBot/CommandHandlers/ButtonHandler.cs index f063155..38e4931 100644 --- a/DiscordBot/CommandHandlers/ButtonHandler.cs +++ b/DiscordBot/CommandHandlers/ButtonHandler.cs @@ -22,34 +22,23 @@ namespace DiscordBot.CommandHandlers public async Task Handler(SocketMessageComponent component) { string[] ids = component.Data.CustomId.Split('-'); - List roles = new List(); - int parsedRaidId = 0; - if(ids.Length > 1) - { - int.TryParse(ids[1],out parsedRaidId); - } switch(ids[0]) { case Constants.ComponentIds.SIGN_UP_BUTTON: - if(await _handlerFunctions.IsRaidSignUpAllowed(component, parsedRaidId, ids[0])) - { - roles = await _httpService.GetRoles(parsedRaidId, component.User.Id); - await component.RespondAsync("Please choose a role.", components: RoleSelectionMessage.buildMessage(roles, parsedRaidId, ids[0], false) , ephemeral: true); - } - break; + RaidMessage.ButtonParameters signUpParameters = RaidMessage.ParseButtonId(component.Data.CustomId); + await _handlerFunctions.SelectRole(component, signUpParameters.RaidId, signUpParameters.ButtonType, false, component.User.Id, 0); + break; case Constants.ComponentIds.MAYBE_BUTTON: case Constants.ComponentIds.BACKUP_BUTTON: case Constants.ComponentIds.FLEX_BUTTON: - if(await _handlerFunctions.IsRaidSignUpAllowed(component, parsedRaidId, ids[0])) - { - roles = await _httpService.GetRoles(parsedRaidId, component.User.Id); - await component.RespondAsync("Please choose a role.", components: RoleSelectionMessage.buildMessage(roles, parsedRaidId, ids[0], true) , ephemeral: true); - } - break; + RaidMessage.ButtonParameters maybeParameters = RaidMessage.ParseButtonId(component.Data.CustomId); + await _handlerFunctions.SelectRole(component, maybeParameters.RaidId, maybeParameters.ButtonType, true, component.User.Id, 0); + break; case Constants.ComponentIds.SIGN_OFF_BUTTON: + RaidMessage.ButtonParameters signOffParameters = RaidMessage.ParseButtonId(component.Data.CustomId); ApiSignUp signOff = new ApiSignUp() { - raidId = parsedRaidId, + raidId = signOffParameters.RaidId, userId = component.User.Id }; await _httpService.SignOff(signOff); @@ -57,6 +46,5 @@ namespace DiscordBot.CommandHandlers break; } } - } } \ No newline at end of file diff --git a/DiscordBot/CommandHandlers/HandlerFunctions.cs b/DiscordBot/CommandHandlers/HandlerFunctions.cs index 419dd10..378a191 100644 --- a/DiscordBot/CommandHandlers/HandlerFunctions.cs +++ b/DiscordBot/CommandHandlers/HandlerFunctions.cs @@ -69,6 +69,75 @@ namespace DiscordBot.CommandHandlers } } } + + public async Task SelectRole(SocketInteraction component, int raidId, string buttonType, bool allRoles, ulong userIdToSignUp, ulong signedUpByUserId) + { + if(await IsRaidSignUpAllowed(component, raidId, buttonType)) + { + List roles = new List(); + roles = await _httpService.GetRoles(raidId, userIdToSignUp); + if(roles.Count > 1) + { + await component.RespondAsync("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); + } + else + { + await component.RespondAsync("no role found.", ephemeral: true); + } + } + } + + public async Task SelectAccount(string buttonType, int raidId, SocketInteraction component, int roleId, ulong userIdToSignUp, ulong signedUpByUserId) + { + + List accounts = await _httpService.GetSignUpAccounts(userIdToSignUp, raidId); + if(accounts.Count == 1) + { + ApiGuildWars2Account account = accounts.First(); + await SignUp(buttonType, raidId, roleId, userIdToSignUp, account.GuildWars2AccountId, signedUpByUserId); + await component.RespondAsync("successfully signed up", ephemeral: true); + } + else if(accounts.Count > 1) + { + await component.RespondAsync("Please choose an account.", components: AccountSelectionMessage.buildMessage(accounts, raidId, buttonType, roleId, userIdToSignUp, signedUpByUserId) , ephemeral: true); + } + else + { + await component.RespondAsync("no suitable Guild Wars 2 account found.", ephemeral: true); + } + } + + public async Task SignUp(string buttonType, int raidId, int roleId, ulong userIdToSignUp, int gw2AccountId, ulong signedUpByUserId) + { + ApiSignUp signUp = new ApiSignUp() + { + raidId = raidId, + roleId = roleId, + gw2AccountId = gw2AccountId, + userId = userIdToSignUp, + signedUpByUserId = signedUpByUserId + }; + + switch(buttonType) + { + case Constants.ComponentIds.SIGN_UP_BUTTON: + await _httpService.SignUp(signUp); + break; + case Constants.ComponentIds.MAYBE_BUTTON: + await _httpService.SignUpMaybe(signUp); + break; + case Constants.ComponentIds.BACKUP_BUTTON: + await _httpService.SignUpBackup(signUp); + break; + case Constants.ComponentIds.FLEX_BUTTON: + await _httpService.SignUpFlex(signUp); + break; + } + } //to avoid error messages because of no response... public async Task Respond(SocketInteraction component) diff --git a/DiscordBot/CommandHandlers/ModalHandler.cs b/DiscordBot/CommandHandlers/ModalHandler.cs index 79a5e30..88b8788 100644 --- a/DiscordBot/CommandHandlers/ModalHandler.cs +++ b/DiscordBot/CommandHandlers/ModalHandler.cs @@ -42,15 +42,7 @@ namespace DiscordBot.CommandHandlers //sign up CreateAccountModal.Parameters createAccountParameters = CreateAccountModal.ParseId(modal.Data.CustomId); - if(await _handlerFunctions.IsRaidSignUpAllowed(modal, createAccountParameters.RaidId, createAccountParameters.ButtonId)) - { - List roles = await _httpService.GetRoles(createAccountParameters.RaidId, modal.User.Id); - await modal.RespondAsync("Please choose a role.", - components: RoleSelectionMessage.buildMessage(roles, createAccountParameters.RaidId, createAccountParameters.ButtonId, false), - ephemeral: true); - return; - } - await _handlerFunctions.Respond(modal); + await _handlerFunctions.SelectRole(modal, createAccountParameters.RaidId, createAccountParameters.ButtonId, false, modal.User.Id, 0); break; case Constants.ComponentIds.SIGN_UP_EXTERNAL_MODAL: string userName = components.First(x => x.CustomId == Constants.ComponentIds.NAME_TEXT_BOX).Value; diff --git a/DiscordBot/CommandHandlers/SelectMenuHandler.cs b/DiscordBot/CommandHandlers/SelectMenuHandler.cs index caffe81..ab7da5d 100644 --- a/DiscordBot/CommandHandlers/SelectMenuHandler.cs +++ b/DiscordBot/CommandHandlers/SelectMenuHandler.cs @@ -24,77 +24,22 @@ namespace DiscordBot.CommandHandlers string[] ids = component.Data.CustomId.Split('-'); switch(ids[0]) { - case Constants.ComponentIds.SIGN_UP_DROP_DOWN: + case Constants.ComponentIds.ROLE_SELECT_DROP_DOWN: RoleSelectionMessage.Parameters roleParameters = RoleSelectionMessage.ParseId(component.Data.CustomId); - ulong userIdToSignUp = component.User.Id;; - ulong signedUpByUserId = 0; - if(roleParameters.UserIdToSignUp != 0) - { - userIdToSignUp = roleParameters.UserIdToSignUp; - signedUpByUserId = component.User.Id; - } - await ManageSignUp(roleParameters.ButtonType, roleParameters.RaidId, component, userIdToSignUp, signedUpByUserId); + int parsedRoleId = int.Parse(component.Data.Values.First()); + await _handlerFunctions.SelectAccount(roleParameters.ButtonType, roleParameters.RaidId, component, parsedRoleId, roleParameters.UserIdToSignUp, roleParameters.SignedUpByUserId); break; - case Constants.ComponentIds.SIGN_UP_EXTERNAL_DROP_DOWN: + case Constants.ComponentIds.ROLE_SELECT_EXTERNAL_DROP_DOWN: ExternalRoleSelectionMessage.Parameters externalRoleParameters = ExternalRoleSelectionMessage.ParseId(component.Data.CustomId); await component.RespondWithModalAsync(ExternalUserNameModal.buildMessage(externalRoleParameters.RaidId, int.Parse(component.Data.Values.First()))); break; case Constants.ComponentIds.ACCOUNT_SELECT_DROP_DOWN: AccountSelectionMessage.Parameters accountParameters = AccountSelectionMessage.ParseId(component.Data.CustomId); int accountId = int.Parse(component.Data.Values.First()); - await 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); break; } } - - private async Task ManageSignUp(string buttonType, int raidId, SocketMessageComponent component, ulong userIdToSignUp, ulong signedUpByUserId = 0) - { - if(! int.TryParse(component.Data.Values.First(), out int parsedRoleId)) return; - - List accounts = await _httpService.GetSignUpAccounts(userIdToSignUp, raidId); - if(accounts.Count == 1) - { - ApiGuildWars2Account account = accounts.First(); - await SignUp(buttonType, raidId, parsedRoleId, userIdToSignUp, account.GuildWars2AccountId, signedUpByUserId); - await component.RespondAsync("successfully signed up", ephemeral: true); - } - else if(accounts.Count > 1) - { - await component.RespondAsync("Please choose an account.", components: AccountSelectionMessage.buildMessage(accounts, raidId, buttonType, parsedRoleId, userIdToSignUp, signedUpByUserId) , ephemeral: true); - } - else - { - await component.RespondAsync("no suitable Guild Wars 2 account found.", ephemeral: true); - } - } - - private async Task SignUp(string buttonType, int raidId, int roleId, ulong userIdToSignUp, int gw2AccountId, ulong signedUpByUserId = 0) - { - ApiSignUp signUp = new ApiSignUp() - { - raidId = raidId, - roleId = roleId, - gw2AccountId = gw2AccountId, - userId = userIdToSignUp, - signedUpByUserId = signedUpByUserId - }; - - switch(buttonType) - { - case Constants.ComponentIds.SIGN_UP_BUTTON: - await _httpService.SignUp(signUp); - break; - case Constants.ComponentIds.MAYBE_BUTTON: - await _httpService.SignUpMaybe(signUp); - break; - case Constants.ComponentIds.BACKUP_BUTTON: - await _httpService.SignUpBackup(signUp); - break; - case Constants.ComponentIds.FLEX_BUTTON: - await _httpService.SignUpFlex(signUp); - break; - } - } } } \ No newline at end of file diff --git a/DiscordBot/CommandHandlers/SlashCommandHandler.cs b/DiscordBot/CommandHandlers/SlashCommandHandler.cs index 1e46578..791a120 100644 --- a/DiscordBot/CommandHandlers/SlashCommandHandler.cs +++ b/DiscordBot/CommandHandlers/SlashCommandHandler.cs @@ -22,8 +22,6 @@ namespace DiscordBot.CommandHandlers _handlerFunctions = new HandlerFunctions(_httpService); } - - public async Task Handler(SocketSlashCommand command) { Tuple commandExecutionAllowed = await _httpService.IsSlashCommandAllowed(command.User.Id, command.Data.Name); @@ -99,24 +97,20 @@ namespace DiscordBot.CommandHandlers } private async Task SignUpUser(SocketSlashCommand command, IUser user, int raidId) - { - if(await _handlerFunctions.IsRaidSignUpAllowed(command, raidId, Constants.ComponentIds.SIGN_UP_BUTTON)) - { - List roles = await _httpService.GetRoles(raidId, user.Id); - if(await _httpService.DoesUserExist(user.Id)) - { - Tuple signUpAllowed = await _httpService.IsSignUpAllowed(raidId, user.Id, true); - if(!signUpAllowed.Item1) - { - await command.RespondAsync(signUpAllowed.Item2, ephemeral: true); - return; - } - await command.RespondAsync("Please choose a role.", components: RoleSelectionMessage.buildMessage(roles, raidId, Constants.ComponentIds.SIGN_UP_BUTTON, false, user.Id) , ephemeral: true); - } - else + { + if(await _httpService.DoesUserExist(user.Id)) + { + Tuple signUpAllowed = await _httpService.IsSignUpAllowed(raidId, user.Id, true); + if(!signUpAllowed.Item1) { - await SignUpExternalUser(command, raidId, roles); + await command.RespondAsync(signUpAllowed.Item2, ephemeral: true); + return; } + await _handlerFunctions.SelectRole(command, raidId, Constants.ComponentIds.SIGN_UP_BUTTON, false, user.Id, command.User.Id); + } + else + { + await SignUpExternalUser(command, raidId); } } @@ -125,21 +119,16 @@ namespace DiscordBot.CommandHandlers if(await _handlerFunctions.IsRaidSignUpAllowed(command, raidId, Constants.ComponentIds.SIGN_UP_BUTTON)) { List roles = await _httpService.GetRoles(raidId, uint.MaxValue); - await SignUpExternalUser(command, raidId, roles); + Tuple signUpAllowed = await _httpService.IsExternalSignUpAllowed(raidId); + if(!signUpAllowed.Item1) + { + await command.RespondAsync(signUpAllowed.Item2, ephemeral: true); + return; + } + await command.RespondAsync("Please choose a role.", components: ExternalRoleSelectionMessage.buildMessage(roles, raidId) , ephemeral: true); } } - private async Task SignUpExternalUser(SocketSlashCommand command, int raidId, List roles) - { - Tuple signUpAllowed = await _httpService.IsExternalSignUpAllowed(raidId); - if(!signUpAllowed.Item1) - { - await command.RespondAsync(signUpAllowed.Item2, ephemeral: true); - return; - } - await command.RespondAsync("Please choose a role.", components: ExternalRoleSelectionMessage.buildMessage(roles, raidId) , ephemeral: true); - } - private async Task SendMessages(string message, int raidId) { ApiRaid raid = await _httpService.GetRaid(raidId); diff --git a/DiscordBot/Constants.cs b/DiscordBot/Constants.cs index 040745c..3e0237f 100644 --- a/DiscordBot/Constants.cs +++ b/DiscordBot/Constants.cs @@ -11,8 +11,8 @@ public const string FLEX_BUTTON = "flexButton"; public const string SIGN_OFF_BUTTON = "signOffButton"; - public const string SIGN_UP_DROP_DOWN = "signUpDropDown"; - public const string SIGN_UP_EXTERNAL_DROP_DOWN = "signUpExternalDropDown"; + public const string ROLE_SELECT_DROP_DOWN = "roleSelectDropDown"; + public const string ROLE_SELECT_EXTERNAL_DROP_DOWN = "roleSelectExternalDropDown"; public const string ACCOUNT_SELECT_DROP_DOWN = "accountSelectDropDown"; public const string NAME_TEXT_BOX = "nameTextbox"; diff --git a/DiscordBot/Messages/ExternalRoleSelectionMessage.cs b/DiscordBot/Messages/ExternalRoleSelectionMessage.cs index 63f0607..7936fee 100644 --- a/DiscordBot/Messages/ExternalRoleSelectionMessage.cs +++ b/DiscordBot/Messages/ExternalRoleSelectionMessage.cs @@ -9,7 +9,7 @@ namespace DiscordBot.Messages { var signUpSelect = new SelectMenuBuilder() .WithPlaceholder("Select an option") - .WithCustomId($"{Constants.ComponentIds.SIGN_UP_EXTERNAL_DROP_DOWN}-{raidId}") + .WithCustomId($"{Constants.ComponentIds.ROLE_SELECT_EXTERNAL_DROP_DOWN}-{raidId}") .WithMinValues(1) .WithMaxValues(1); diff --git a/DiscordBot/Messages/RaidMessage.cs b/DiscordBot/Messages/RaidMessage.cs index 39bd072..06158cd 100644 --- a/DiscordBot/Messages/RaidMessage.cs +++ b/DiscordBot/Messages/RaidMessage.cs @@ -69,7 +69,7 @@ namespace DiscordBot.Messages return raid; } - public Embed CreateRaidMessage(ApiRaid raid) + private Embed CreateRaidMessage(ApiRaid raid) { var embed = new EmbedBuilder() { @@ -124,5 +124,27 @@ namespace DiscordBot.Messages } return rolesString; } + + public static ButtonParameters ParseButtonId(string customId) + { + ButtonParameters parameters = new ButtonParameters(); + + string[] ids = customId.Split('-'); + if(ids.Length > 0) + { + parameters.ButtonType = ids[0]; + } + if(ids.Length > 1) + { + int.TryParse(ids[1],out parameters.RaidId); + } + return parameters; + } + + public class ButtonParameters + { + public int RaidId; + public string ButtonType = string.Empty; + } } } diff --git a/DiscordBot/Messages/RoleSelectionMessage.cs b/DiscordBot/Messages/RoleSelectionMessage.cs index 4a95d14..fd6c896 100644 --- a/DiscordBot/Messages/RoleSelectionMessage.cs +++ b/DiscordBot/Messages/RoleSelectionMessage.cs @@ -5,11 +5,11 @@ namespace DiscordBot.Messages { public class RoleSelectionMessage { - public static MessageComponent buildMessage(List roles, int raidId, string buttonType, bool allRoles, ulong userIdToSignUp = 0) + public static MessageComponent buildMessage(List roles, int raidId, string buttonType, bool allRoles, ulong userIdToSignUp, ulong signedUpByUserId) { var signUpSelect = new SelectMenuBuilder() .WithPlaceholder("Select an option") - .WithCustomId($"{Constants.ComponentIds.SIGN_UP_DROP_DOWN}-{raidId}-{buttonType}-{userIdToSignUp}") + .WithCustomId($"{Constants.ComponentIds.ROLE_SELECT_DROP_DOWN}-{raidId}-{buttonType}-{userIdToSignUp}-{signedUpByUserId}") .WithMinValues(1) .WithMaxValues(1); @@ -42,6 +42,10 @@ namespace DiscordBot.Messages { ulong.TryParse(ids[3],out parameters.UserIdToSignUp); } + if(ids.Length > 4) + { + ulong.TryParse(ids[4],out parameters.SignedUpByUserId); + } return parameters; } @@ -49,7 +53,8 @@ namespace DiscordBot.Messages { public int RaidId; public string ButtonType = string.Empty; - public ulong UserIdToSignUp; + public ulong UserIdToSignUp; + public ulong SignedUpByUserId; } } } \ No newline at end of file