signing up through the discord bot works now

This commit is contained in:
Sarah Faey 2022-11-07 00:16:49 +01:00
parent e445b2a181
commit 69337e69ae
11 changed files with 327 additions and 42 deletions

View file

@ -2,6 +2,9 @@
using Discord.Commands;
using Discord.WebSocket;
using System.Reflection;
using DiscordBot.Services;
using SharedClasses.SharedModels;
using DiscordBot.Messages;
namespace DiscordBot
{
@ -9,18 +12,21 @@ namespace DiscordBot
{
private readonly DiscordSocketClient _client;
private readonly CommandService _commands;
private readonly HttpService _httpService;
// Retrieve client and CommandService instance via ctor
public CommandHandler(DiscordSocketClient client, CommandService commands)
public CommandHandler(DiscordSocketClient client, CommandService commands, HttpService httpService)
{
_commands = commands;
_client = client;
_httpService = httpService;
}
public async Task InstallCommandsAsync()
{
_client.SlashCommandExecuted += SlashCommandHandler;
_client.ButtonExecuted += MyButtonHandler;
_client.ButtonExecuted += ButtonHandler;
_client.SelectMenuExecuted += SelectMenuHandler;
}
private async Task SlashCommandHandler(SocketSlashCommand command)
@ -54,28 +60,95 @@ namespace DiscordBot
await command.RespondAsync(embed: embedBuiler.Build());
}
public async Task MyButtonHandler(SocketMessageComponent component)
public async Task ButtonHandler(SocketMessageComponent component)
{
string[] ids = component.Data.CustomId.Split('-');
List<ApiRole> roles = new List<ApiRole>();
int parsedRaidId = 0;
if(ids.Length > 1)
{
int.TryParse(ids[1],out parsedRaidId);
}
switch(ids[0])
{
case Constants.ComponentIds.SIGN_UP:
//await component.RespondAsync($"{component.User.Mention} has clicked the SignUp button!");
var mb = new ModalBuilder()
.WithTitle("Fav Food")
.WithCustomId("food_menu")
.AddTextInput("What??", "food_name", placeholder:"Pizza")
.AddTextInput("Why??", "food_reason", TextInputStyle.Paragraph,
"Kus it's so tasty");
//await component.RespondWithModalAsync(mb.Build());
await component.RespondAsync("hi", ephemeral: true);
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);
break;
case Constants.ComponentIds.SIGN_OFF:
//await component.RespondAsync($"{component.User.Mention} has clicked the SignOff button!");
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);
break;
case Constants.ComponentIds.SIGN_OFF_BUTTON:
ApiSignUp signOff = new ApiSignUp()
{
raidId = parsedRaidId,
userId = component.User.Id
};
await _httpService.SignOff(signOff);
await Respond(component);
break;
}
}
public async Task SelectMenuHandler(SocketMessageComponent component)
{
string[] ids = component.Data.CustomId.Split('-');
List<ApiRole> roles = new List<ApiRole>();
int parsedRaidId = 0;
if(ids.Length > 1)
{
int.TryParse(ids[1],out parsedRaidId);
}
switch(ids[0])
{
case Constants.ComponentIds.SIGN_UP_DROP_DOWN:
await ManageSignUp(ids[2], parsedRaidId, component);
await Respond(component);
break;
}
}
private async Task ManageSignUp(string buttonType, int raidId, SocketMessageComponent component)
{
if(! int.TryParse(component.Data.Values.First(), out int parsedRoleId)) return;
ApiSignUp signUp = new ApiSignUp()
{
raidId = raidId,
userId = component.User.Id,
roleId = parsedRoleId
};
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...
private async Task Respond(SocketMessageComponent component)
{
try
{
await component.RespondAsync();
}
catch(Discord.Net.HttpException e)
{
}
}
}