implemented polls in the discord bot

This commit is contained in:
Sarah Faey 2023-01-14 16:44:18 +01:00
parent 1d999e89bf
commit 715b14ecc5
13 changed files with 277 additions and 6 deletions

View file

@ -64,6 +64,22 @@ namespace DiscordBot.CommandHandlers
await component.RespondAsync("Opting out failed, please try again later or change the setting on the website.");
}
break;
case Constants.ComponentIds.POLL_ANSWER_BUTTON:
PollMessage.Parameters pollAnswerParameters = PollMessage.ParseId(component.Data.CustomId);
ApiPollAnswer answer = new ApiPollAnswer()
{
Answer = string.Empty,
OptionId = pollAnswerParameters.OptionId,
PollId = pollAnswerParameters.PollId,
UserId = component.User.Id
};
await _httpService.AnswerPoll(answer);
await component.RespondAsync("Answer sent.", ephemeral: true);
break;
case Constants.ComponentIds.POLL_CUSTOM_ANSWER_BUTTON:
PollMessage.Parameters pollCustomParameters = PollMessage.ParseId(component.Data.CustomId);
await component.RespondWithModalAsync(PollCustomModal.buildMessage(pollCustomParameters.PollId, component.Message.Content));
break;
}
}

View file

@ -63,6 +63,19 @@ namespace DiscordBot.CommandHandlers
await modal.RespondAsync($"signing up failed", ephemeral: true);
}
break;
case Constants.ComponentIds.POLL_CUSTOM_ANSWER_MODAL:
PollCustomModal.Parameters pollParameters = PollCustomModal.ParseId(modal.Data.CustomId);
string modalAnswer = components.First(x => x.CustomId == Constants.ComponentIds.POLL_CUSTOM_ANSWER_TEXT_BOX).Value;
ApiPollAnswer answer = new ApiPollAnswer()
{
Answer = modalAnswer,
OptionId = 0,
PollId = pollParameters.PollId,
UserId = modal.User.Id
};
await _httpService.AnswerPoll(answer);
await modal.RespondAsync("Answer sent.", ephemeral: true);
break;
}
}
}

View file

@ -54,6 +54,18 @@ namespace DiscordBot.CommandHandlers
});
}
break;
case Constants.ComponentIds.POLL_DROP_DOWN:
PollMessage.Parameters pollParameters = PollMessage.ParseId(component.Data.CustomId);
ApiPollAnswer answer = new ApiPollAnswer()
{
Answer = string.Empty,
OptionId = int.Parse(component.Data.Values.First()),
PollId = pollParameters.PollId,
UserId = component.User.Id
};
await _httpService.AnswerPoll(answer);
await component.RespondAsync("Answer sent.", ephemeral: true);
break;
}
}

View file

@ -22,6 +22,13 @@
public const string CREATE_ACCOUNT_MODAL = "createAccountModal";
public const string SIGN_UP_EXTERNAL_MODAL = "signUpExternalModal";
public const string POLL_DROP_DOWN = "pollDropDown";
public const string POLL_ANSWER_BUTTON = "pollAnswerButton";
public const string POLL_CUSTOM_ANSWER_BUTTON = "pollCustomAnswerButton";
public const string POLL_CUSTOM_ANSWER_MODAL = "pollCustomAnswerModal";
public const string POLL_CUSTOM_ANSWER_TEXT_BOX = "pollCustomAnswerTextBox";
}
public class SlashCommands

View file

@ -118,5 +118,38 @@ namespace DiscordBot.Controllers
{
await ReminderSubscriptionMessage.sendMessage(_client, userId);
}
[HttpPost]
[Route("[action]")]
public async Task<List<ulong>> SendDropdownPoll(ApiPoll poll)
{
return await SendPoll(poll, true);
}
[HttpPost]
[Route("[action]")]
public async Task<List<ulong>> SendButtonPoll(ApiPoll poll)
{
return await SendPoll(poll, false);
}
private async Task<List<ulong>> SendPoll(ApiPoll poll, bool isDropdown)
{
List<ulong> sent = new List<ulong>();
foreach(ulong userId in poll.UserIds)
{
var user = await _client.GetUserAsync(userId);
if(user != null)
{
try
{
await user.SendMessageAsync(poll.Question, components: PollMessage.buildMessage(poll, isDropdown));
sent.Add(user.Id);
}
catch {}
}
}
return sent;
}
}
}

View file

@ -0,0 +1,38 @@
using Discord;
using Discord.WebSocket;
using System;
using System.ComponentModel.DataAnnotations;
using SharedClasses.SharedModels;
namespace DiscordBot.Messages
{
public class PollCustomModal
{
public static Modal buildMessage(int pollId, string question)
{
var mb = new ModalBuilder()
.WithTitle(question)
.WithCustomId($"{Constants.ComponentIds.POLL_CUSTOM_ANSWER_MODAL}-{pollId}")
.AddTextInput("Answer", Constants.ComponentIds.POLL_CUSTOM_ANSWER_TEXT_BOX, placeholder: "Yes", required: true);
return mb.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.PollId);
}
return parameters;
}
public class Parameters
{
public int PollId;
}
}
}

View file

@ -0,0 +1,63 @@
using Discord;
using SharedClasses.SharedModels;
namespace DiscordBot.Messages
{
public class PollMessage
{
public static MessageComponent buildMessage(ApiPoll poll, bool isDropdown)
{
var builder = new ComponentBuilder();
if(isDropdown)
{
var signUpSelect = new SelectMenuBuilder()
.WithPlaceholder(poll.Question)
.WithCustomId($"{Constants.ComponentIds.POLL_DROP_DOWN}-{poll.PollId}")
.WithMinValues(1)
.WithMaxValues(1);
foreach(KeyValuePair<int, string> option in poll.Options)
{
signUpSelect.AddOption(option.Value, option.Key.ToString());
}
builder.WithSelectMenu(signUpSelect, 0);
}
else
{
foreach(KeyValuePair<int, string> option in poll.Options)
{
builder.WithButton(option.Value, $"{Constants.ComponentIds.POLL_ANSWER_BUTTON}-{poll.PollId}-{option.Key}", ButtonStyle.Secondary);
}
}
if(poll.AllowCustomAnswer)
{
builder.WithButton("Custom", $"{Constants.ComponentIds.POLL_CUSTOM_ANSWER_BUTTON}-{poll.PollId}", ButtonStyle.Secondary);
}
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.PollId);
}
if(ids.Length > 2)
{
int.TryParse(ids[2], out parameters.OptionId);
}
return parameters;
}
public class Parameters
{
public int PollId;
public int OptionId;
}
}
}

View file

@ -269,5 +269,19 @@ namespace DiscordBot.Services
}
return false;
}
public async Task AnswerPoll(ApiPollAnswer answer)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
var raidItemJson = new StringContent(
JsonSerializer.Serialize(answer),
Encoding.UTF8,
Application.Json);
var httpResponseMessage = await httpClient.PostAsync("DiscordBot/AnswerPoll", raidItemJson);
httpResponseMessage.EnsureSuccessStatusCode();
}
}
}