dotbot/Services/PollService.cs

52 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;
using System;
2017-12-10 03:24:57 +00:00
namespace dotbot.Services
{
public class PollService
{
2017-12-12 15:57:34 +00:00
private readonly DiscordSocketClient ;
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-12 15:57:34 +00:00
discord.ReactionAdded += OnReactionAddedAsync;
}
private Task OnReactionAddedAsync(Cacheable<IUserMessage, ulong> CachedMessage, ISocketMessageChannel Channel, SocketReaction Reaction)
{
if (currentPolls.ContainsKey(Channel.Id))
{
2017-12-12 15:57:34 +00:00
currentPolls[Channel.Id].Options.First(o => o.Message.Id == CachedMessage.Id).Votes++;
}
2017-12-12 15:57:34 +00:00
Console.WriteLine($"{Reaction.Emote} reaction added to {CachedMessage.Id} in {Channel.Name}");
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-11 21:15:47 +00:00
public IUserMessage Message { 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
}
}