Exemplo n.º 1
0
        public UserProfile SetAdditionalSettingsForProfile(UserProfile profile, int loginUserId)
        {
            UserFriend friend = _context.UserFriends.FirstOrDefault(x =>
                                                                    (x.IdUser == profile.UserId && x.IdUserToFriendList == loginUserId) ||
                                                                    (x.IdUser == loginUserId && x.IdUserToFriendList == profile.UserId));
            UserBlacklist blackList = _context.UserBlacklists.FirstOrDefault(x =>
                                                                             (x.IdUser == profile.UserId && x.IdUserToBlackList == loginUserId) ||
                                                                             (x.IdUser == loginUserId && x.IdUserToBlackList == profile.UserId));

            if (friend != null)
            {
                if (friend.RequestAccepted)
                {
                    profile.AreFriends = true;
                }
                else
                {
                    profile.FriendRequestSend = true;
                }
            }

            if (blackList != null)
            {
                profile.IsBlocked = false;
            }

            if (_context.UsersFollowings.Any(x => x.IdUser == loginUserId && x.IdUserToFollow == profile.UserId))
            {
                profile.IsFollowing = true;
            }

            return(profile);
        }
Exemplo n.º 2
0
            public async Task <int> Handle(CreateCommand request, CancellationToken cancellationToken)
            {
                var entity = new UserBlacklist
                {
                    OwnerUserId = request.OwnerUserId, BlockedUserId = request.BlockedUserId
                };

                await _context.UserBlacklists.AddAsync(entity, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.UserBlacklistId);
            }
Exemplo n.º 3
0
        public static bool IsValid(string value, Type type)
        {
            switch (type)
            {
            case Type.Organization:
                return(!OrganizationBlacklist.Contains(value));

            case Type.Repository:
                return(!RepositoryBlacklist.Contains(value));

            case Type.User:
                return(!UserBlacklist.Contains(value));
            }

            return(false);
        }
Exemplo n.º 4
0
        public async Task <UserBlacklist> Remove(long userId, long guildId)
        {
            using (var context = _services.GetService <JakeBotContext>())
            {
                var blacklist = new UserBlacklist()
                {
                    GuildId = guildId, UserId = userId
                };
                if (await IsBlackListed(userId, guildId))
                {
                    context.UserBlacklists.Remove(blacklist);
                    return(await context.SaveChangesAsync() > 0 ? blacklist : null);
                }

                return(blacklist);
            }
        }
Exemplo n.º 5
0
        public static void Add(string value, Type type)
        {
            switch (type)
            {
            case Type.Organization:
                OrganizationBlacklist.Add(value);
                IsOrganizationChanged = true;
                break;

            case Type.Repository:
                RepositoryBlacklist.Add(value);
                IsRepositoryChanged = true;
                break;

            case Type.User:
                UserBlacklist.Add(value);
                IsUserChanged = true;
                break;
            }

            Save();
        }
Exemplo n.º 6
0
        public async Task <UserBlacklist> Add(long userId, long guildId)
        {
            using (var context = _services.GetService <JakeBotContext>())
            {
                var blacklist = new UserBlacklist()
                {
                    GuildId = guildId, UserId = userId
                };
                if (await IsBlackListed(userId, guildId))
                {
                    return(blacklist);
                }

                blacklist = new UserBlacklist()
                {
                    GuildId = guildId, UserId = userId
                };
                context.Add(blacklist);
                var changes = await context.SaveChangesAsync();

                return(changes > 0 ? blacklist : null);
            }
        }
