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

@ -17,15 +17,17 @@ namespace Lieb.Controllers
{
RaidService _raidService;
UserService _userService;
PollService _pollService;
GuildWars2AccountService _gw2AccountService;
DiscordService _discordService;
public DiscordBotController(RaidService raidService, UserService userService, GuildWars2AccountService gw2AccountService, DiscordService discordService)
public DiscordBotController(RaidService raidService, UserService userService, GuildWars2AccountService gw2AccountService, DiscordService discordService, PollService pollService)
{
_raidService = raidService;
_userService = userService;
_gw2AccountService = gw2AccountService;
_discordService = discordService;
_pollService = pollService;
}
[HttpGet]
@ -306,5 +308,12 @@ namespace Lieb.Controllers
{
return Ok(await _userService.ReminderOptOut(userId));
}
[HttpPost]
[Route("[action]")]
public async Task AnswerPoll(ApiPollAnswer answer)
{
await _pollService.UpdateAnswer(answer.PollId, answer.OptionId, answer.Answer, answer.UserId);
}
}
}

View file

@ -171,11 +171,11 @@ namespace Lieb.Data
context.RaidSignUps.AddRange(signUps);
context.SaveChanges();
GuildWars2Build healTempest = new GuildWars2Build() { BuildName = "HealTempest", Class = GuildWars2Class.Elementalist, EliteSpecialization = EliteSpecialization.Tempest, Might = true, Alacrity = true, DamageType = DamageType.Heal };
GuildWars2Build condiScourge = new GuildWars2Build() { BuildName = "CondiScourge", Class = GuildWars2Class.Necromancer, EliteSpecialization = EliteSpecialization.Scourge, DamageType = DamageType.Condition };
GuildWars2Build quickBrand = new GuildWars2Build() { BuildName = "QuickBrand", Class = GuildWars2Class.Guard, EliteSpecialization = EliteSpecialization.Firebrand, Quickness = true, DamageType = DamageType.Condition };
GuildWars2Build alacregate = new GuildWars2Build() { BuildName = "Alacregate", Class = GuildWars2Class.Revenant, EliteSpecialization = EliteSpecialization.Renegade, Alacrity = true, DamageType = DamageType.Power };
GuildWars2Build chrono = new GuildWars2Build() { BuildName = "Chrono", Class = GuildWars2Class.Mesmer, EliteSpecialization = EliteSpecialization.Chronomancer, Alacrity = true, Quickness = true, DamageType = DamageType.Power };
GuildWars2Build healTempest = new GuildWars2Build() { BuildName = "HealTempest", Class = GuildWars2Class.Elementalist, EliteSpecialization = EliteSpecialization.Tempest, Might = true, Alacrity = true, DamageType = DamageType.Heal, UseInRandomRaid = true };
GuildWars2Build condiScourge = new GuildWars2Build() { BuildName = "CondiScourge", Class = GuildWars2Class.Necromancer, EliteSpecialization = EliteSpecialization.Scourge, DamageType = DamageType.Condition, UseInRandomRaid = true };
GuildWars2Build quickBrand = new GuildWars2Build() { BuildName = "QuickBrand", Class = GuildWars2Class.Guard, EliteSpecialization = EliteSpecialization.Firebrand, Quickness = true, DamageType = DamageType.Condition, UseInRandomRaid = true };
GuildWars2Build alacregate = new GuildWars2Build() { BuildName = "Alacregate", Class = GuildWars2Class.Revenant, EliteSpecialization = EliteSpecialization.Renegade, Alacrity = true, DamageType = DamageType.Power, UseInRandomRaid = true };
GuildWars2Build chrono = new GuildWars2Build() { BuildName = "Chrono", Class = GuildWars2Class.Mesmer, EliteSpecialization = EliteSpecialization.Chronomancer, Alacrity = true, Quickness = true, DamageType = DamageType.Power, UseInRandomRaid = true };
GuildWars2Build daredevil = new GuildWars2Build() { BuildName = "Daredevil", Class = GuildWars2Class.Thief, EliteSpecialization = EliteSpecialization.DareDevil };
context.GuildWars2Builds.AddRange(new List<GuildWars2Build>(){healTempest, condiScourge, quickBrand, alacregate, chrono, daredevil });
context.SaveChanges();

View file

@ -5,6 +5,7 @@ using System.Text.Json;
using System.Text;
using Lieb.Models.GuildWars2.Raid;
using Lieb.Models;
using Lieb.Models.Poll;
using Microsoft.EntityFrameworkCore;
namespace Lieb.Data
@ -421,5 +422,42 @@ namespace Lieb.Data
}
catch {}
}
public async Task SendPoll(Poll poll, List<ulong>? userIds = null)
{
try
{
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);
if(userIds == null)
{
userIds = poll.Answers.Select(a => a.UserId).ToList();
}
ApiPoll apiPoll = new ApiPoll()
{
AllowCustomAnswer = poll.AllowCustomAnswer,
PollId = poll.PollId,
Question = poll.Question,
UserIds = userIds,
Options = poll.Options.ToDictionary(o => o.PollOptionId, o => o.Name)
};
var messageItemJson = new StringContent(
JsonSerializer.Serialize(apiPoll),
Encoding.UTF8,
Application.Json);
if(poll.AnswerType == AnswerType.Dropdown)
{
var httpResponseMessage = await httpClient.PostAsync("raid/SendDropdownPoll", messageItemJson);
}
else
{
var httpResponseMessage = await httpClient.PostAsync("raid/SendButtonPoll", messageItemJson);
}
}
catch {}
}
}
}

View file

@ -74,6 +74,7 @@ namespace Lieb.Data
using var context = _contextFactory.CreateDbContext();
context.Polls.Add(poll);
await context.SaveChangesAsync();
await _discordService.SendPoll(poll);
return poll.PollId;
}
@ -102,11 +103,18 @@ namespace Lieb.Data
using var context = _contextFactory.CreateDbContext();
Poll? poll = context.Polls
.Include(p => p.Answers)
.Include(p => p.Options)
.FirstOrDefault(p => p.PollId == pollId && p.Answers.Where(a => a.UserId == userId).Any());
if (poll == null) return;
PollAnswer pollAnswer = poll.Answers.First(a => a.UserId == userId);
if(string.IsNullOrEmpty(answer) && pollOptionId > 0)
{
PollOption option = poll.Options.FirstOrDefault(o => o.PollOptionId == pollOptionId);
answer = option != null ? option.Name : string.Empty;
}
pollAnswer.Answer = answer;
pollAnswer.PollOptionId = pollOptionId;
await context.SaveChangesAsync();
@ -126,6 +134,7 @@ namespace Lieb.Data
UserId = userId
});
await context.SaveChangesAsync();
await _discordService.SendPoll(poll, new List<ulong>(){userId});
}
public async Task RemoveUser(int pollId, ulong userId)