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

@ -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;
}
}
}