Beispiel #1
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);
            }
        }
Beispiel #2
0
        private static async void OnReactionChanged(Cacheable <IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction, bool add)
        {
            if (reaction.User.Value.IsBot)
            {
                return;
            }
            IUserMessage messageValue = await channel.GetMessageAsync(message.Id) as IUserMessage;

            if (message.Id == votingMessageID)
            {
                int reactionID = -1;
                for (int i = 0; i < gamesPerWeek; i++)
                {
                    if (GetUnicodeEmoji(i) == reaction.Emote.Name)
                    {
                        reactionID = i;
                        break;
                    }
                }

                if (reactionID != -1)
                {
                    if (add)
                    {
                        if (!VoteForGame(reaction.User.Value.Id, reactionID))
                        {
                            await messageValue.RemoveReactionAsync(reaction.Emote, reaction.User.Value);
                        }
                    }
                    else
                    {
                        RemoveVote(reaction.UserId, reactionID);
                    }
                }
                else
                {
                    messageValue.RemoveReactionAsync(reaction.Emote, reaction.User.Value);
                }
            }

            if (message.Id == joinMessageID)
            {
                if (reaction.Emote.Name == "🗓")
                {
                    if (add)
                    {
                        if (DiscordEvents.JoinEvent(reaction.UserId, EVENT_NAME))
                        {
                            Program.messageControl.SendMessage(Utility.GetServer().GetUser(reaction.UserId), onEventJoinedDM);
                        }
                    }
                    else
                    {
                        if (DiscordEvents.LeaveEvent(reaction.UserId, EVENT_NAME))
                        {
                            Program.messageControl.SendMessage(Utility.GetServer().GetUser(reaction.UserId), onEventLeftDM);
                        }
                    }
                }
            }
        }