forked from Sarah/Lieb-Website
reworked the commandHandler
added Slash Commands
This commit is contained in:
parent
17dceda408
commit
b8feed971c
15 changed files with 599 additions and 214 deletions
62
DiscordBot/CommandHandlers/ButtonHandler.cs
Normal file
62
DiscordBot/CommandHandlers/ButtonHandler.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using System.Reflection;
|
||||
using DiscordBot.Services;
|
||||
using SharedClasses.SharedModels;
|
||||
using DiscordBot.Messages;
|
||||
|
||||
namespace DiscordBot.CommandHandlers
|
||||
{
|
||||
public class ButtonHandler
|
||||
{
|
||||
private readonly HttpService _httpService;
|
||||
private readonly HandlerFunctions _handlerFunctions;
|
||||
|
||||
public ButtonHandler(HttpService httpService)
|
||||
{
|
||||
_httpService = httpService;
|
||||
_handlerFunctions = new HandlerFunctions(_httpService);
|
||||
}
|
||||
|
||||
public async Task Handler(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_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: 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:
|
||||
if(await _handlerFunctions.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()
|
||||
{
|
||||
raidId = parsedRaidId,
|
||||
userId = component.User.Id
|
||||
};
|
||||
await _httpService.SignOff(signOff);
|
||||
await _handlerFunctions.Respond(component);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
56
DiscordBot/CommandHandlers/HandlerFunctions.cs
Normal file
56
DiscordBot/CommandHandlers/HandlerFunctions.cs
Normal file
|
@ -0,0 +1,56 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using System.Reflection;
|
||||
using DiscordBot.Services;
|
||||
using SharedClasses.SharedModels;
|
||||
using DiscordBot.Messages;
|
||||
|
||||
namespace DiscordBot.CommandHandlers
|
||||
{
|
||||
public class HandlerFunctions
|
||||
{
|
||||
private readonly HttpService _httpService;
|
||||
|
||||
public HandlerFunctions(HttpService httpService)
|
||||
{
|
||||
_httpService = httpService;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//to avoid error messages because of no response...
|
||||
public async Task Respond(SocketInteraction component)
|
||||
{
|
||||
try
|
||||
{
|
||||
await component.RespondAsync();
|
||||
}
|
||||
catch(Discord.Net.HttpException e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
72
DiscordBot/CommandHandlers/ModalHandler.cs
Normal file
72
DiscordBot/CommandHandlers/ModalHandler.cs
Normal file
|
@ -0,0 +1,72 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using System.Reflection;
|
||||
using DiscordBot.Services;
|
||||
using SharedClasses.SharedModels;
|
||||
using DiscordBot.Messages;
|
||||
using DiscordBot;
|
||||
|
||||
namespace DiscordBot.CommandHandlers
|
||||
{
|
||||
public class ModalHandler
|
||||
{
|
||||
private readonly HttpService _httpService;
|
||||
private readonly HandlerFunctions _handlerFunctions;
|
||||
|
||||
public ModalHandler(HttpService httpService)
|
||||
{
|
||||
_httpService = httpService;
|
||||
_handlerFunctions = new HandlerFunctions(_httpService);
|
||||
}
|
||||
|
||||
public async Task Handler(SocketModal modal)
|
||||
{
|
||||
string[] ids = modal.Data.CustomId.Split('-');
|
||||
List<SocketMessageComponentData> components = modal.Data.Components.ToList();
|
||||
switch(ids[0])
|
||||
{
|
||||
case Constants.ComponentIds.CREATE_ACCOUNT_MODAL:
|
||||
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
|
||||
if(ids.Length > 2 && int.TryParse(ids[1], out int parsedRaidId) && await _handlerFunctions.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 _handlerFunctions.Respond(modal);
|
||||
break;
|
||||
case Constants.ComponentIds.SIGN_UP_EXTERNAL_MODAL:
|
||||
string userName = components.First(x => x.CustomId == Constants.ComponentIds.NAME_TEXT_BOX).Value;
|
||||
int raidId = int.Parse(ids[1]);
|
||||
int roleId = int.Parse(ids[2]);
|
||||
ApiSignUp signUpExternal = new ApiSignUp()
|
||||
{
|
||||
raidId = raidId,
|
||||
userName = userName,
|
||||
signedUpByUserId = modal.User.Id,
|
||||
roleId = roleId
|
||||
};
|
||||
await _httpService.SignUp(signUpExternal);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
96
DiscordBot/CommandHandlers/SelectMenuHandler.cs
Normal file
96
DiscordBot/CommandHandlers/SelectMenuHandler.cs
Normal file
|
@ -0,0 +1,96 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using System.Reflection;
|
||||
using DiscordBot.Services;
|
||||
using SharedClasses.SharedModels;
|
||||
using DiscordBot.Messages;
|
||||
|
||||
namespace DiscordBot.CommandHandlers
|
||||
{
|
||||
public class SelectMenuHandler
|
||||
{
|
||||
private readonly HttpService _httpService;
|
||||
private readonly HandlerFunctions _handlerFunctions;
|
||||
|
||||
public SelectMenuHandler(HttpService httpService)
|
||||
{
|
||||
_httpService = httpService;
|
||||
_handlerFunctions = new HandlerFunctions(_httpService);
|
||||
}
|
||||
|
||||
public async Task Handler(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:
|
||||
ulong userId = 0;
|
||||
if(ids.Length >= 4)
|
||||
{
|
||||
ulong.TryParse(ids[3],out userId);
|
||||
}
|
||||
await ManageSignUp(ids[2], parsedRaidId, component, userId);
|
||||
await component.RespondAsync("successfully signed up");
|
||||
break;
|
||||
case Constants.ComponentIds.SIGN_UP_EXTERNAL_DROP_DOWN:
|
||||
await component.RespondWithModalAsync(CreateUserNameModal(parsedRaidId, int.Parse(component.Data.Values.First())));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ManageSignUp(string buttonType, int raidId, SocketMessageComponent component, ulong userIdToSignUp)
|
||||
{
|
||||
if(! int.TryParse(component.Data.Values.First(), out int parsedRoleId)) return;
|
||||
|
||||
ApiSignUp signUp = new ApiSignUp()
|
||||
{
|
||||
raidId = raidId,
|
||||
roleId = parsedRoleId
|
||||
};
|
||||
|
||||
if(userIdToSignUp == 0)
|
||||
{
|
||||
signUp.userId = component.User.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
signUp.userId = userIdToSignUp;
|
||||
signUp.signedUpByUserId = component.User.Id;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private Modal CreateUserNameModal(int raidId, int roleId)
|
||||
{
|
||||
var mb = new ModalBuilder()
|
||||
.WithTitle("Create Account")
|
||||
.WithCustomId($"{Constants.ComponentIds.SIGN_UP_EXTERNAL_MODAL}-{raidId}-{roleId}")
|
||||
.AddTextInput("Name", Constants.ComponentIds.NAME_TEXT_BOX, placeholder: "Name", required: true);
|
||||
|
||||
return mb.Build();
|
||||
}
|
||||
}
|
||||
}
|
154
DiscordBot/CommandHandlers/SlashCommandHandler.cs
Normal file
154
DiscordBot/CommandHandlers/SlashCommandHandler.cs
Normal file
|
@ -0,0 +1,154 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using System.Reflection;
|
||||
using DiscordBot.Services;
|
||||
using SharedClasses.SharedModels;
|
||||
using DiscordBot.Messages;
|
||||
using DiscordBot;
|
||||
|
||||
namespace DiscordBot.CommandHandlers
|
||||
{
|
||||
public class SlashCommandHandler
|
||||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly HttpService _httpService;
|
||||
private readonly HandlerFunctions _handlerFunctions;
|
||||
|
||||
public SlashCommandHandler(DiscordSocketClient client, HttpService httpService)
|
||||
{
|
||||
_client = client;
|
||||
_httpService = httpService;
|
||||
_handlerFunctions = new HandlerFunctions(_httpService);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task Handler(SocketSlashCommand command)
|
||||
{
|
||||
switch (command.Data.Name)
|
||||
{
|
||||
case Constants.SlashCommands.RAID:
|
||||
await HandleRaidCommands(command);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleRaidCommands(SocketSlashCommand command)
|
||||
{
|
||||
switch (command.Data.Options.First().Name)
|
||||
{
|
||||
case Constants.SlashCommands.USER:
|
||||
await HandleUserCommands(command);
|
||||
break;
|
||||
case Constants.SlashCommands.SEND_MESSAGE_COMMAND:
|
||||
int raidId = (int)(long)command.Data.Options.First().Options?.FirstOrDefault(o => o.Name == Constants.SlashCommands.OptionNames.RAID_ID).Value;
|
||||
string message = (string)command.Data.Options.First().Options?.FirstOrDefault(o => o.Name == Constants.SlashCommands.OptionNames.MESSAGE).Value;
|
||||
await SendMessages(message, raidId);
|
||||
await command.RespondAsync($"message sent", ephemeral:true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleUserCommands(SocketSlashCommand command)
|
||||
{
|
||||
int raidId = (int)(long)command.Data.Options.First().Options.First().Options?.FirstOrDefault(o => o.Name == Constants.SlashCommands.OptionNames.RAID_ID).Value;
|
||||
string userName = string.Empty;
|
||||
IUser user;
|
||||
switch (command.Data.Options.First().Options.First().Name)
|
||||
{
|
||||
case Constants.SlashCommands.ADD_USER_COMMAND:
|
||||
user = (IUser)command.Data.Options.First().Options.First().Options?.FirstOrDefault(o => o.Name == Constants.SlashCommands.OptionNames.USER).Value;
|
||||
await SignUpUser(command, user, raidId);
|
||||
await command.RespondAsync($"signed up {user.Username}", ephemeral:true);
|
||||
break;
|
||||
case Constants.SlashCommands.ADD_EXTERNAL_USER_COMMAND:
|
||||
await SignUpExternalUser(command, raidId);
|
||||
break;
|
||||
case Constants.SlashCommands.REMOVE_USER_COMMAND:
|
||||
user = (IUser)command.Data.Options.First().Options.First().Options?.FirstOrDefault(o => o.Name == Constants.SlashCommands.OptionNames.USER).Value;
|
||||
ApiSignUp signOff = new ApiSignUp()
|
||||
{
|
||||
raidId = raidId,
|
||||
userId = user.Id,
|
||||
signedUpByUserId = command.User.Id
|
||||
};
|
||||
await _httpService.SignOff(signOff);
|
||||
await command.RespondAsync($"signed off {user.Username}", ephemeral:true);
|
||||
break;
|
||||
case Constants.SlashCommands.REMOVE_EXTERNAL_USER_COMMAND:
|
||||
userName = (string)command.Data.Options.First().Options.First().Options?.FirstOrDefault(o => o.Name == Constants.SlashCommands.OptionNames.USER_NAME).Value;
|
||||
ApiSignUp signOffExternal = new ApiSignUp()
|
||||
{
|
||||
raidId = raidId,
|
||||
userName = userName,
|
||||
signedUpByUserId = command.User.Id
|
||||
};
|
||||
await _httpService.SignOff(signOffExternal);
|
||||
await command.RespondAsync($"signed off {userName}", ephemeral:true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SignUpUser(SocketSlashCommand command, IUser user, int raidId)
|
||||
{
|
||||
if(await _handlerFunctions.IsRaidSignUpAllowed(command, raidId, Constants.ComponentIds.SIGN_UP_BUTTON))
|
||||
{
|
||||
List<ApiRole> roles = await _httpService.GetRoles(raidId, user.Id);
|
||||
if(await _httpService.DoesUserExist(user.Id))
|
||||
{
|
||||
await command.RespondAsync("Please choose a role.", components: SignUpMessage.buildMessage(roles, raidId, Constants.ComponentIds.SIGN_UP_BUTTON, false, user.Id) , ephemeral: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
await SignUpExternalUser(command, raidId, roles);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SignUpExternalUser(SocketSlashCommand command, int raidId)
|
||||
{
|
||||
if(await _handlerFunctions.IsRaidSignUpAllowed(command, raidId, Constants.ComponentIds.SIGN_UP_BUTTON))
|
||||
{
|
||||
List<ApiRole> roles = await _httpService.GetRoles(raidId, uint.MaxValue);
|
||||
await SignUpExternalUser(command, raidId, roles);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SignUpExternalUser(SocketSlashCommand command, int raidId, List<ApiRole> roles)
|
||||
{
|
||||
var signUpSelect = new SelectMenuBuilder()
|
||||
.WithPlaceholder("Select an option")
|
||||
.WithCustomId($"{Constants.ComponentIds.SIGN_UP_EXTERNAL_DROP_DOWN}-{raidId}")
|
||||
.WithMinValues(1)
|
||||
.WithMaxValues(1);
|
||||
|
||||
foreach(ApiRole role in roles)
|
||||
{
|
||||
if(role.IsSignUpAllowed)
|
||||
signUpSelect.AddOption(role.Name, role.roleId.ToString(), role.Description);
|
||||
}
|
||||
|
||||
var builder = new ComponentBuilder()
|
||||
.WithSelectMenu(signUpSelect, 0);
|
||||
|
||||
await command.RespondAsync("Please choose a role.", components: builder.Build() , ephemeral: true);
|
||||
}
|
||||
|
||||
private async Task SendMessages(string message, int raidId)
|
||||
{
|
||||
ApiRaid raid = await _httpService.GetRaid(raidId);
|
||||
foreach(ApiRaid.Role role in raid.Roles)
|
||||
{
|
||||
foreach(var user in role.Users)
|
||||
{
|
||||
if(user.UserId > 100)
|
||||
{
|
||||
IUser discordUser = await _client.GetUserAsync(user.UserId);
|
||||
await discordUser.SendMessageAsync($"{raid.Title}: {message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue