Beispiel #1
0
        public static DiscordEmbed ToEmbed(this ReactionsPoll poll, LocalizationService lcs)
        {
            var emb = new LocalizedEmbedBuilder(lcs, poll.Channel.GuildId);

            emb.WithTitle(poll.Question);
            emb.WithLocalizedDescription("str-vote-react");
            emb.WithColor(DiscordColor.Orange);

            for (int i = 0; i < poll.Options.Count; i++)
            {
                if (!string.IsNullOrWhiteSpace(poll.Options[i]))
                {
                    emb.AddField($"{i + 1}", poll.Options[i], inline: true);
                }
            }

            if (poll.EndTime is { })
Beispiel #2
0
        public async Task ReactionsPollAsync(CommandContext ctx,
                                             [Description("Time for poll to run.")] TimeSpan timeout,
                                             [RemainingText, Description("Question.")] string question)
        {
            if (string.IsNullOrWhiteSpace(question))
            {
                throw new InvalidCommandUsageException("Poll requires a question.");
            }

            if (PollService.IsPollRunningInChannel(ctx.Channel.Id))
            {
                throw new CommandFailedException("Another poll is already running in this channel.");
            }

            if (timeout < TimeSpan.FromSeconds(10) || timeout >= TimeSpan.FromDays(1))
            {
                throw new InvalidCommandUsageException("Poll cannot run for less than 10 seconds or more than 1 day(s).");
            }

            var rpoll = new ReactionsPoll(ctx.Client.GetInteractivity(), ctx.Channel, question);

            if (!PollService.RegisterPollInChannel(rpoll, ctx.Channel.Id))
            {
                throw new ConcurrentOperationException("Failed to start the poll. Please try again.");
            }

            try {
                await this.InformAsync(ctx, StaticDiscordEmoji.Question, "And what will be the possible answers? (separate with a semicolon)");

                var options = await ctx.WaitAndParsePollOptionsAsync();

                if (options.Count < 2 || options.Count > 10)
                {
                    throw new CommandFailedException("Poll must have minimum 2 and maximum 10 options!");
                }
                rpoll.Options = options;

                await rpoll.RunAsync(timeout);
            } finally {
                PollService.UnregisterPollInChannel(ctx.Channel.Id);
            }
        }
        public async Task ExecuteGroupAsync(CommandContext ctx,
                                            [Description("desc-poll-t")] TimeSpan timeout,
                                            [RemainingText, Description("desc-poll-q")] string question)
        {
            if (string.IsNullOrWhiteSpace(question))
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-poll-q-none");
            }

            if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-poll-dup");
            }

            if (timeout < TimeSpan.FromSeconds(10) || timeout >= TimeSpan.FromDays(1))
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-poll-time", Poll.MinTimeSeconds, Poll.MaxTimeDays);
            }

            var rpoll = new ReactionsPoll(ctx.Client.GetInteractivity(), ctx.Channel, ctx.Member, question, timeout);

            this.Service.RegisterEventInChannel(rpoll, ctx.Channel.Id);
            try {
                await ctx.InfoAsync(this.ModuleColor, Emojis.Question, "q-poll-ans");

                List <string>?options = await ctx.WaitAndParsePollOptionsAsync();

                if (options is null || options.Count < 2 || options.Count > Poll.MaxPollOptions)
                {
                    throw new CommandFailedException(ctx, "cmd-err-poll-opt", Poll.MaxPollOptions);
                }
                rpoll.Options = options;

                await rpoll.RunAsync(this.Localization);
            } catch (TaskCanceledException) {
                await ctx.FailAsync("cmd-err-poll-cancel");
            } finally {
                this.Service.UnregisterEventInChannel(ctx.Channel.Id);
            }
        }