Ejemplo n.º 1
0
        public async Task <Embed> GetMutedListAsync(Database.ManagmentContext db, SocketCommandContext context)
        {
            string mutedList = "Brak";

            var list = (await db.Penalties.Include(x => x.Roles).FromCacheAsync(new string[] { $"mute" })).Where(x => x.Guild == context.Guild.Id && x.Type == PenaltyType.Mute);

            if (list.Count() > 0)
            {
                mutedList = "";
                foreach (var penalty in list)
                {
                    var endDate = penalty.StartDate.AddHours(penalty.DurationInHours);
                    var name    = context.Guild.GetUser(penalty.User)?.Mention;
                    if (name is null)
                    {
                        continue;
                    }

                    mutedList += $"{name} [DO: {endDate.ToShortDateString()} {endDate.ToShortTimeString()}] - {penalty.Reason}\n";
                }
            }

            return(new EmbedBuilder
            {
                Description = $"**Wyciszeni**:\n\n{mutedList.TrimToLength(1900)}",
                Color = EMType.Bot.Color(),
            }.Build());
        }
Ejemplo n.º 2
0
        private async Task CyclicCheckPenalties(Database.ManagmentContext db)
        {
            foreach (var penalty in await db.GetCachedFullPenalties())
            {
                var guild = _client.GetGuild(penalty.Guild);
                if (guild == null)
                {
                    continue;
                }

                var user = guild.GetUser(penalty.User);
                if (user != null)
                {
                    using (var conf = new Database.GuildConfigContext(_config))
                    {
                        var gconfig = await conf.GetCachedGuildFullConfigAsync(guild.Id);

                        var muteModRole = guild.GetRole(gconfig.ModMuteRole);
                        var muteRole    = guild.GetRole(gconfig.MuteRole);

                        if ((DateTime.Now - penalty.StartDate).TotalHours < penalty.DurationInHours)
                        {
                            var muteMod = penalty.Roles.Any(x => gconfig.ModeratorRoles.Any(z => z.Role == x.Role)) ? muteModRole : null;
                            _ = Task.Run(async() => { await MuteUserGuildAsync(user, muteRole, penalty.Roles, muteMod); });
                            continue;
                        }

                        if (penalty.Type == PenaltyType.Mute)
                        {
                            await UnmuteUserGuildAsync(user, muteRole, muteModRole, penalty.Roles);
                            await RemovePenaltyFromDb(db, penalty);
                        }
                    }
                }
                else
                {
                    if ((DateTime.Now - penalty.StartDate).TotalHours > penalty.DurationInHours)
                    {
                        if (penalty.Type == PenaltyType.Ban)
                        {
                            var ban = await guild.GetBanAsync(penalty.User);

                            if (ban != null)
                            {
                                await guild.RemoveBanAsync(penalty.User);
                            }
                        }
                        await RemovePenaltyFromDb(db, penalty);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private async Task RemovePenaltyFromDb(Database.ManagmentContext db, PenaltyInfo penalty)
        {
            if (penalty == null)
            {
                return;
            }

            db.OwnedRoles.RemoveRange(penalty.Roles);
            db.Penalties.Remove(penalty);

            await db.SaveChangesAsync();

            QueryCacheManager.ExpireTag(new string[] { $"mute" });
        }
Ejemplo n.º 4
0
        public async Task <bool> OnAccept(SessionContext context)
        {
            if (await Message.Channel.GetMessageAsync(Message.Id) is IUserMessage msg)
            {
                await msg.DeleteAsync();
            }

            using (var mdb = new Database.ManagmentContext(_config))
            {
                var info = await Moderation.MuteUserAysnc(User, MuteRole, null, UserRole, mdb, (Fun.GetRandomValue(365) * 24) + 24, "Chciał to dostał :)");

                await Moderation.NotifyAboutPenaltyAsync(User, NotifChannel, info, "Sanakan");

                await Message.Channel.SendMessageAsync("", embed : $"{User.Mention} został wyciszony.".ToEmbedMessage(EMType.Success).Build());
            }
            return(true);
        }
Ejemplo n.º 5
0
        private async Task BotLeftGuildAsync(SocketGuild guild)
        {
            using (var db = new Database.GuildConfigContext(_config))
            {
                var gConfig = await db.GetGuildConfigOrCreateAsync(guild.Id);

                db.Guilds.Remove(gConfig);

                var stats = db.TimeStatuses.Where(x => x.Guild == guild.Id).ToList();
                db.TimeStatuses.RemoveRange(stats);

                await db.SaveChangesAsync();
            }

            using (var db = new Database.ManagmentContext(_config))
            {
                var mute = db.Penalties.Where(x => x.Guild == guild.Id).ToList();
                db.Penalties.RemoveRange(mute);

                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 6
0
        public async Task <PenaltyInfo> BanUserAysnc(SocketGuildUser user, Database.ManagmentContext db, long duration, string reason = "nie podano")
        {
            var info = new PenaltyInfo
            {
                User            = user.Id,
                Reason          = reason,
                Guild           = user.Guild.Id,
                Type            = PenaltyType.Ban,
                StartDate       = DateTime.Now,
                DurationInHours = duration,
                Roles           = new List <OwnedRole>(),
            };

            await db.Penalties.AddAsync(info);

            await db.SaveChangesAsync();

            QueryCacheManager.ExpireTag(new string[] { $"mute" });

            await user.Guild.AddBanAsync(user, 0, reason);

            return(info);
        }
Ejemplo n.º 7
0
        private async Task MakeActionAsync(Action action, SocketGuildUser user, SocketUserMessage message, SocketRole userRole, SocketRole muteRole, ITextChannel notifChannel)
        {
            switch (action)
            {
            case Action.Warn:
                await message.Channel.SendMessageAsync("",
                                                       embed : $"{user.Mention} zaraz przekroczysz granicę!".ToEmbedMessage(EMType.Bot).Build());

                break;

            case Action.Mute:
                if (muteRole != null)
                {
                    if (user.Roles.Contains(muteRole))
                    {
                        return;
                    }

                    using (var db = new Database.ManagmentContext(_config))
                    {
                        var info = await _moderator.MuteUserAysnc(user, muteRole, null, userRole, db, 24, "spam/flood");

                        await _moderator.NotifyAboutPenaltyAsync(user, notifChannel, info);
                    }
                }
                break;

            case Action.Ban:
                await user.Guild.AddBanAsync(user, 1, "Supervisor(ban) spam/flood");

                break;

            default:
            case Action.None:
                break;
            }
        }
Ejemplo n.º 8
0
        public Moderator(ILogger logger, IConfig config, DiscordSocketClient client)
        {
            _logger = logger;
            _config = config;
            _client = client;

            _timer = new Timer(async _ =>
            {
                try
                {
                    using (var db = new Database.ManagmentContext(_config))
                    {
                        await CyclicCheckPenalties(db);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Log($"in penalty: {ex}");
                }
            },
                               null,
                               TimeSpan.FromMinutes(1),
                               TimeSpan.FromSeconds(30));
        }
Ejemplo n.º 9
0
        public async Task <PenaltyInfo> MuteUserAysnc(SocketGuildUser user, SocketRole muteRole, SocketRole muteModRole, SocketRole userRole,
                                                      Database.ManagmentContext db, long duration, string reason = "nie podano", IEnumerable <ModeratorRoles> modRoles = null)
        {
            var info = new PenaltyInfo
            {
                User            = user.Id,
                Reason          = reason,
                Guild           = user.Guild.Id,
                Type            = PenaltyType.Mute,
                StartDate       = DateTime.Now,
                DurationInHours = duration,
                Roles           = new List <OwnedRole>(),
            };

            await db.Penalties.AddAsync(info);

            if (userRole != null)
            {
                if (user.Roles.Contains(userRole))
                {
                    await user.RemoveRoleAsync(userRole);

                    info.Roles.Add(new OwnedRole
                    {
                        Role = userRole.Id
                    });
                }
            }

            if (modRoles != null)
            {
                foreach (var r in modRoles)
                {
                    var role = user.Roles.FirstOrDefault(x => x.Id == r.Role);
                    if (role == null)
                    {
                        continue;
                    }

                    await user.RemoveRoleAsync(role);

                    info.Roles.Add(new OwnedRole
                    {
                        Role = role.Id
                    });
                }
            }

            if (!user.Roles.Contains(muteRole))
            {
                await user.AddRoleAsync(muteRole);
            }

            if (muteModRole != null)
            {
                if (!user.Roles.Contains(muteModRole))
                {
                    await user.AddRoleAsync(muteModRole);
                }
            }

            await db.SaveChangesAsync();

            QueryCacheManager.ExpireTag(new string[] { $"mute" });

            return(info);
        }
Ejemplo n.º 10
0
        public async Task UnmuteUserAsync(SocketGuildUser user, SocketRole muteRole, SocketRole muteModRole, Database.ManagmentContext db)
        {
            var penalty = await db.Penalties.Include(x => x.Roles).FirstOrDefaultAsync(x => x.User == user.Id &&
                                                                                       x.Type == PenaltyType.Mute && x.Guild == user.Guild.Id);

            await UnmuteUserGuildAsync(user, muteRole, muteModRole, penalty?.Roles);
            await RemovePenaltyFromDb(db, penalty);
        }
Ejemplo n.º 11
0
 public static async Task <IEnumerable <PenaltyInfo> > GetCachedFullPenalties(this Database.ManagmentContext context)
 {
     return((await context.Penalties.Include(x => x.Roles).AsNoTracking().FromCacheAsync(new string[] { $"mute" })).ToList());
 }