Ejemplo n.º 1
0
        public Task <ActionResult> PollAsync([Remainder] string all)
        {
            var content  = all.Split(';', StringSplitOptions.RemoveEmptyEntries);
            var pollInfo = PollHelpers.GetPollBody(content, EmojiService);

            if (!pollInfo.IsValid)
            {
                return(BadRequest((content.Length - 1) > 5
                    ? "More than 5 options were specified."
                    : "No options specified."));
            }

            var embed = Context.CreateEmbedBuilder()
                        .WithTitle(Format.Bold(content[0]));

            foreach (var(key, value) in pollInfo.Fields)
            {
                embed.AddField(key, value, true);
            }

            return(Ok(embed.WithFooter(pollInfo.Footer), async msg =>
            {
                _ = await Context.Message.TryDeleteAsync("Poll invocation message.");
                await PollHelpers.AddPollReactionsAsync(content.Length - 1, msg, EmojiService);
            }));
        }
Ejemplo n.º 2
0
        public Task <ActionResult> PollAsync([Remainder] string pollText)
        {
            var choices  = pollText.Split(';', StringSplitOptions.RemoveEmptyEntries);
            var pollInfo = PollHelpers.GetPollBody(choices, EmojiService);

            if (!pollInfo.IsValid)
            {
                return(BadRequest(choices.Length - 1 > 5
                    ? "More than 5 options were specified."
                    : "No options specified."));
            }

            var embed = Context.CreateEmbedBuilder()
                        .WithDescription(Format.Bold(choices[0]))
                        .WithTitle($"Poll by {Context.User}")
                        .WithThumbnailUrl("http://survation.com/wp-content/uploads/2016/09/polleverywherelogo.png");

            foreach (var(name, value) in pollInfo.Fields)
            {
                embed.AddField(name, value, true);
            }

            return(Ok(embed.WithFooter(pollInfo.Footer), async msg =>
            {
                _ = await Context.Message.TryDeleteAsync();
                await PollHelpers.AddPollReactionsAsync(choices, msg, EmojiService);
            }));
        }
Ejemplo n.º 3
0
        private async Task EndPoll()
        {
            if (!_pollService.Current.IsActive())
            {
                return;
            }

            var results    = _pollService.CalculateResults();
            var totalVotes = results.Count;

            var resultsText = "";

            if (results.Any())
            {
                for (var i = 0; i < results.Count; i++)
                {
                    var key           = results.ElementAt(i).Key;
                    var votes         = results.ElementAt(i).Value;
                    var percentageBar = PollHelpers.GeneratePercentageBar(votes, totalVotes);

                    resultsText += $"• {_pollService.Current.Choices[key]}: `{percentageBar}`\n";
                }
            }
            else
            {
                resultsText = "No votes were cast";
            }

            var embed = BasePollEmbed()
                        .WithTitle("Poll over!")
                        .WithDescription(_pollService.Current.Question)
                        .AddField("Vote Results", resultsText);

            await ReplyAsync("", false, embed.Build());

            _pollService.EndCurrent();
        }