work on polls... can't get the react count right

This commit is contained in:
Ben Harris 2017-12-11 23:22:20 -05:00
parent f802e69e23
commit 97b945fa2b
2 changed files with 33 additions and 7 deletions

View File

@ -23,7 +23,7 @@ namespace dotbot.Commands
[Command] [Command]
[Priority(0)] [Priority(0)]
[Summary("create a new poll")] [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; var pollId = Context.Channel.Id;
if (_polls.ContainsKey(pollId)) if (_polls.ContainsKey(pollId))
@ -36,7 +36,7 @@ namespace dotbot.Commands
{ {
Owner = Context.User, Owner = Context.User,
IsOpen = false, IsOpen = false,
Options = options == null ? new List<PollOption>() : options.Select(o => new PollOption{ Text = o }).ToList() Options = options == null ? new List<PollOption>() : 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`"); 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) foreach (var o in _polls[pollId].Options)
{ {
o.Message = await ReplyAsync($"{o.Text}"); 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; _polls[pollId].IsOpen = true;
} }

View File

@ -1,27 +1,52 @@
using Discord; using Discord;
using Discord.WebSocket;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System;
namespace dotbot.Services namespace dotbot.Services
{ {
public class PollService public class PollService
{ {
private readonly DiscordSocketClient _discord;
public Dictionary<ulong, Poll> currentPolls; public Dictionary<ulong, Poll> currentPolls;
public PollService() public PollService(DiscordSocketClient discord)
{ {
currentPolls = new Dictionary<ulong, Poll>(); currentPolls = new Dictionary<ulong, Poll>();
_discord = discord;
_discord.ReactionAdded += OnReactionAddedAsync;
} }
private Task OnReactionAddedAsync(Cacheable<IUserMessage, ulong> 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 class Poll
{ {
public List<PollOption> Options { get; set; } public List<PollOption> Options { get; set; }
public bool IsOpen { get; set; } public bool IsOpen { get; set; }
public IUser Owner { get; set; } public IUser Owner { get; set; }
public PollOption Winner => Options.OrderBy(o => o.Votes).First();
public PollOption Winner => Options.OrderBy(o => o.Votes).Last();
} }
public class PollOption public class PollOption