added account creation to the Discord bot
This commit is contained in:
parent
69337e69ae
commit
56cb43a479
18 changed files with 198 additions and 152 deletions
|
@ -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
|
||||
{
|
||||
|
|
|
@ -12,6 +12,10 @@
|
|||
public const string SIGN_OFF_BUTTON = "signOffButton";
|
||||
|
||||
public const string SIGN_UP_DROP_DOWN = "signUpDropDown";
|
||||
|
||||
public const string NAME_TEXT_BOX = "nameTextbox";
|
||||
public const string ACCOUNT_TEXT_BOX = "accountTextBox";
|
||||
public const string CREATE_ACCOUNT_MODAL = "createAccountModal";
|
||||
}
|
||||
|
||||
public class SlashCommands
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.7.2" />
|
||||
<PackageReference Include="Discord.Net" Version="3.8.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -88,8 +88,10 @@ namespace DiscordBot.Messages
|
|||
|
||||
private void AddMessageDetails(ApiRaid raid, ref EmbedBuilder embed)
|
||||
{
|
||||
embed.AddField("Date", $"{raid.StartTimeUTC.ToLocalTime().DateTime.ToLongDateString()}");
|
||||
embed.AddField("Time", $"from: {raid.StartTimeUTC.ToLocalTime().DateTime.ToShortTimeString()} to: {raid.EndTimeUTC.ToLocalTime().DateTime.ToShortTimeString()}");
|
||||
//embed.AddField("Date", $"{raid.StartTimeUTC.ToLocalTime().DateTime.ToLongDateString()}");
|
||||
//embed.AddField("Time", $"from: {raid.StartTimeUTC.ToLocalTime().DateTime.ToShortTimeString()} to: {raid.EndTimeUTC.ToLocalTime().DateTime.ToShortTimeString()}");
|
||||
embed.AddField("Start ", $"<t:{raid.StartTimeUTC.ToUnixTimeSeconds()}:F>");
|
||||
embed.AddField("End", $"<t:{raid.EndTimeUTC.ToUnixTimeSeconds()}:F>");
|
||||
embed.AddField("Organisator", raid.Organizer, true);
|
||||
embed.AddField("Guild", raid.Guild, true);
|
||||
embed.AddField("Voice chat", raid.VoiceChat, true);
|
||||
|
|
|
@ -27,42 +27,5 @@ namespace DiscordBot.Messages
|
|||
|
||||
return builder.Build();
|
||||
}
|
||||
/*
|
||||
public static MessageComponent buildMessage(List<ApiRole> roles, int raidId)
|
||||
{
|
||||
var signUpSelect = new SelectMenuBuilder()
|
||||
.WithPlaceholder("Select an option")
|
||||
.WithCustomId(Constants.ComponentIds.SIGN_UP_DROP_DOWN)
|
||||
.WithMinValues(1)
|
||||
.WithMaxValues(1);
|
||||
|
||||
foreach(ApiRole role in roles)
|
||||
{
|
||||
if(role.IsSignUpAllowed)
|
||||
signUpSelect.AddOption(role.Name, role.roleId.ToString(), role.Description);
|
||||
}
|
||||
|
||||
var flexSelect = new SelectMenuBuilder()
|
||||
.WithPlaceholder("Select an option")
|
||||
.WithCustomId(Constants.ComponentIds.FLEX_DROP_DOWN)
|
||||
.WithMinValues(1)
|
||||
.WithMaxValues(1);
|
||||
|
||||
foreach(ApiRole role in roles)
|
||||
{
|
||||
flexSelect.AddOption(role.Name, role.roleId.ToString(), role.Description);
|
||||
}
|
||||
|
||||
var builder = new ComponentBuilder()
|
||||
.WithSelectMenu(signUpSelect, 0)
|
||||
.WithButton("SignUp", $"{Constants.ComponentIds.SIGN_UP_BUTTON}-{raidId.ToString()}", ButtonStyle.Success, row: 1)
|
||||
.WithSelectMenu(flexSelect, 2)
|
||||
.WithButton("Maybe", $"{Constants.ComponentIds.MAYBE_BUTTON}-{raidId.ToString()}", ButtonStyle.Success, row: 3)
|
||||
.WithButton("Backup", $"{Constants.ComponentIds.BACKUP_BUTTON}-{raidId.ToString()}", ButtonStyle.Success, row: 3)
|
||||
.WithButton("Flex", $"{Constants.ComponentIds.FLEX_BUTTON}-{raidId.ToString()}", ButtonStyle.Success, row: 3);
|
||||
|
||||
return builder.Build();
|
||||
}*/
|
||||
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
using SharedClasses.SharedModels;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
using System.Text.Json;
|
||||
using System.Text;
|
||||
|
@ -20,6 +21,37 @@ namespace DiscordBot.Services
|
|||
};
|
||||
}
|
||||
|
||||
public async Task<bool> DoesUserExist(ulong userId)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
|
||||
|
||||
var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/DoesUserExist/{userId}");
|
||||
|
||||
if (httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
using var contentStream =
|
||||
await httpResponseMessage.Content.ReadAsStreamAsync();
|
||||
|
||||
return await JsonSerializer.DeserializeAsync<bool>(contentStream, _serializerOptions);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<Tuple<bool, string>> IsSignUpAllowed(int raidId, ulong userId)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
|
||||
|
||||
var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsSignUpAllowed/{raidId}/{userId}");
|
||||
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync<ProblemDetails>(_serializerOptions) ?? new ProblemDetails();
|
||||
string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail;
|
||||
return new Tuple<bool, string>(false, errorMessage);
|
||||
}
|
||||
return new Tuple<bool, string>(true, string.Empty);
|
||||
}
|
||||
|
||||
public async Task<List<ApiRole>> GetRoles(int raidId, ulong userId)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
|
||||
|
@ -73,5 +105,25 @@ namespace DiscordBot.Services
|
|||
var httpResponseMessage = await httpClient.PostAsync(requestUri, raidItemJson);
|
||||
}
|
||||
|
||||
public async Task<Tuple<bool, string>> CreateAccount(ApiRaid.Role.User user)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
|
||||
|
||||
var raidItemJson = new StringContent(
|
||||
JsonSerializer.Serialize(user),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
|
||||
var httpResponseMessage = await httpClient.PostAsync("DiscordBot/CreateAccount", raidItemJson);
|
||||
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync<ProblemDetails>(_serializerOptions) ?? new ProblemDetails();
|
||||
string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail;
|
||||
return new Tuple<bool, string>(false, errorMessage);
|
||||
}
|
||||
return new Tuple<bool, string>(true, string.Empty);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue