added account creation to the Discord bot

This commit is contained in:
Sarah Faey 2022-11-10 21:47:44 +01:00
parent 69337e69ae
commit 56cb43a479
18 changed files with 198 additions and 152 deletions

View file

@ -27,6 +27,7 @@ namespace DiscordBot
_client.SlashCommandExecuted += SlashCommandHandler;
_client.ButtonExecuted += ButtonHandler;
_client.SelectMenuExecuted += SelectMenuHandler;
_client.ModalSubmitted += ModalHandler;
}
private async Task SlashCommandHandler(SocketSlashCommand command)
@ -72,14 +73,20 @@ namespace DiscordBot
switch(ids[0])
{
case Constants.ComponentIds.SIGN_UP_BUTTON:
roles = await _httpService.GetRoles(parsedRaidId, component.User.Id);
await component.RespondAsync("Please choose a role.", components: SignUpMessage.buildMessage(roles, parsedRaidId, ids[0], false) , ephemeral: true);
if(await IsRaidSignUpAllowed(component, parsedRaidId, ids[0]))
{
roles = await _httpService.GetRoles(parsedRaidId, component.User.Id);
await component.RespondAsync("Please choose a role.", components: SignUpMessage.buildMessage(roles, parsedRaidId, ids[0], false) , ephemeral: true);
}
break;
case Constants.ComponentIds.MAYBE_BUTTON:
case Constants.ComponentIds.BACKUP_BUTTON:
case Constants.ComponentIds.FLEX_BUTTON:
roles = await _httpService.GetRoles(parsedRaidId, component.User.Id);
await component.RespondAsync("Please choose a role.", components: SignUpMessage.buildMessage(roles, parsedRaidId, ids[0], true) , ephemeral: true);
if(await IsRaidSignUpAllowed(component, parsedRaidId, ids[0]))
{
roles = await _httpService.GetRoles(parsedRaidId, component.User.Id);
await component.RespondAsync("Please choose a role.", components: SignUpMessage.buildMessage(roles, parsedRaidId, ids[0], true) , ephemeral: true);
}
break;
case Constants.ComponentIds.SIGN_OFF_BUTTON:
ApiSignUp signOff = new ApiSignUp()
@ -93,6 +100,28 @@ namespace DiscordBot
}
}
public async Task<bool> IsRaidSignUpAllowed(SocketInteraction component, int raidId, string pressedButtonId)
{
if(! await _httpService.DoesUserExist(component.User.Id))
{
var mb = new ModalBuilder()
.WithTitle("Create Account")
.WithCustomId($"{Constants.ComponentIds.CREATE_ACCOUNT_MODAL}-{raidId}-{pressedButtonId}")
.AddTextInput("Name", Constants.ComponentIds.NAME_TEXT_BOX, placeholder: component.User.Username, required: true, value: component.User.Username)
.AddTextInput("Guild Wars 2 Account", Constants.ComponentIds.ACCOUNT_TEXT_BOX, placeholder: "Account.1234", required: true);
await component.RespondWithModalAsync(mb.Build());
return false;
}
Tuple<bool, string> signUpAllowed = await _httpService.IsSignUpAllowed(raidId, component.User.Id);
if(!signUpAllowed.Item1)
{
await component.RespondAsync(signUpAllowed.Item2, ephemeral: true);
return false;
}
return true;
}
public async Task SelectMenuHandler(SocketMessageComponent component)
{
string[] ids = component.Data.CustomId.Split('-');
@ -139,8 +168,39 @@ namespace DiscordBot
}
}
public async Task ModalHandler(SocketModal modal)
{
List<SocketMessageComponentData> components = modal.Data.Components.ToList();
string name = components.First(x => x.CustomId == Constants.ComponentIds.NAME_TEXT_BOX).Value;
string account = components.First(x => x.CustomId == Constants.ComponentIds.ACCOUNT_TEXT_BOX).Value;
//create Account
ApiRaid.Role.User user = new ApiRaid.Role.User()
{
UserName = name,
AccountName = account,
UserId = modal.User.Id
};
Tuple<bool, string> createAccountResult = await _httpService.CreateAccount(user);
if(!createAccountResult.Item1)
{
await modal.RespondAsync(createAccountResult.Item2, ephemeral: true);
return;
}
//sign up
string[] ids = modal.Data.CustomId.Split('-');
if(ids.Length > 2 && int.TryParse(ids[1], out int parsedRaidId) && await IsRaidSignUpAllowed(modal, parsedRaidId, ids[2]))
{
List<ApiRole> roles = await _httpService.GetRoles(parsedRaidId, modal.User.Id);
await modal.RespondAsync("Please choose a role.", components: SignUpMessage.buildMessage(roles, parsedRaidId, ids[2], false) , ephemeral: true);
return;
}
await Respond(modal);
}
//to avoid error messages because of no response...
private async Task Respond(SocketMessageComponent component)
private async Task Respond(SocketInteraction component)
{
try
{