示例#1
0
        public async Task <Result> Execute(SocketUserMessage e)
        {
            List <Object> results = await MessageControl.CreateQuestionnaire(e.Author.Id, e.Channel,
                                                                             new MessageControl.QE("Give poll name.", typeof(string)),
                                                                             new MessageControl.QE("Give poll end date.", typeof(DateTime)),
                                                                             new MessageControl.QE("Give poll max votes per person.", typeof(int)),
                                                                             new MessageControl.QE("Give poll option #", typeof(string [])));

            object [] obj     = results [3] as object [];
            string [] options = new string [obj.Length];
            for (int i = 0; i < obj.Length; i++)
            {
                options [i] = obj [i] as string;
            }

            try {
                MessageControl.Poll poll    = new MessageControl.Poll((string)results [0], e.Channel.Id, 0, DateTime.Parse(results [1].ToString()), (int)results [2], null, options);
                IMessage            message = await MessageControl.CreatePoll(poll);

                await poll.AwaitEnd();

                return(new Result(poll.winner, poll.winner.name));
            } catch (Exception exc) {
                Logging.DebugLog(Logging.LogType.EXCEPTION, exc.Message + " - " + exc.StackTrace);
                return(new Result(null, ""));
            }
        }
示例#2
0
        private async void CountVotes()
        {
            try {
                status = WeeklyEventStatus.Waiting;

                highestGame = null;
                int         highestVote       = int.MinValue;
                List <Game> highestVotedGames = new List <Game> ();

                foreach (Game game in games)
                {
                    if (game.votes == highestVote)
                    {
                        highestVotedGames.Add(game);
                    }
                    if (game.votes > highestVote)
                    {
                        highestVote       = game.votes;
                        highestVotedGames = new List <Game> ();
                        highestVotedGames.Add(game);
                    }
                }

                if (highestVotedGames.Count == 1)
                {
                    highestGame = highestVotedGames.FirstOrDefault();
                }
                else
                {
                    SocketChannel announcementChannel = Utility.SearchChannel(announcementsChannelName);
                    string        pollWinner          = string.Empty;

                    MessageControl.Poll poll = new MessageControl.Poll("Tiebreaker Vote!", announcementChannel.Id, 0, DateTime.Now.AddDays(1), 1, delegate(MessageControl.Poll p) {
                        pollWinner = p.winner.name;
                    },
                                                                       highestVotedGames.Select(x => x.name).ToArray()
                                                                       );
                    // What even is formatting.

                    MessageControl.CreatePoll(poll);
                    await poll.AwaitEnd();

                    highestGame = allGames.Find(x => x.name == pollWinner);
                }

                DateTime now      = DateTime.Now;
                DateTime eventDay = new DateTime(now.Year, now.Month, now.Day, eventHour, 0, 0).AddDays(daysBetween);
                DiscordEvents.CreateEvent(EVENT_NAME, eventDay, new TimeSpan(4, 0, 0), Program.discordClient.CurrentUser.Id, highestGame.iconUrl, highestGame.name + " has been chosen by vote!", new TimeSpan(0));

                SocketGuildChannel mainChannel = Utility.GetMainChannel();
                RestUserMessage    joinMessage = await Program.messageControl.AsyncSend(mainChannel as SocketTextChannel, onEventChosenByVoteMessage.Replace("{VOTEDGAME}", highestGame.name), true);

                joinMessageID = joinMessage.Id;
                joinMessage.AddReactionAsync(new Emoji("🗓"));

                Dictionary <ulong, bool> didWin = new Dictionary <ulong, bool> ();

                foreach (Vote vote in votes)
                {
                    if (!didWin.ContainsKey(vote.voterID))
                    {
                        didWin.Add(vote.voterID, false);
                    }

                    if (!didWin [vote.voterID])
                    {
                        if (highestGame.name == games [vote.votedGameID].name)
                        {
                            didWin [vote.voterID] = true;
                        }
                    }
                }

                int count = didWin.Count();
                for (int i = 0; i < count; i++)
                {
                    KeyValuePair <ulong, bool> pair = didWin.ElementAt(i);
                    if (pair.Value)
                    {
                        DiscordEvents.JoinEvent(pair.Key, EVENT_NAME);
                    }
                }

                await UpdateVoteMessage(false);

                SaveData();
            } catch (Exception e) {
                Logging.DebugLog(Logging.LogType.EXCEPTION, e.Message + " - " + e.StackTrace);
            }
        }