Example #1
0
        public Task StopAsync(CommandContext ctx)
        {
            var poll = PollService.GetPollInChannel(ctx.Channel.Id);

            if (poll is null || poll is ReactionsPoll)
            {
                throw new CommandFailedException("There are no text polls running in this channel.");
            }

            if (!ctx.Member.PermissionsIn(ctx.Channel).HasPermission(Permissions.Administrator) && ctx.User.Id != poll.Initiator.Id)
            {
                throw new CommandFailedException("You do not have the sufficient permissions to close another person's poll!");
            }

            poll.Stop();

            return(Task.CompletedTask);
        }
Example #2
0
        public Task CancelAsync(CommandContext ctx)
        {
            var poll = PollService.GetPollInChannel(ctx.Channel.Id);

            if (poll is null || !poll.IsRunning || poll is ReactionsPoll)
            {
                throw new CommandFailedException("There are no polls running in this channel.");
            }

            if (!poll.UserVoted(ctx.User.Id))
            {
                throw new CommandFailedException("You have not voted in this poll!");
            }

            if (!poll.CancelVote(ctx.User.Id))
            {
                throw new CommandFailedException("Failed to cancel your vote!");
            }

            return(this.InformAsync(ctx, "Your vote has been cancelled!", important: false));
        }
Example #3
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, ctx.Member, question);

            PollService.RegisterPollInChannel(rpoll, ctx.Channel.Id);
            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);
            }
        }
Example #4
0
        public async Task ExecuteGroupAsync(CommandContext ctx, [Description("Option to vote for.")] int option)
        {
            var poll = PollService.GetPollInChannel(ctx.Channel.Id);

            if (poll is null || !poll.IsRunning || poll is ReactionsPoll)
            {
                throw new CommandFailedException("There are no polls running in this channel.");
            }

            option--;
            if (!poll.IsValidVote(option))
            {
                throw new CommandFailedException($"Invalid poll option. Valid range: [1, {poll.Options.Count}].");
            }

            if (poll.UserVoted(ctx.User.Id))
            {
                throw new CommandFailedException("You have already voted in this poll!");
            }

            poll.VoteFor(ctx.User.Id, option);

            await this.InformAsync(ctx, $"{ctx.User.Mention} voted for: {Formatter.Bold(poll.OptionWithId(option))} in poll: {Formatter.Italic($"\"{poll.Question}\"")}", important : false);
        }