public int GetUserCurrency(Snowflake userId)
        {
            using var scope = RiasBot.CreateScope();
            var db = scope.ServiceProvider.GetRequiredService <RiasDbContext>();

            return(db.Users.FirstOrDefault(x => x.UserId == userId)?.Currency ?? 0);
        }
示例#2
0
        private async Task MemberUpdatedAsync(MemberUpdatedEventArgs args)
        {
            var oldMember = args.OldMember;
            var newMember = args.NewMember;

            // we care only about the mute role
            if (oldMember.Roles.Count == newMember.Roles.Count)
            {
                return;
            }

            using var scope = RiasBot.CreateScope();
            var db      = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var guildDb = await db.Guilds.FirstOrDefaultAsync(x => x.GuildId == newMember.Guild.Id);

            if (guildDb is null)
            {
                return;
            }

            var userGuildDb = await db.GuildUsers.FirstOrDefaultAsync(x => x.GuildId == newMember.Guild.Id && x.UserId == newMember.Id);

            if (userGuildDb is null)
            {
                return;
            }

            userGuildDb.IsMuted = newMember.Roles.FirstOrDefault(x => x.Key == guildDb.MuteRoleId).Value != null;
            await db.SaveChangesAsync();
        }
示例#3
0
        private async Task CheckVotesAsync()
        {
            using var scope = RiasBot.CreateScope();
            var db    = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var votes = await db.Votes.Where(x => !x.Checked).ToListAsync();

            foreach (var vote in votes)
            {
                var userDb = await db.GetOrAddAsync(x => x.UserId == vote.UserId, () => new UsersEntity { UserId = vote.UserId });

                if (userDb.IsBlacklisted)
                {
                    db.Remove(vote);
                    Log.Information($"Vote discord user with ID {vote.UserId} is blacklisted, it was removed from the votes database table");
                    continue;
                }

                var reward = vote.IsWeekend ? 50 : 25;
                userDb.Currency += reward;

                vote.Checked = true;
                Log.Information($"Vote discord user with ID {vote.UserId} was rewarded with {reward} hearts");
            }

            await db.SaveChangesAsync();
        }
示例#4
0
        private async Task PledgeReceivedAsync(string data)
        {
            try
            {
                var pledgeData = JsonConvert.DeserializeObject <JToken>(data);
                var userId     = pledgeData !.Value <ulong>("discord_id");

                using var scope = RiasBot.CreateScope();
                var db        = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
                var patreonDb = await db.Patreon.FirstOrDefaultAsync(x => x.UserId == userId);

                if (patreonDb is null)
                {
                    Log.Error("Couldn't take the patreon data from the database for user {UserId}", userId);
                    return;
                }

                var reward = pledgeData.Value <int>("reward") * 5;
                var userDb = await db.GetOrAddAsync(x => x.UserId == userId, () => new UserEntity { UserId = userId });

                userDb.Currency += reward;

                patreonDb.Checked = true;
                await db.SaveChangesAsync();

                Log.Information("Patreon discord user with ID {UserId} was rewarded with {Reward} hearts", userId, reward);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Exception on receiving a pledge");
            }
        }
示例#5
0
        /// <summary>
        /// Gets the user's currency.
        /// </summary>
        public async Task <int> GetUserCurrencyAsync(ulong userId)
        {
            using var scope = RiasBot.CreateScope();
            var db = scope.ServiceProvider.GetRequiredService <RiasDbContext>();

            return((await db.Users.FirstOrDefaultAsync(x => x.UserId == userId))?.Currency ?? 0);
        }
        public async Task <bool> CheckColorAsync(CachedUser user, Color color, int?tier = null)
        {
            if (Credentials.PatreonConfig is null)
            {
                return(true);
            }

            if (user.Id == Credentials.MasterId)
            {
                return(true);
            }

            if (_colors.Any(x => x == color))
            {
                return(true);
            }

            if (tier.HasValue)
            {
                return(tier.Value >= PatreonService.ProfileColorTier);
            }

            using var scope = RiasBot.CreateScope();
            var db = scope.ServiceProvider.GetRequiredService <RiasDbContext>();

            tier = (await db.Patreon.FirstOrDefaultAsync(x => x.UserId == user.Id))?.Tier ?? 0;
            return(tier >= PatreonService.ProfileColorTier);
        }
