void HeuristicVote(IChatChannel channel, StreamCommand command)
        {
            Logger.Info(this, $"Executing heuristic vote for '{command}'");


            PollOption[] options = module.FindOptions(command.Arguments);

            string optionname = string.Join(" ", command.Arguments);

            if (options.Length > 1)
            {
                throw new StreamCommandException($"Sadly there is more than one poll which contains an option '{optionname}' so you need to specify in which poll you want to vote ({string.Join(", ", options.Select(o => o.Poll))}).", false);
            }

            if (options.Length == 0)
            {
                Poll poll = module.GetPoll(optionname);
                if (poll == null)
                {
                    throw new StreamCommandException($"There is no poll and no option named '{optionname}' so i have no idea what you want to vote for.", false);
                }

                options = module.GetOptions(poll.Name);
                throw new StreamCommandException($"You need to specify the option to vote for. The following options are available. {string.Join(", ", options.Select(o => $"'{o.Key}' for '{o.Description}'"))}", false);
            }

            module.ExecuteVote(options[0].Poll, command.User, options[0].Key);
            SendMessage(channel, command.User, $"You voted successfully for '{options[0].Description}' in poll '{options[0].Poll}'.");
        }
Beispiel #2
0
        public override void ExecuteCommand(IChatChannel channel, StreamCommand command)
        {
            PollVote[] votes;
            if (command.Arguments.Length == 0 || (command.Arguments.Length == 1 && command.Arguments[0] == "all"))
            {
                votes = module.GetUserVotes(command.User).ToArray();
                if (votes.Length == 0)
                {
                    throw new StreamCommandException("You haven't voted for anything, so revoking a vote doesn't make any sense.", false);
                }

                if (votes.Length > 1)
                {
                    if (command.Arguments.Length == 1 && command.Arguments[0] == "all")
                    {
                        foreach (PollVote vote in votes)
                        {
                            module.ExecuteRevoke(vote.Poll, command.User);
                        }
                        SendMessage(channel, command.User, $"You revoked your votes in polls '{string.Join(", ", votes.Select(v => v.Poll))}'");
                        return;
                    }
                    throw new StreamCommandException($"You have voted in more than one poll. Type !revoke all to remove all your votes. You voted in the following polls: {string.Join(", ", votes.Select(v => v.Poll))}");
                }

                module.ExecuteRevoke(votes[0].Poll, command.User);
                SendMessage(channel, command.User, $"You revoked your vote in poll '{votes[0].Poll}'");
                return;
            }

            string poll = command.Arguments[0].ToLower();

            if (module.RevokeVote(command.User, poll))
            {
                SendMessage(channel, command.User, $"You revoked your vote in poll '{poll}'");
                return;
            }

            PollOption[] options = module.FindOptions(command.Arguments);
            string[]     keys    = options.Select(o => o.Key).ToArray();
            votes = module.GetUserVotes(command.User, keys).ToArray();

            if (votes.Length == 0)
            {
                SendMessage(channel, command.User, "No votes match your arguments so no clue what you want to revoke.");
                return;
            }

            foreach (PollVote vote in votes)
            {
                module.ExecuteRevoke(vote.Poll, command.User);
            }
            SendMessage(channel, command.User, $"You revoked your votes in polls '{string.Join(", ", votes.Select(v => v.Poll))}'");
        }