Exemplo n.º 7
0
        public async Task Command(ulong id, string duration, [Remainder] string reason = "No reason provided.")
        {
            User target = await DatabaseQueries.GetOrCreateUserAsync(id);

            SocketUser socketTarget = Client.GetUser(target.UserId);

            double expiration          = (DateTime.Now + duration.ParseToTimespan()).ToOADate();
            string humanizedExpiration = duration.ParseToTimespan().Humanize(3);

            if (target.IsBlacklisted)
            {
                await SendBasicErrorEmbedAsync($"This user is already blacklisted.\n\n" +
                                               $"Reason: `{target.Blacklist.Reason}`\n" +
                                               $"Expires `{DateTime.FromOADate(target.Blacklist.Expiration).Humanize()}`");

                return;
            }

            var blacklist = new UserBlacklist
            {
                UserId     = target.UserId,
                Expiration = expiration,
                Reason     = reason,
                User       = target
            };

            await DatabaseQueries.InsertAsync(blacklist);

            await ConsoleLogger.LogAsync($"User {target.UserId} has been blacklisted for " +
                                         $"{humanizedExpiration}", LogLvl.INFO);

            if (socketTarget != null &&
                await socketTarget.GetOrCreateDMChannelAsync() != null)
            {
                var embed = new KaguyaEmbedBuilder
                {
                    Title       = "Kaguya Bot Blacklist",
                    Description = $"You have been blacklisted from the Kaguya Bot by an " +
                                  $"Administrator. During this time, you will not be able " +
                                  $"to use any Kaguya Commands, earn points, or earn EXP in any way.\n\n" +
                                  $"[[Appeal]](https://forms.gle/g9LmUZDD8KGchDXYA)",
                    Fields = new List <EmbedFieldBuilder>
                    {
                        new EmbedFieldBuilder
                        {
                            Name  = "Reason",
                            Value = reason
                        },
                        new EmbedFieldBuilder
                        {
                            Name  = "Duration",
                            Value = humanizedExpiration
                        },
                        new EmbedFieldBuilder
                        {
                            Name  = "Administrator",
                            Value = Context.User.ToString()
                        }
                    }
                };

                await(await socketTarget.GetOrCreateDMChannelAsync()).SendEmbedAsync(embed);
            }

            await SendBasicSuccessEmbedAsync($"Successfully blacklisted user " +
                                             $"`{socketTarget?.ToString() ?? target.UserId.ToString()}` " +
                                             $"for `{humanizedExpiration}`.");
        }