示例#7
0
        public async Task AddAssignableRoleAsync(CachedMember member)
        {
            var currentMember = member.Guild.CurrentMember;

            if (!currentMember.Permissions.ManageRoles)
            {
                return;
            }

            if (member.Roles.Count > 1)
            {
                return;
            }

            using var scope = RiasBot.CreateScope();
            var db      = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var guildDb = await db.Guilds.FirstOrDefaultAsync(x => x.GuildId == member.Guild.Id);

            if (guildDb is null)
            {
                return;
            }

            var aar = member.Guild.GetRole(guildDb.AutoAssignableRoleId);

            if (aar is null)
            {
                return;
            }

            if (currentMember.CheckRoleHierarchy(aar) > 0 && !aar.IsManaged && member.GetRole(aar.Id) is null)
            {
                await member.GrantRoleAsync(aar.Id);
            }
        }
        public async Task <int> RemoveUserCurrencyAsync(Snowflake userId, int currency)
        {
            using var scope = RiasBot.CreateScope();
            var db     = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var userDb = await db.Users.FirstOrDefaultAsync(x => x.UserId == userId);

            if (userDb == null)
            {
                return(0);
            }

            var currencyTaken = currency;

            if (currency > userDb.Currency)
            {
                currencyTaken   = userDb.Currency;
                userDb.Currency = 0;
            }
            else
            {
                userDb.Currency -= currency;
            }

            await db.SaveChangesAsync();

            return(currencyTaken);
        }
        public async Task AddUserCurrencyAsync(Snowflake userId, int currency)
        {
            using var scope = RiasBot.CreateScope();
            var db     = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var userDb = await db.GetOrAddAsync(x => x.UserId == userId, () => new UsersEntity { UserId = userId });

            userDb.Currency += currency;
            await db.SaveChangesAsync();
        }
示例#10
0
        private async Task MemberLeftAsync(MemberLeftEventArgs args)
        {
            if (RiasBot.CurrentUser != null && args.User.Id == RiasBot.CurrentUser.Id)
            {
                return;
            }

            using var scope = RiasBot.CreateScope();
            var db      = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var guildDb = await db.Guilds.FirstOrDefaultAsync(g => g.GuildId == args.Guild.Id);

            await SendByeMessageAsync(guildDb, args.User, args.Guild);
        }
示例#11
0
        private async Task DisableByeAsync(CachedGuild guild)
        {
            using var scope = RiasBot.CreateScope();
            var db      = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var guildDb = await db.Guilds.FirstOrDefaultAsync(x => x.GuildId == guild.Id);

            if (guildDb is null)
            {
                return;
            }

            guildDb.ByeNotification = false;
            await db.SaveChangesAsync();
        }
示例#12
0
        private async Task MemberJoinedAsync(MemberJoinedEventArgs args)
        {
            var member = args.Member;

            if (RiasBot.CurrentUser != null && member.Id == RiasBot.CurrentUser.Id)
            {
                return;
            }

            await RunTaskAsync(AddAssignableRoleAsync(member));

            using var scope = RiasBot.CreateScope();
            var db      = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var guildDb = await db.Guilds.FirstOrDefaultAsync(g => g.GuildId == member.Guild.Id);

            await SendGreetMessageAsync(guildDb, member);

            var currentUser = member.Guild.CurrentMember;

            if (!currentUser.Permissions.ManageRoles)
            {
                return;
            }

            var userGuildDb = await db.GuildUsers.FirstOrDefaultAsync(x => x.GuildId == member.Guild.Id && x.UserId == member.Id);

            if (userGuildDb is null)
            {
                return;
            }
            if (!userGuildDb.IsMuted)
            {
                return;
            }

            var role = member.Guild.GetRole(guildDb?.MuteRoleId ?? 0)
                       ?? member.Guild.Roles.FirstOrDefault(x => string.Equals(x.Value.Name, MuteService.MuteRole)).Value;

            if (role != null)
            {
                await member.GrantRoleAsync(role.Id);
            }
            else
            {
                userGuildDb.IsMuted = false;
                await db.SaveChangesAsync();
            }
        }
示例#13
0
        /// <summary>
        /// Remove currency from the user and returns the new currency.
        /// </summary>
        public async Task <int> RemoveUserCurrencyAsync(ulong userId, int currency)
        {
            using var scope = RiasBot.CreateScope();
            var db     = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var userDb = await db.Users.FirstOrDefaultAsync(x => x.UserId == userId);

            if (userDb == null)
            {
                return(0);
            }

            userDb.Currency -= Math.Min(currency, userDb.Currency);
            await db.SaveChangesAsync();

            return(userDb.Currency);
        }
