poll stuffs

This commit is contained in:
Ben Harris 2017-12-11 16:15:47 -05:00
parent 07c7ffbc21
commit f802e69e23
2 changed files with 27 additions and 6 deletions

View File

@ -4,6 +4,7 @@ using dotbot.Services;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Linq;
namespace dotbot.Commands namespace dotbot.Commands
{ {
@ -22,12 +23,12 @@ namespace dotbot.Commands
[Command] [Command]
[Priority(0)] [Priority(0)]
[Summary("create a new poll")] [Summary("create a new poll")]
public async Task CreatePoll() public async Task CreatePoll([Summary("poll options")] string[] options = null)
{ {
var pollId = Context.Channel.Id; var pollId = Context.Channel.Id;
if (_polls.ContainsKey(pollId)) if (_polls.ContainsKey(pollId))
{ {
await ReplyAsync($"add some more options or start the poll with `{_config["prefix"]}poll start`"); await ReplyAsync($"respond with some more options or start the poll with `{_config["prefix"]}poll start`");
} }
else else
{ {
@ -35,6 +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()
}; };
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`");
} }
@ -51,13 +53,30 @@ namespace dotbot.Commands
{ {
foreach (var o in _polls[pollId].Options) foreach (var o in _polls[pollId].Options)
{ {
var opt = await ReplyAsync($"{o.Text}"); o.Message = await ReplyAsync($"{o.Text}");
await opt.AddReactionAsync(Emote.Parse(":thumbsup:")); await o.Message.AddReactionAsync(Emote.Parse(":thumbsup:"));
} }
_polls[pollId].IsOpen = true; _polls[pollId].IsOpen = true;
} }
else await ReplyAsync($"no poll ready to start"); else await ReplyAsync($"no poll ready to start");
} }
[Command("stop")]
[Alias("finish", "resolve")]
[Priority(1)]
[Summary("gets winner of poll")]
public async Task StopPoll()
{
var pollId = Context.Channel.Id;
if (_polls.ContainsKey(pollId) && Context.User == _polls[pollId].Owner)
{
var poll = _polls[pollId];
poll.IsOpen = false;
await ReplyAsync($"the winner was **{poll.Winner}**\nwith {poll.Winner.Votes} votes");
}
else await ReplyAsync("you haven't started any polls");
}
} }
} }

View File

@ -20,13 +20,15 @@ namespace dotbot.Services
public bool IsOpen { get; set; } public bool IsOpen { get; set; }
public IUser Owner { get; set; } public IUser Owner { get; set; }
public PollOption GetWinner => Options.OrderBy(o => o.Votes).Last(); public PollOption Winner => Options.OrderBy(o => o.Votes).Last();
} }
public class PollOption public class PollOption
{ {
public string Text { get; set; } public string Text { get; set; }
public int Votes { get; set; } public IUserMessage Message { get; set; }
public int Votes => Message.Reactions.Count;
public override string ToString() => Text;
} }
} }