actually check if poll is going on

This commit is contained in:
Ben Harris 2017-12-12 13:45:38 -05:00
parent a3896896a1
commit ab0f9ab821
2 changed files with 19 additions and 13 deletions

View File

@ -49,13 +49,20 @@ namespace dotbot.Commands
public async Task StartPoll() public async Task StartPoll()
{ {
var pollId = Context.Channel.Id; var pollId = Context.Channel.Id;
if (_polls.ContainsKey(pollId) && Context.User == _polls[pollId].Owner) if (_polls.ContainsKey(pollId) && Context.User == _polls[pollId].Owner && !_polls[pollId].IsOpen)
{ {
foreach (var o in _polls[pollId].Options) if (_polls[pollId].IsOpen)
{ {
o.Message = await ReplyAsync($"{o.Text}"); await ReplyAsync("poll already started");
await o.Message.AddReactionAsync(new Emoji("👍")); return;
await Task.Delay(100); }
foreach (var opt in _polls[pollId].Options)
{
var msg = await ReplyAsync($"{opt.Text}");
await msg.AddReactionAsync(new Emoji("👍"));
opt.MessageId = msg.Id;
await Task.Delay(300);
} }
_polls[pollId].IsOpen = true; _polls[pollId].IsOpen = true;
} }
@ -70,12 +77,11 @@ namespace dotbot.Commands
public async Task StopPoll() public async Task StopPoll()
{ {
var pollId = Context.Channel.Id; var pollId = Context.Channel.Id;
if (_polls.ContainsKey(pollId) && Context.User == _polls[pollId].Owner) if (_polls.ContainsKey(pollId) && Context.User == _polls[pollId].Owner && _polls[pollId].IsOpen)
{ {
var poll = _polls[pollId]; _polls[pollId].IsOpen = false;
poll.IsOpen = false; await base.ReplyAsync($"the winner was **{_polls[pollId].Winner}**\nwith {_polls[pollId].Winner.Votes} votes");
await ReplyAsync($"the winner was **{poll.Winner}**\nwith {poll.Winner.Votes} votes"); _polls.Remove(pollId);
poll = null;
} }
else await ReplyAsync("you haven't started any polls"); else await ReplyAsync("you haven't started any polls");
} }

View File

@ -22,9 +22,9 @@ namespace dotbot.Services
{ {
if (currentPolls.ContainsKey(Channel.Id)) if (currentPolls.ContainsKey(Channel.Id))
{ {
currentPolls[Channel.Id].Options.First(o => o.Message.Id == CachedMessage.Id).Votes++; currentPolls[Channel.Id].Options.First(o => o.MessageId == CachedMessage.Id).Votes++;
} }
Console.WriteLine($"{Reaction.Emote} reaction added to {CachedMessage.Id} in {Channel.Name}"); // Console.WriteLine($"{Reaction.Emote} reaction added to {CachedMessage.Id} in {Channel.Name}");
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -43,7 +43,7 @@ namespace dotbot.Services
public class PollOption public class PollOption
{ {
public string Text { get; set; } public string Text { get; set; }
public IUserMessage Message { get; set; } public ulong MessageId { get; set; }
public int Votes { get; set; } public int Votes { get; set; }
public override string ToString() => Text; public override string ToString() => Text;
} }