Added Remove user via dropdown command

This commit is contained in:
t.ruspekhofer 2022-12-26 11:17:12 +01:00
parent 05487cba84
commit bb5926d062
5 changed files with 97 additions and 4 deletions

View file

@ -1,10 +1,7 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using System.Reflection; using DiscordBot.Messages;
using DiscordBot.Services; using DiscordBot.Services;
using SharedClasses.SharedModels; using SharedClasses.SharedModels;
using DiscordBot.Messages;
namespace DiscordBot.CommandHandlers namespace DiscordBot.CommandHandlers
{ {
@ -33,6 +30,10 @@ namespace DiscordBot.CommandHandlers
ExternalRoleSelectionMessage.Parameters externalRoleParameters = ExternalRoleSelectionMessage.ParseId(component.Data.CustomId); ExternalRoleSelectionMessage.Parameters externalRoleParameters = ExternalRoleSelectionMessage.ParseId(component.Data.CustomId);
await component.RespondWithModalAsync(ExternalUserNameModal.buildMessage(externalRoleParameters.RaidId, int.Parse(component.Data.Values.First()))); await component.RespondWithModalAsync(ExternalUserNameModal.buildMessage(externalRoleParameters.RaidId, int.Parse(component.Data.Values.First())));
break; break;
case Constants.ComponentIds.REMOVE_USER_DROP_DOWN:
RemoveUserMessage.Parameters removeUserParameters = RemoveUserMessage.ParseId(component.Data.CustomId);
await RemoveUser(component, removeUserParameters);
break;
case Constants.ComponentIds.ACCOUNT_SELECT_DROP_DOWN: case Constants.ComponentIds.ACCOUNT_SELECT_DROP_DOWN:
AccountSelectionMessage.Parameters accountParameters = AccountSelectionMessage.ParseId(component.Data.CustomId); AccountSelectionMessage.Parameters accountParameters = AccountSelectionMessage.ParseId(component.Data.CustomId);
int accountId = int.Parse(component.Data.Values.First()); int accountId = int.Parse(component.Data.Values.First());
@ -55,5 +56,24 @@ namespace DiscordBot.CommandHandlers
break; break;
} }
} }
private async Task RemoveUser(SocketMessageComponent component, RemoveUserMessage.Parameters removeUserParameters)
{
ApiSignUp signOff = new ApiSignUp()
{
raidId = removeUserParameters.RaidId,
signedUpByUserId = removeUserParameters.SignedUpByUserId
};
if (ulong.TryParse(component.Data.Values.First(), out ulong userId))
{
signOff.userId = userId;
}
else
{
signOff.userName = component.Data.Values.First();
}
await _httpService.SignOff(signOff);
await _handlerFunctions.UpdateOrRespond(component, $"signed off {component.Data.Values.First()}", null, true);
}
} }
} }

View file

@ -94,6 +94,10 @@ namespace DiscordBot.CommandHandlers
await _httpService.SignOff(signOffExternal); await _httpService.SignOff(signOffExternal);
await command.RespondAsync($"signed off {userName}", ephemeral:true); await command.RespondAsync($"signed off {userName}", ephemeral:true);
break; break;
case Constants.SlashCommands.REMOVE_USER_DROPDOWN_COMMAND:
ApiRaid raid = await _httpService.GetRaid(raidId);
await command.RespondAsync("Which user do you want to sign off?", components: RemoveUserMessage.buildMessage(raid, command.User.Id), ephemeral: true);
break;
} }
} }

View file

