dotbot/Services/PollService.cs

49 lines
1.4 KiB
C#
Raw Normal View History

2017-12-10 03:24:57 +00:00
using Discord;
using Discord.WebSocket;
2017-12-10 03:24:57 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2017-12-10 03:24:57 +00:00
namespace dotbot.Services
{
public class PollService
{
2017-12-19 03:07:45 +00:00
private DiscordSocketClient _discord;
2017-12-10 03:24:57 +00:00
public Dictionary<ulong, Poll> currentPolls;
public PollService(DiscordSocketClient discord)
2017-12-10 03:24:57 +00:00
{
currentPolls = new Dictionary<ulong, Poll>();
2017-12-19 03:07:45 +00:00
_discord = discord;
_discord.ReactionAdded += OnReactionAddedAsync;
}
private Task OnReactionAddedAsync(Cacheable<IUserMessage, ulong> CachedMessage, ISocketMessageChannel Channel, SocketReaction Reaction)
{
2017-12-19 03:07:45 +00:00
if (currentPolls.ContainsKey(Channel.Id) && Reaction.UserId != _discord.CurrentUser.Id)
2017-12-13 05:30:30 +00:00
currentPolls[Channel.Id].Options.First(o => o.MessageId == Reaction.MessageId).Votes++;
return Task.CompletedTask;
2017-12-10 03:24:57 +00:00
}
2017-12-10 03:24:57 +00:00
}
2017-12-10 03:24:57 +00:00
public class Poll
{
public List<PollOption> Options { get; set; }
public bool IsOpen { get; set; }
public IUser Owner { get; set; }
2017-12-12 15:57:34 +00:00
public PollOption Winner => Options.OrderBy(o => o.Votes).Last();
2017-12-10 03:24:57 +00:00
}
public class PollOption
{
public string Text { get; set; }
2017-12-12 18:45:38 +00:00
public ulong MessageId { get; set; }
2017-12-12 15:57:34 +00:00
public int Votes { get; set; }
2017-12-11 21:15:47 +00:00
public override string ToString() => Text;
2017-12-10 03:24:57 +00:00
}
}