2017-12-10 03:24:57 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using dotbot.Services;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-12-11 21:15:47 +00:00
|
|
|
|
using System.Linq;
|
2017-12-10 03:24:57 +00:00
|
|
|
|
|
|
|
|
|
namespace dotbot.Commands
|
|
|
|
|
{
|
|
|
|
|
[Group("poll")]
|
|
|
|
|
public class Polls : ModuleBase<SocketCommandContext>
|
|
|
|
|
{
|
|
|
|
|
private Dictionary<ulong, Poll> _polls;
|
|
|
|
|
private IConfigurationRoot _config;
|
|
|
|
|
|
|
|
|
|
public Polls(PollService polls, IConfigurationRoot config)
|
|
|
|
|
{
|
|
|
|
|
_polls = polls.currentPolls;
|
|
|
|
|
_config = config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command]
|
|
|
|
|
[Priority(0)]
|
|
|
|
|
[Summary("create a new poll")]
|
2017-12-12 04:22:20 +00:00
|
|
|
|
public async Task CreatePoll([Remainder] [Summary("poll options")] string options = null)
|
2017-12-10 03:24:57 +00:00
|
|
|
|
{
|
|
|
|
|
var pollId = Context.Channel.Id;
|
|
|
|
|
if (_polls.ContainsKey(pollId))
|
|
|
|
|
{
|
2017-12-11 21:15:47 +00:00
|
|
|
|
await ReplyAsync($"respond with some more options or start the poll with `{_config["prefix"]}poll start`");
|
2017-12-10 03:24:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_polls[pollId] = new Poll
|
|
|
|
|
{
|
|
|
|
|
Owner = Context.User,
|
|
|
|
|
IsOpen = false,
|
2017-12-12 04:22:20 +00:00
|
|
|
|
Options = options == null ? new List<PollOption>() : options.Split(" ").Select(o => new PollOption{ Text = o }).ToList()
|
2017-12-10 03:24:57 +00:00
|
|
|
|
};
|
|
|
|
|
await ReplyAsync($"you started a new poll. respond with some options and then start the poll with `{_config["prefix"]}poll start`");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Command("start")]
|
|
|
|
|
[Priority(1)]
|
|
|
|
|
[Summary("starts a poll!")]
|
|
|
|
|
public async Task StartPoll()
|
|
|
|
|
{
|
|
|
|
|
var pollId = Context.Channel.Id;
|
2017-12-12 18:45:38 +00:00
|
|
|
|
if (_polls.ContainsKey(pollId) && Context.User == _polls[pollId].Owner && !_polls[pollId].IsOpen)
|
2017-12-10 03:24:57 +00:00
|
|
|
|
{
|
2017-12-12 18:45:38 +00:00
|
|
|
|
if (_polls[pollId].IsOpen)
|
2017-12-10 03:24:57 +00:00
|
|
|
|
{
|
2017-12-12 18:45:38 +00:00
|
|
|
|
await ReplyAsync("poll already started");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2017-12-10 03:24:57 +00:00
|
|
|
|
}
|
|
|
|
|
_polls[pollId].IsOpen = true;
|
|
|
|
|
}
|
|
|
|
|
else await ReplyAsync($"no poll ready to start");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-11 21:15:47 +00:00
|
|
|
|
|
|
|
|
|
[Command("stop")]
|
|
|
|
|
[Alias("finish", "resolve")]
|
|
|
|
|
[Priority(1)]
|
|
|
|
|
[Summary("gets winner of poll")]
|
|
|
|
|
public async Task StopPoll()
|
|
|
|
|
{
|
|
|
|
|
var pollId = Context.Channel.Id;
|
2017-12-12 18:45:38 +00:00
|
|
|
|
if (_polls.ContainsKey(pollId) && Context.User == _polls[pollId].Owner && _polls[pollId].IsOpen)
|
2017-12-11 21:15:47 +00:00
|
|
|
|
{
|
2017-12-12 18:45:38 +00:00
|
|
|
|
_polls[pollId].IsOpen = false;
|
|
|
|
|
await base.ReplyAsync($"the winner was **{_polls[pollId].Winner}**\nwith {_polls[pollId].Winner.Votes} votes");
|
|
|
|
|
_polls.Remove(pollId);
|
2017-12-11 21:15:47 +00:00
|
|
|
|
}
|
|
|
|
|
else await ReplyAsync("you haven't started any polls");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 03:24:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|