Exemplo n.º 1
0
        public async Task <List <EventMessageObject> > GetMessage(IDiscordGuild guild, EventMessageType type, IDiscordUser user)
        {
            long guildId = guild.Id.ToDbLong();

            var channels = await guild.GetTextChannelsAsync();

            var channelIds = channels.Select(x => x.Id.ToDbLong());

            var guildCount = (await guild.GetUsersAsync()).Count;

            IDiscordGuildUser owner = await guild.GetOwnerAsync();

            var ownerMention = owner.Mention;
            var ownerName    = owner.Username;

            List <EventMessageObject> output = new List <EventMessageObject>();

            using (var context = new MikiContext())
            {
                var messageObjects = await context.EventMessages
                                     .Where(x => channelIds.Contains(x.ChannelId) && (short)type == x.EventType)
                                     .ToListAsync();

                var allUsers = await guild.GetUsersAsync();

                foreach (var c in messageObjects)
                {
                    if (c == null)
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(c.Message))
                    {
                        continue;
                    }

                    IDiscordGuild g = await(user as IDiscordGuildUser).GetGuildAsync();


                    string modifiedMessage = c.Message;

                    modifiedMessage = modifiedMessage.Replace("-um", user.Mention);
                    modifiedMessage = modifiedMessage.Replace("-uc",
                                                              (await g.GetUsersAsync()).Count().ToString());
                    modifiedMessage = modifiedMessage.Replace("-u", user.Username);

                    modifiedMessage = modifiedMessage.Replace("-ru", allUsers.ElementAt(MikiRandom.Next(0, allUsers.Count())).Username);

                    modifiedMessage = modifiedMessage.Replace("-now", DateTime.Now.ToShortDateString());
                    modifiedMessage = modifiedMessage.Replace("-sc", guildCount.ToString());
                    modifiedMessage = modifiedMessage.Replace("-s",
                                                              g.Name);


                    modifiedMessage = modifiedMessage.Replace("-om", ownerMention);
                    modifiedMessage = modifiedMessage.Replace("-o", ownerName);

                    modifiedMessage = modifiedMessage.Replace("-cc", (await g.GetChannelsAsync()).Count.ToString());
                    modifiedMessage = modifiedMessage.Replace("-vc", (await g.GetVoiceChannelsAsync()).Count().ToString());

                    output.Add(new EventMessageObject()
                    {
                        message            = modifiedMessage,
                        destinationChannel = channels.FirstOrDefault(x => x.Id.ToDbLong() == c.ChannelId)
                    });
                }
                return(output);
            }
        }