geekbot/Geekbot.net/Commands/Utils/Poll.cs

200 lines
7 KiB
C#
Raw Normal View History

2017-11-06 23:55:28 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2017-11-06 23:55:28 +01:00
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Database;
using Geekbot.net.Database.Models;
using Geekbot.net.Lib.Converters;
using Geekbot.net.Lib.ErrorHandling;
2018-05-17 22:06:58 +02:00
using Geekbot.net.Lib.Extensions;
using Geekbot.net.Lib.UserRepository;
2017-11-06 23:55:28 +01:00
namespace Geekbot.net.Commands.Utils
2017-11-06 23:55:28 +01:00
{
[Group("poll")]
public class Poll : ModuleBase
{
2017-12-29 01:53:50 +01:00
private readonly IEmojiConverter _emojiConverter;
2017-11-06 23:55:28 +01:00
private readonly IErrorHandler _errorHandler;
private readonly DatabaseContext _database;
2017-11-06 23:55:28 +01:00
private readonly IUserRepository _userRepository;
public Poll(IErrorHandler errorHandler, DatabaseContext database, IEmojiConverter emojiConverter, IUserRepository userRepository)
2017-11-06 23:55:28 +01:00
{
_errorHandler = errorHandler;
_database = database;
2017-11-06 23:55:28 +01:00
_emojiConverter = emojiConverter;
_userRepository = userRepository;
}
[Command(RunMode = RunMode.Async)]
[Summary("Check status of the current poll")]
public async Task Dflt()
{
try
{
var currentPoll = GetCurrentPoll();
if (currentPoll.Question == null)
2017-11-06 23:55:28 +01:00
{
await ReplyAsync(
"There is no poll in this channel ongoing at the moment\r\nYou can create one with `!poll create question;option1;option2;option3`");
return;
}
2017-12-29 01:53:50 +01:00
await ReplyAsync("There is a poll running at the moment");
2017-11-06 23:55:28 +01:00
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-11-06 23:55:28 +01:00
}
}
2017-12-29 01:53:50 +01:00
2017-11-06 23:55:28 +01:00
[Command("create", RunMode = RunMode.Async)]
[Summary("Create a poll")]
2017-12-29 01:53:50 +01:00
public async Task Create([Remainder] [Summary("question;option1;option2")]
string rawPollString)
2017-11-06 23:55:28 +01:00
{
try
{
await ReplyAsync("Poll creation currently disabled");
return;
2019-03-04 02:39:31 +01:00
// var currentPoll = GetCurrentPoll();
// if (currentPoll.Question != null && !currentPoll.IsFinshed)
// {
// await ReplyAsync("You have not finished you last poll yet. To finish it use `!poll end`");
// return;
// }
//
// var pollList = rawPollString.Split(';').ToList();
// if (pollList.Count <= 2)
// {
// await ReplyAsync(
// "You need a question with atleast 2 options, a valid creation would look like this `question;option1;option2`");
// return;
// }
//
// var question = pollList[0];
// pollList.RemoveAt(0);
//
// var eb = new EmbedBuilder
// {
// Title = $"Poll by {Context.User.Username}",
// Description = question
// };
//
// var options = new List<PollQuestionModel>();
// var i = 1;
// pollList.ForEach(option =>
// {
// options.Add(new PollQuestionModel()
// {
// OptionId = i,
// OptionText = option
// });
// eb.AddInlineField($"Option {_emojiConverter.NumberToEmoji(i)}", option);
// i++;
// });
// var pollMessage = await ReplyAsync("", false, eb.Build());
//
// var poll = new PollModel()
// {
// Creator = Context.User.Id.AsLong(),
// MessageId = pollMessage.Id.AsLong(),
// IsFinshed = false,
// Question = question,
// Options = options
// };
// _database.Polls.Add(poll);
//
// i = 1;
// pollList.ForEach(option =>
// {
// pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i)));
// Task.Delay(500);
// i++;
// });
2017-11-06 23:55:28 +01:00
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-11-06 23:55:28 +01:00
}
}
2017-12-29 01:53:50 +01:00
2017-11-06 23:55:28 +01:00
[Command("end", RunMode = RunMode.Async)]
[Summary("End the current poll")]
public async Task End()
{
try
{
var currentPoll = GetCurrentPoll();
if (currentPoll.Question == null || currentPoll.IsFinshed)
2017-11-06 23:55:28 +01:00
{
await ReplyAsync("There is no ongoing poll at the moment");
2017-11-06 23:55:28 +01:00
return;
}
2017-12-29 01:53:50 +01:00
currentPoll = await GetPollResults(currentPoll);
var sb = new StringBuilder();
sb.AppendLine("**Poll Results**");
sb.AppendLine(currentPoll.Question);
foreach (var result in currentPoll.Options) sb.AppendLine($"{result.Votes} - {result.OptionText}");
await ReplyAsync(sb.ToString());
currentPoll.IsFinshed = true;
_database.Polls.Update(currentPoll);
2017-11-06 23:55:28 +01:00
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-11-06 23:55:28 +01:00
}
}
private PollModel GetCurrentPoll()
2017-11-06 23:55:28 +01:00
{
try
{
var currentPoll = _database.Polls.FirstOrDefault(poll =>
poll.ChannelId.Equals(Context.Channel.Id.AsLong()) &&
poll.GuildId.Equals(Context.Guild.Id.AsLong())
);
return currentPoll ?? new PollModel();
2017-11-06 23:55:28 +01:00
}
2017-12-29 01:53:50 +01:00
catch
2017-11-06 23:55:28 +01:00
{
return new PollModel();
2017-11-06 23:55:28 +01:00
}
}
private async Task<PollModel> GetPollResults(PollModel poll)
2017-11-06 23:55:28 +01:00
{
var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId.AsUlong());
var results = new Dictionary<int, int>();
2017-11-06 23:55:28 +01:00
foreach (var r in message.Reactions)
{
try
{
results.Add(r.Key.Name.ToCharArray()[0], r.Value.ReactionCount);
}
catch
{
// ignored
}
}
foreach (var q in poll.Options)
{
q.Votes = results.FirstOrDefault(e => e.Key.Equals(q.OptionId)).Value;
}
return poll;
2017-12-29 01:53:50 +01:00
// var sortedValues = results.OrderBy(e => e.Value);
// return sortedValues;
2017-11-06 23:55:28 +01:00
}
}
}