changing the sign up type is now possible with 1 click in the bot

This commit is contained in:
Sarah Faey 2022-12-11 01:10:50 +01:00
parent 058cc89cbc
commit e675323adc
7 changed files with 155 additions and 40 deletions

View file

@ -25,17 +25,17 @@ namespace DiscordBot.CommandHandlers
switch(ids[0])
{
case Constants.ComponentIds.SIGN_UP_BUTTON:
await SignUpClicked(component, false, SharedConstants.SIGNED_UP);
break;
case Constants.ComponentIds.MAYBE_BUTTON:
RaidMessage.ButtonParameters signUpParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
await _handlerFunctions.SelectRole(component, signUpParameters.RaidId, signUpParameters.ButtonType, false, component.User.Id, 0);
await SignUpClicked(component, false, SharedConstants.MAYBE);
break;
case Constants.ComponentIds.BACKUP_BUTTON:
RaidMessage.ButtonParameters backupParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
await _handlerFunctions.SelectRole(component, backupParameters.RaidId, backupParameters.ButtonType, true, component.User.Id, 0);
await SignUpClicked(component, true, SharedConstants.BACKUP);
break;
case Constants.ComponentIds.FLEX_BUTTON:
RaidMessage.ButtonParameters flexParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
if(await _httpService.IsUserSignedUp(flexParameters.RaidId, component.User.Id))
if(!string.IsNullOrEmpty(await _httpService.IsUserSignedUp(flexParameters.RaidId, component.User.Id)))
{
await _handlerFunctions.SelectRole(component, flexParameters.RaidId, flexParameters.ButtonType, true, component.User.Id, 0);
}
@ -66,5 +66,36 @@ namespace DiscordBot.CommandHandlers
break;
}
}
public async Task SignUpClicked(SocketMessageComponent component, bool allRoles, string signUpType)
{
RaidMessage.ButtonParameters parameters = RaidMessage.ParseButtonId(component.Data.CustomId);
string currentSignUp = await _httpService.IsUserSignedUp(parameters.RaidId, component.User.Id);
if(string.IsNullOrEmpty(currentSignUp) || currentSignUp == signUpType)
{
await _handlerFunctions.SelectRole(component, parameters.RaidId, parameters.ButtonType, allRoles, component.User.Id, 0);
}
else
{
ApiSignUp signUp = new ApiSignUp()
{
raidId = parameters.RaidId,
userId = component.User.Id
};
switch(signUpType)
{
case SharedConstants.SIGNED_UP:
await _httpService.ChangeToSignUp(signUp);
break;
case SharedConstants.MAYBE:
await _httpService.ChangeToMaybe(signUp);
break;
case SharedConstants.BACKUP:
await _httpService.ChangeToBackup(signUp);
break;
}
await component.RespondAsync("Sign up type changed.", ephemeral: true);
}
}
}
}

View file

@ -67,7 +67,7 @@ namespace DiscordBot.Services
return new Tuple<bool, string>(true, string.Empty);
}
public async Task<bool> IsUserSignedUp(int raidId, ulong userId)
public async Task<string> IsUserSignedUp(int raidId, ulong userId)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
@ -75,12 +75,9 @@ namespace DiscordBot.Services
if (httpResponseMessage.IsSuccessStatusCode)
{
using var contentStream =
await httpResponseMessage.Content.ReadAsStreamAsync();
return await JsonSerializer.DeserializeAsync<bool>(contentStream, _serializerOptions);
return await httpResponseMessage.Content.ReadAsStringAsync();
}
return false;
return string.Empty;
}
public async Task<List<ApiRole>> GetRoles(int raidId, ulong userId)
@ -124,6 +121,21 @@ namespace DiscordBot.Services
await SendSignUp(signUp, "DiscordBot/SignOff");
}
public async Task<bool> ChangeToSignUp(ApiSignUp signUp)
{
return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToSignUp");
}
public async Task<bool> ChangeToMaybe(ApiSignUp signUp)
{
return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToMaybe");
}
public async Task<bool> ChangeToBackup(ApiSignUp signUp)
{
return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToBackup");
}
private async Task<bool> SendSignUp(ApiSignUp signUp, string requestUri)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);