Exemplo n.º 8
0
        public static Task Initialize()
        {
            var timer = new Timer(DURATION_MS);

            timer.AutoReset = true;
            timer.Enabled   = true;
            timer.Elapsed  += async(sender, e) =>
            {
                List <User> users = await DatabaseQueries.GetAllAsync <User>(x => x.ActiveRateLimit > 0 && x.UserId != 146092837723832320);

                foreach (User registeredUser in users)
                {
                    if (registeredUser.LastRatelimited < DateTime.Now.Add(TimeSpan.FromDays(-30)).ToOADate() &&
                        registeredUser.RateLimitWarnings > 0)
                    {
                        registeredUser.RateLimitWarnings = 0;
                        await ConsoleLogger.LogAsync($"User [ID: {registeredUser.UserId}] has had their Ratelimit Warnings reset " +
                                                     $"due to not being ratelimited for 30 days.", LogLvl.INFO);
                    }

                    // The user has been rate limited within the last 30 seconds.
                    // Don't accidentally double-rate-limit them.
                    if (registeredUser.LastRatelimited > DateTime.Now.AddSeconds(-30).ToOADate() &&
                        registeredUser.RateLimitWarnings > 0)
                    {
                        return;
                    }

                    if (registeredUser.ActiveRateLimit >= THRESHOLD_REG && !registeredUser.IsPremium ||
                        registeredUser.ActiveRateLimit >= THRESHOLD_PREMIUM && registeredUser.IsPremium)
                    {
                        registeredUser.LastRatelimited = DateTime.Now.ToOADate();
                        registeredUser.RateLimitWarnings++;
                        if (registeredUser.RateLimitWarnings > 7 && registeredUser.ActiveRateLimit > 0)
                        {
                            SocketUser socketUser = ConfigProperties.Client.GetUser(registeredUser.UserId);

                            var maxRlimitEmbed = new KaguyaEmbedBuilder(EmbedColor.RED)
                            {
                                Description = "You have exceeded your maximum allotment of ratelimit strikes, therefore " +
                                              "you will be permanently blacklisted."
                            };

                            try
                            {
                                await socketUser.SendMessageAsync(embed : maxRlimitEmbed.Build());
                            }
                            catch (HttpException)
                            {
                                await ConsoleLogger.LogAsync($"Attempted to DM user {socketUser.Id} about " +
                                                             $"acheiving the maximum allotted ratelimit strikes, " +
                                                             $"but a Discord.Net.HttpException was thrown.", LogLvl.WARN);
                            }

                            var bl = new UserBlacklist
                            {
                                UserId     = socketUser.Id,
                                Expiration = DateTime.MaxValue.ToOADate(),
                                Reason     = "Ratelimit service: Automatic permanent blacklist for surpassing " +
                                             "7 ratelimit strikes in one month.",
                                User = registeredUser
                            };

                            registeredUser.ActiveRateLimit = 0;
                            await DatabaseQueries.UpdateAsync(registeredUser);

                            await DatabaseQueries.InsertOrReplaceAsync(bl);

                            await ConsoleLogger.LogAsync(
                                $"User [Name: {socketUser.Username} | ID: {socketUser.Id} | Supporter: {registeredUser.IsPremium}] " +
                                "has been permanently blacklisted. Reason: Excessive Ratelimiting", LogLvl.WARN);

                            return;
                        }

                        SocketUser user = ConfigProperties.Client.GetUser(registeredUser.UserId);

                        if (user == null)
                        {
                            return;
                        }

                        string[] durations =
                        {
                            "60s",
                            "5m",
                            "30m",
                            "3h",
                            "12h",
                            "1d",
                            "3d"
                        };

                        List <TimeSpan> timeSpans     = durations.Select(RegexTimeParser.ParseToTimespan).ToList();
                        string          humanizedTime = timeSpans.ElementAt(registeredUser.RateLimitWarnings - 1).Humanize();

                        var tempBlacklist = new UserBlacklist
                        {
                            UserId     = user.Id,
                            Expiration = (DateTime.Now + timeSpans.ElementAt(registeredUser.RateLimitWarnings - 1)).ToOADate(),
                            Reason     = $"Ratelimit service: Automatic {timeSpans.ElementAt(registeredUser.RateLimitWarnings - 1)} " +
                                         $"temporary blacklist for surpassing a ratelimit strike",
                            User = registeredUser
                        };

                        await DatabaseQueries.InsertOrReplaceAsync(tempBlacklist);

                        var curRlimEmbed = new KaguyaEmbedBuilder
                        {
                            Description = $"You have been ratelimited for `{humanizedTime}`\n\n" +
                                          $"For this time, you may not use any commands or earn experience points.",
                            Footer = new EmbedFooterBuilder
                            {
                                Text = $"You have {registeredUser.RateLimitWarnings} ratelimit strikes. Receiving " +
                                       $"{durations.Length - registeredUser.RateLimitWarnings} more strikes will result " +
                                       $"in a permanent blacklist."
                            }
                        };

                        curRlimEmbed.SetColor(EmbedColor.RED);

                        bool dm = true;

                        try
                        {
                            await user.SendMessageAsync(embed : curRlimEmbed.Build());
                        }
                        catch (Exception)
                        {
                            dm = false;
                        }

                        await ConsoleLogger.LogAsync($"User [Name: {user?.Username} | ID: {user?.Id} | Supporter: {registeredUser.IsPremium}] " +
                                                     $"has been ratelimited. Duration: {humanizedTime} Direct Message Sent: {dm}", LogLvl.INFO);
                    }

                    if (registeredUser.ActiveRateLimit > 0)
                    {
                        registeredUser.ActiveRateLimit = 0;
                        await DatabaseQueries.UpdateAsync(registeredUser);
                    }
                }
            };

            return(Task.CompletedTask);
        }