Ejemplo n.º 1
0
        private void RemovePollFromId(PollIdentifier Poll)
        {
            if (PollMessageIds.ContainsKey(Poll.MessageId))
            {
                List <PollIdentifier> polls = new List <PollIdentifier>();
                polls.RemoveAll(t => t.Identical(Poll));

                if (polls.Count == 0)
                {
                    PollMessageIds.Remove(Poll.MessageId);
                }
                else
                {
                    PollMessageIds[Poll.MessageId] = polls;
                }
            }
        }
Ejemplo n.º 2
0
        public PollManager(DiscordSocketClient Client)
        {
            Polls                 = new Dictionary <ulong, List <Poll> >();
            PollMessageIds        = new Dictionary <ulong, List <PollIdentifier> >();
            Client.ReactionAdded += async(Cachable, Channel, Reaction) =>
            {
                if (!(await Channel.GetOrDownloadAsync() is IGuildChannel GuildChannel))
                {
                    return;
                }
                IUserMessage message = await Cachable.GetOrDownloadAsync();

                if (PollMessageIds.ContainsKey(message.Id))
                {
                    PollIdentifier id = new PollIdentifier()
                    {
                        MessageId = message.Id,
                        ChannelId = GuildChannel.Id,
                        GuildId   = GuildChannel.GuildId
                    };

                    id = PollMessageIds[message.Id].Find(t => t.Identical(id));

                    if (id != null)
                    {
                        int Index = Polls[id.PollCreator].FindIndex(t => t.Identical(id));
                        if (Index > -1)
                        {
                            Poll poll = Polls[id.PollCreator][Index];
                            bool save = false;
                            for (int i = 0; i < poll.ReactionEmotes.Length; i++)
                            {
                                if (poll.ReactionEmotes[i] == Reaction.Emote.Name)
                                {
                                    Polls[id.PollCreator][Index].Results[i]++;
                                    save = true;
                                }
                            }
                            if (save)
                            {
                                SavePolls();
                            }
                        }
                    }
                }
            };
            Client.ReactionRemoved += async(Cachable, Channel, Reaction) =>
            {
                if (!(await Channel.GetOrDownloadAsync() is IGuildChannel GuildChannel))
                {
                    return;
                }
                IUserMessage message = await Cachable.GetOrDownloadAsync();

                if (PollMessageIds.ContainsKey(message.Id))
                {
                    PollIdentifier id = new PollIdentifier()
                    {
                        MessageId = message.Id,
                        ChannelId = GuildChannel.Id,
                        GuildId   = GuildChannel.GuildId
                    };

                    id = PollMessageIds[message.Id].Find(t => t.Identical(id));

                    if (id != null)
                    {
                        int Index = Polls[id.PollCreator].FindIndex(t => t.Identical(id));
                        if (Index > -1)
                        {
                            Poll poll = Polls[id.PollCreator][Index];
                            bool save = false;
                            for (int i = 0; i < poll.ReactionEmotes.Length; i++)
                            {
                                if (poll.ReactionEmotes[i] == Reaction.Emote.Name)
                                {
                                    Polls[id.PollCreator][Index].Results[i]--;
                                    save = true;
                                }
                            }
                            if (save)
                            {
                                SavePolls();
                            }
                        }
                    }
                }
            };

            Client.MessageDeleted += async(Cachable, Channel) =>
            {
                if (!(await Channel.GetOrDownloadAsync() is IGuildChannel guildChannel))
                {
                    return;
                }

                if (PollMessageIds.ContainsKey(Cachable.Id))
                {
                    List <PollIdentifier> ids = PollMessageIds[Cachable.Id];
                    int            index      = 0;
                    PollIdentifier id         = ids[index];
                    if (ids.Count != 1)
                    {
                        for (int i = 0; i < ids.Count; i++)
                        {
                            if (ids[i].ChannelId == Channel.Id)
                            {
                                index = i;
                                id    = ids[index];
                                break;
                            }
                        }
                    }
                    CancelPoll(id.PollCreator, id.PollId);

                    IUser usr = Global.Client.GetUser(id.PollCreator);
                    if (usr == null)
                    {
                        foreach (IGuild guild in Global.Client.Guilds)
                        {
                            IUser u = guild.GetUserAsync(id.PollCreator).Result;
                            if (u != null)
                            {
                                usr = u;
                                break;
                            }
                        }
                    }

                    IDMChannel ch = await usr.CreateDMChannelAsync();

                    await ch.SendMessageAsync($"The message of the poll you made with id \"{ id.PollId }\" has been removed from #{ guildChannel.Name } on guild \"{ guildChannel.Guild.Name }\"!");
                }
            };
            Client.LeftGuild += async(Guild) =>
            {
                foreach (KeyValuePair <ulong, List <PollIdentifier> > poll in PollMessageIds)
                {
                    for (int i = 0; i < poll.Value.Count; i++)
                    {
                        PollIdentifier id = poll.Value[i];
                        if (id.GuildId == Guild.Id)
                        {
                            CancelPoll(id.PollCreator, id.PollId);

                            IUser usr = Global.Client.GetUser(id.PollCreator);
                            if (usr == null)
                            {
                                foreach (IGuild guild in Global.Client.Guilds)
                                {
                                    IUser u = guild.GetUserAsync(id.PollCreator).Result;
                                    if (u != null)
                                    {
                                        usr = u;
                                        break;
                                    }
                                }
                            }

                            IDMChannel ch = await usr.CreateDMChannelAsync();

                            await ch.SendMessageAsync($"Chino-chan has been kicked or banned form { Guild.Name } so your poll with id { id.PollId } has been canceled!");
                        }
                    }
                }
            };
            Client.ChannelDestroyed += async(Channel) =>
            {
                if (!(Channel is IGuildChannel guildChannel))
                {
                    return;
                }

                foreach (KeyValuePair <ulong, List <PollIdentifier> > poll in PollMessageIds)
                {
                    for (int i = 0; i < poll.Value.Count; i++)
                    {
                        PollIdentifier id = poll.Value[i];
                        if (id.GuildId == Channel.Id)
                        {
                            CancelPoll(id.PollCreator, id.PollId);

                            IUser usr = Global.Client.GetUser(id.PollCreator);
                            if (usr == null)
                            {
                                foreach (IGuild guild in Global.Client.Guilds)
                                {
                                    IUser u = guild.GetUserAsync(id.PollCreator).Result;
                                    if (u != null)
                                    {
                                        usr = u;
                                        break;
                                    }
                                }
                            }

                            IDMChannel ch = await usr.CreateDMChannelAsync();

                            await ch.SendMessageAsync($"{ guildChannel.Name } channel has been removed from { guildChannel.Guild.Name } so the poll with id ");
                        }
                    }
                }
            };
            Logger.Log(LogType.Poll, ConsoleColor.Green, "Poll", "Manager ready!");
        }