@ -15,6 +15,7 @@
public const string ROLE_SELECT_DROP_DOWN = "roleSelectDropDown"; public const string ROLE_SELECT_DROP_DOWN = "roleSelectDropDown";
public const string ROLE_SELECT_EXTERNAL_DROP_DOWN = "roleSelectExternalDropDown"; public const string ROLE_SELECT_EXTERNAL_DROP_DOWN = "roleSelectExternalDropDown";
public const string ACCOUNT_SELECT_DROP_DOWN = "accountSelectDropDown"; public const string ACCOUNT_SELECT_DROP_DOWN = "accountSelectDropDown";
public const string REMOVE_USER_DROP_DOWN = "removeUserDropDown";
public const string NAME_TEXT_BOX = "nameTextbox"; public const string NAME_TEXT_BOX = "nameTextbox";
public const string ACCOUNT_TEXT_BOX = "accountTextBox"; public const string ACCOUNT_TEXT_BOX = "accountTextBox";
@ -33,6 +34,7 @@
public const string REMOVE_USER_COMMAND = "remove"; public const string REMOVE_USER_COMMAND = "remove";
public const string ADD_EXTERNAL_USER_COMMAND = "add-external"; public const string ADD_EXTERNAL_USER_COMMAND = "add-external";
public const string REMOVE_EXTERNAL_USER_COMMAND = "remove-external"; public const string REMOVE_EXTERNAL_USER_COMMAND = "remove-external";
public const string REMOVE_USER_DROPDOWN_COMMAND = "remove-dropdown";
public class OptionNames public class OptionNames
{ {

View file

@ -0,0 +1,61 @@
using Discord;
using SharedClasses.SharedModels;
using System.Security.Principal;
namespace DiscordBot.Messages
{
public class RemoveUserMessage
{
public static MessageComponent buildMessage(ApiRaid raid, ulong signedUpByUserId)
{
var signUpSelect = new SelectMenuBuilder()
.WithPlaceholder("Select an account")
.WithCustomId($"{Constants.ComponentIds.REMOVE_USER_DROP_DOWN}-{raid.RaidId}-{signedUpByUserId}")
.WithMinValues(1)
.WithMaxValues(1);
foreach (ApiRaid.Role role in raid.Roles)
{
foreach (ApiRaid.Role.User user in role.Users)
{
if(user.UserId > 0)
{
signUpSelect.AddOption($"({user.UserName} | {user.AccountName}", user.UserId.ToString());
}
else
{
signUpSelect.AddOption(user.UserName, user.UserName);
}
}
}
var builder = new ComponentBuilder()
.WithSelectMenu(signUpSelect, 0);
return builder.Build();
}
public static Parameters ParseId(string customId)
{
Parameters parameters = new Parameters();
string[] ids = customId.Split('-');
if (ids.Length > 1)
{
int.TryParse(ids[1], out parameters.RaidId);
}
if (ids.Length > 2)
{
ulong.TryParse(ids[2], out parameters.SignedUpByUserId);
}
return parameters;
}
public class Parameters
{
public int RaidId;
public ulong SignedUpByUserId;
}
}
}

View file

@ -47,6 +47,12 @@ namespace DiscordBot.SlashCommands
.AddOption(Constants.SlashCommands.OptionNames.RAID_ID, ApplicationCommandOptionType.Integer, "The Id of the Raid, found at the bottom of the raid message", isRequired: true) .AddOption(Constants.SlashCommands.OptionNames.RAID_ID, ApplicationCommandOptionType.Integer, "The Id of the Raid, found at the bottom of the raid message", isRequired: true)
.AddOption(Constants.SlashCommands.OptionNames.USER_NAME, ApplicationCommandOptionType.String, "The user name you want to sign off", isRequired: true) .AddOption(Constants.SlashCommands.OptionNames.USER_NAME, ApplicationCommandOptionType.String, "The user name you want to sign off", isRequired: true)
) )
.AddOption(new SlashCommandOptionBuilder()
.WithName(Constants.SlashCommands.REMOVE_USER_DROPDOWN_COMMAND)
.WithDescription("Sign off user via dropdown")
.WithType(ApplicationCommandOptionType.SubCommand)
.AddOption(Constants.SlashCommands.OptionNames.RAID_ID, ApplicationCommandOptionType.Integer, "The Id of the Raid, found at the bottom of the raid message", isRequired: true)
)
) )
.AddOption(new SlashCommandOptionBuilder() .AddOption(new SlashCommandOptionBuilder()
.WithName(Constants.SlashCommands.SEND_MESSAGE_COMMAND) .WithName(Constants.SlashCommands.SEND_MESSAGE_COMMAND)