Exemple #1
0
        public async Task <IDiscordGuildUser> GetUserAsync(IDiscordGuild guild)
        {
            if (string.IsNullOrWhiteSpace(Argument))
            {
                return(null);
            }

            if (IsMention)
            {
                return(await guild.GetUserAsync(ulong.Parse(Argument
                                                            .TrimStart('<')
                                                            .TrimStart('@')
                                                            .TrimStart('!')
                                                            .TrimEnd('>'))));
            }
            else if (ulong.TryParse(Argument, out ulong id))
            {
                return(await guild.GetUserAsync(id));
            }
            return((await guild.GetUsersAsync())
                   .Where(x => x.Username.ToLower() == Argument.ToLower() ||
                          (x.Nickname?.ToLower() ?? "") == Argument.ToLower() ||
                          x.Username.ToLower() + "#" + x.Discriminator == Argument.ToLower())
                   .FirstOrDefault());
        }
Exemple #2
0
        private async Task UpdateGuildUserCountAsync(IDiscordGuild guild)
        {
            using (MikiContext context = new MikiContext())
            {
                GuildUser g = await GuildUser.GetAsync(context, guild);

                g.UserCount = (await guild.GetUsersAsync()).Count;
                await context.SaveChangesAsync();
            }
        }
Exemple #3
0
        private async Task Client_JoinedGuild(IDiscordGuild arg)
        {
            IDiscordChannel defaultChannel = await arg.GetDefaultChannelAsync();

            defaultChannel.QueueMessageAsync(Locale.GetString(defaultChannel.Id, "miki_join_message"));

            List <string> allArgs      = new List <string>();
            List <object> allParams    = new List <object>();
            List <object> allExpParams = new List <object>();

            try
            {
                var users = await arg.GetUsersAsync();

                for (int i = 0; i < users.Count; i++)
                {
                    allArgs.Add($"(@p{i * 2}, @p{i * 2 + 1})");

                    allParams.Add(users.ElementAt(i).Id.ToDbLong());
                    allParams.Add(users.ElementAt(i).Username);

                    allExpParams.Add((await users.ElementAt(i).GetGuildAsync()).Id.ToDbLong());
                    allExpParams.Add(users.ElementAt(i).Id.ToDbLong());
                }

                using (var context = new MikiContext())
                {
                    await context.Database.ExecuteSqlCommandAsync(
                        $"INSERT INTO dbo.\"Users\" (\"Id\", \"Name\") VALUES {string.Join(",", allArgs)} ON CONFLICT DO NOTHING", allParams);

                    await context.Database.ExecuteSqlCommandAsync(
                        $"INSERT INTO dbo.\"LocalExperience\" (\"ServerId\", \"UserId\") VALUES {string.Join(",", allArgs)} ON CONFLICT DO NOTHING", allExpParams);

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }

            DogStatsd.Increment("guilds.joined");
            //	DogStatsd.Set("guilds", Bot.Instance.Client.Guilds.Count, Bot.Instance.Client.Guilds.Count);
        }
Exemple #4
0
        public static async Task <GuildUser> CreateAsync(MikiContext context, IDiscordGuild guild)
        {
            long id        = guild.Id.ToDbLong();
            int  userCount = (await guild.GetUsersAsync()).Count;
            int  value     = await context.LocalExperience
                             .Where(x => x.ServerId == id)
                             .SumAsync(x => x.Experience);

            var guildUser = new GuildUser();

            guildUser.Name             = guild.Name;
            guildUser.Id               = id;
            guildUser.Experience       = value;
            guildUser.UserCount        = userCount;
            guildUser.LastRivalRenewed = Utils.MinDbValue;
            guildUser.MinimalExperienceToGetRewards = 100;
            GuildUser outputGuildUser = context.GuildUsers.Add(guildUser).Entity;
            await context.SaveChangesAsync();

            return(outputGuildUser);
        }
        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);
            }
        }