From 97b945fa2bb0a3d40f9ca1b7615bd2139bc0319f Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 11 Dec 2017 23:22:20 -0500 Subject: [PATCH] work on polls... can't get the react count right --- Commands/Polls.cs | 7 ++++--- Services/PollService.cs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Commands/Polls.cs b/Commands/Polls.cs index 8313895..2ae6020 100644 --- a/Commands/Polls.cs +++ b/Commands/Polls.cs @@ -23,7 +23,7 @@ namespace dotbot.Commands [Command] [Priority(0)] [Summary("create a new poll")] - public async Task CreatePoll([Summary("poll options")] string[] options = null) + public async Task CreatePoll([Remainder] [Summary("poll options")] string options = null) { var pollId = Context.Channel.Id; if (_polls.ContainsKey(pollId)) @@ -36,7 +36,7 @@ namespace dotbot.Commands { Owner = Context.User, IsOpen = false, - Options = options == null ? new List() : options.Select(o => new PollOption{ Text = o }).ToList() + Options = options == null ? new List() : options.Split(" ").Select(o => new PollOption{ Text = o }).ToList() }; await ReplyAsync($"you started a new poll. respond with some options and then start the poll with `{_config["prefix"]}poll start`"); } @@ -54,7 +54,8 @@ namespace dotbot.Commands foreach (var o in _polls[pollId].Options) { o.Message = await ReplyAsync($"{o.Text}"); - await o.Message.AddReactionAsync(Emote.Parse(":thumbsup:")); + await o.Message.AddReactionAsync(new Emoji("👍")); + await Task.Delay(100); } _polls[pollId].IsOpen = true; } diff --git a/Services/PollService.cs b/Services/PollService.cs index c78a4a1..411cca1 100644 --- a/Services/PollService.cs +++ b/Services/PollService.cs @@ -1,27 +1,52 @@ using Discord; +using Discord.WebSocket; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; +using System; namespace dotbot.Services { public class PollService { + private readonly DiscordSocketClient _discord; public Dictionary currentPolls; - public PollService() + public PollService(DiscordSocketClient discord) { currentPolls = new Dictionary(); + _discord = discord; + + _discord.ReactionAdded += OnReactionAddedAsync; } + + private Task OnReactionAddedAsync(Cacheable CachedMessage, ISocketMessageChannel Channel, SocketReaction Reaction) + { + if (currentPolls.ContainsKey(Channel.Id)) + { + currentPolls[Channel.Id].Options.First(o => o.Message.Id == CachedMessage.Id).Message = CachedMessage.Value; + } + Console.WriteLine(CachedMessage.Value.Reactions.Count); + + return Task.CompletedTask; + } + + //var msg = s as SocketUserMessage; + //if (msg == null) return; + //var context = new SocketCommandContext(_discord, msg); + //if (!currentPolls.ContainsKey(context.Channel.Id)) return; + + } + + public class Poll { public List Options { get; set; } public bool IsOpen { get; set; } public IUser Owner { get; set; } - - public PollOption Winner => Options.OrderBy(o => o.Votes).Last(); - + public PollOption Winner => Options.OrderBy(o => o.Votes).First(); } public class PollOption