示例#14
0
        private async Task CheckPatronsAsync()
        {
            using var scope = RiasBot.CreateScope();
            var db      = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var patrons = await db.Patreon.Where(x => x.PatronStatus == PatronStatus.ActivePatron && !x.Checked && x.Tier > 0)
                          .ToListAsync();

            foreach (var patron in patrons)
            {
                var reward = patron.AmountCents * 5;
                var userDb = await db.GetOrAddAsync(x => x.UserId == patron.UserId, () => new UserEntity { UserId = patron.UserId });

                userDb.Currency += reward;

                patron.Checked = true;
                Log.Information("Patreon discord user with ID {UserId} was rewarded with {Reward} hearts", patron.UserId, reward);
            }

            await db.SaveChangesAsync();
        }
示例#15
0
        private async Task SendPatronsAsync()
        {
            using var scope = RiasBot.CreateScope();
            var db      = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var patrons = (await db.GetOrderedListAsync <PatreonEntity, int>(
                               x => x.PatronStatus == PatronStatus.ActivePatron && x.Tier > 0,
                               x => x.Tier,
                               true))
                          .Where(x => RiasBot.Members.ContainsKey(x.UserId))
                          .Select(x =>
            {
                var user = RiasBot.Members[x.UserId];
                return(new PatreonDiscordUser
                {
                    PatreonId = x.PatreonUserId,
                    DiscordId = x.UserId,
                    PatreonUsername = x.PatreonUserName,
                    DiscordUsername = user.Username,
                    DiscordDiscriminator = user.Discriminator,
                    DiscordAvatar = user.GetAvatarUrl(ImageFormat.Auto),
                    Tier = x.Tier
                });
            });

            try
            {
                var data = JsonConvert.SerializeObject(patrons, Formatting.Indented);
#if RIAS_GLOBAL
                await _httpClient !.PostAsync("https://rias.gg/api/patreon", new StringContent(data));
#elif DEBUG
                await _httpClient !.PostAsync("https://localhost/api/patreon", new StringContent(data));
#endif
            }
            catch
            {
                // ignored
            }
        }
示例#16
0
        private async Task VoteReceivedAsync(string data)
        {
            var voteData = JsonConvert.DeserializeObject <Vote>(data);

            using var scope = RiasBot.CreateScope();
            var db     = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var voteDb = await db.Votes.FirstOrDefaultAsync(x => x.UserId == voteData.UserId && !x.Checked);

            if (voteDb is null)
            {
                Log.Error($"Couldn't take the vote data from the database for user {voteData.UserId}");
                return;
            }

            var reward = voteDb.IsWeekend ? 50 : 25;
            var userDb = await db.GetOrAddAsync(x => x.UserId == voteData.UserId, () => new UsersEntity { UserId = voteData.UserId });

            userDb.Currency += reward;

            voteDb.Checked = true;
            await db.SaveChangesAsync();

            Log.Information($"Vote discord user with ID {voteDb.UserId} was rewarded with {reward} hearts");
        }
示例#17
0
        private async Task PledgeReceivedAsync(string data)
        {
            var pledgeData = JsonConvert.DeserializeObject <PatreonPledge>(data);

            using var scope = RiasBot.CreateScope();
            var db        = scope.ServiceProvider.GetRequiredService <RiasDbContext>();
            var patreonDb = await db.Patreon.FirstOrDefaultAsync(x => x.UserId == pledgeData.DiscordId);

            if (patreonDb is null)
            {
                Log.Error($"Couldn't take the patreon data from the database for user {pledgeData.DiscordId}");
                return;
            }

            var reward = pledgeData.AmountCents * 5;
            var userDb = await db.GetOrAddAsync(x => x.UserId == pledgeData.DiscordId, () => new UsersEntity { UserId = pledgeData.DiscordId });

            userDb.Currency += reward;

            patreonDb.Checked = true;
            await db.SaveChangesAsync();

            Log.Information($"Patreon discord user with ID {patreonDb.UserId} was rewarded with {reward} hearts");
        }
示例#18
0
        public async Task <ICharacterEntity?> GetOrAddCharacterAsync(string name)
        {
            using var scope = RiasBot.CreateScope();
            var db = scope.ServiceProvider.GetRequiredService <RiasDbContext>();

            if (name.StartsWith("w", StringComparison.OrdinalIgnoreCase) && int.TryParse(name[1..], out var id))