예제 #1
0
        private async Task HandleUserJoin(SocketGuildUser u)
        {
            await Logger.DetailedLog(u.Guild, "Event", "User Joined", "User", $"{u}", u.Id, new Color(12, 255, 129), false);

            using (var db = new DbContext())
            {
                var guildRepo = new GuildRepository(db);
                var muteRepo  = new MuteRepository(db);
                var user      = u as IGuildUser;
                var mutedRole = user.Guild.GetRole((await guildRepo.FetchGuildAsync(user.Guild.Id)).MutedRoleId);
                if (mutedRole != null && u.Guild.CurrentUser.GuildPermissions.ManageRoles &&
                    mutedRole.Position < u.Guild.CurrentUser.Roles.OrderByDescending(x => x.Position).First().Position)
                {
                    await RankHandler.Handle(u.Guild, u.Id);

                    if (await muteRepo.IsMutedAsync(user.Id, user.Guild.Id) && mutedRole != null && user != null)
                    {
                        await user.AddRoleAsync(mutedRole);
                    }
                }

                if (Config.BLACKLISTED_IDS.Any(x => x == u.Id))
                {
                    if (u.Guild.CurrentUser.GuildPermissions.BanMembers &&
                        u.Guild.CurrentUser.Roles.OrderByDescending(x => x.Position).First().Position >
                        u.Roles.OrderByDescending(x => x.Position).First().Position&& u.Guild.OwnerId != u.Id)
                    {
                        await u.Guild.AddBanAsync(u);
                    }
                }
            }
        }
예제 #2
0
        public async Task Mute(IGuildUser userToMute, [Remainder] string reason = "No reason.")
        {
            var guild = await GuildRepository.FetchGuildAsync(Context.Guild.Id);

            var mutedRole = Context.Guild.GetRole((ulong)guild.MutedRoleId);

            if (mutedRole == null)
            {
                throw new Exception($"You may not mute users if the muted role is not valid.\nPlease use the " +
                                    $"`{guild.Prefix}SetMutedRole` command to change that.");
            }
            if (await IsModAsync(userToMute))
            {
                throw new Exception("You cannot mute another mod!");
            }
            await InformSubjectAsync(Context.User, "Mute", userToMute, reason);

            await userToMute.AddRoleAsync(mutedRole);

            await MuteRepository.AddMuteAsync(userToMute.Id, Context.Guild.Id, Config.DEFAULT_MUTE_TIME);

            await Logger.ModLog(Context, "Mute", new Color(255, 114, 14), reason, userToMute, $"\n**Length:** {Config.DEFAULT_MUTE_TIME.TotalHours} hours");

            await ReplyAsync($"{Context.User.Mention} has successfully muted {userToMute.Mention}!");
        }
예제 #3
0
 public ModerationService(IServiceProvider serviceProvider)
 {
     _serviceProvider = serviceProvider;
     _guildRepository = _serviceProvider.GetRequiredService <GuildRepository>();
     _userRepository  = _serviceProvider.GetRequiredService <UserRepository>();
     _muteRepository  = _serviceProvider.GetRequiredService <MuteRepository>();
     _text            = _serviceProvider.GetRequiredService <Text>();
 }
예제 #4
0
 public Moderator(IServiceProvider serviceProvider) : base(serviceProvider)
 {
     _serviceProvider   = serviceProvider;
     _userRepository    = _serviceProvider.GetRequiredService <UserRepository>();
     _pollRepository    = _serviceProvider.GetRequiredService <PollRepository>();
     _muteRepository    = _serviceProvider.GetRequiredService <MuteRepository>();
     _moderationService = _serviceProvider.GetRequiredService <ModerationService>();
 }
예제 #5
0
 public UserJoined(IServiceProvider serviceProvider)
 {
     _serviceProvider    = serviceProvider;
     _userRepo           = _serviceProvider.GetService <UserRepository>();
     _guildRepo          = serviceProvider.GetService <GuildRepository>();
     _muteRepo           = serviceProvider.GetService <MuteRepository>();
     _blacklistRepo      = serviceProvider.GetService <BlacklistRepository>();
     _rankHandler        = serviceProvider.GetService <RankHandler>();
     _client             = _serviceProvider.GetService <DiscordSocketClient>();
     _client.UserJoined += HandleUserJoined;
 }
예제 #6
0
        public AutoUnmute(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _client          = _serviceProvider.GetService <DiscordSocketClient>();
            _guildRepo       = _serviceProvider.GetService <GuildRepository>();
            _muteRepo        = _serviceProvider.GetService <MuteRepository>();

            ObjectState StateObj = new ObjectState();

            TimerCallback TimerDelegate = new TimerCallback(Unmute);

            _timer = new Timer(TimerDelegate, StateObj, TimeSpan.FromMilliseconds(1000), Config.AUTO_UNMUTE_COOLDOWN);

            StateObj.TimerReference = _timer;
        }
예제 #7
0
        private async void OnTimedAutoUnmute(object source, ElapsedEventArgs e)
        {
            using (var db = new DbContext())
            {
                var    guildRepo = new GuildRepository(db);
                var    muteRepo  = new MuteRepository(db);
                Mute[] mutes     = muteRepo.GetAll().ToArray();
                foreach (Mute muted in mutes)
                {
                    if (DateTime.Now.Subtract(DateTime.Parse(muted.MutedAt)).TotalMilliseconds > muted.MuteLength)
                    {
                        var guild = _client.GetGuild(muted.GuildId);
                        if (guild != null && guild.GetUser(muted.UserId) != null)
                        {
                            var guildData = await guildRepo.FetchGuildAsync(guild.Id);

                            var mutedRole = guild.GetRole(guildData.MutedRoleId);
                            if (mutedRole != null && guild.GetUser(muted.UserId).Roles.Any(x => x.Id == mutedRole.Id))
                            {
                                var channel = guild.GetTextChannel(guildData.ModLogChannelId);
                                if (channel != null && guild.CurrentUser.GuildPermissions.EmbedLinks &&
                                    (guild.CurrentUser as IGuildUser).GetPermissions(channel as SocketTextChannel).SendMessages &&
                                    (guild.CurrentUser as IGuildUser).GetPermissions(channel as SocketTextChannel).EmbedLinks)
                                {
                                    await guild.GetUser(muted.UserId).RemoveRoleAsync(mutedRole);

                                    var footer = new EmbedFooterBuilder()
                                    {
                                        IconUrl = "http://i.imgur.com/BQZJAqT.png",
                                        Text    = $"Case #{guildData.CaseNumber}"
                                    };
                                    var builder = new EmbedBuilder()
                                    {
                                        Color       = new Color(12, 255, 129),
                                        Description = $"**Action:** Automatic Unmute\n**User:** {guild.GetUser(muted.UserId)} ({guild.GetUser(muted.UserId).Id})",
                                        Footer      = footer
                                    }.WithCurrentTimestamp();
                                    await guildRepo.ModifyAsync(x => { x.CaseNumber++; return(Task.CompletedTask); }, guild.Id);

                                    await channel.SendMessageAsync("", embed : builder);
                                }
                            }
                        }
                        await muteRepo.RemoveMuteAsync(muted.UserId, muted.GuildId);
                    }
                }
            }
        }
예제 #8
0
        public AutoUnmute(IServiceProvider serviceProvider)
        {
            _serviceProvider   = serviceProvider;
            _client            = _serviceProvider.GetRequiredService <DiscordSocketClient>();
            _guildRepository   = _serviceProvider.GetRequiredService <GuildRepository>();
            _muteRepository    = _serviceProvider.GetRequiredService <MuteRepository>();
            _moderationService = _serviceProvider.GetRequiredService <ModerationService>();

            ObjectState StateObj = new ObjectState();

            TimerCallback TimerDelegate = new TimerCallback(Unmute);

            _timer = new Timer(TimerDelegate, StateObj, TimeSpan.Zero, Configuration.AutoUnmuteCooldown);

            StateObj.TimerReference = _timer;
        }
예제 #9
0
        public async Task Unmute(IGuildUser userToUnmute, [Remainder] string reason = "No reason.")
        {
            var mutedRoleId = GuildRepository.FetchGuild(Context.Guild.Id).MutedRoleId;

            if (userToUnmute.RoleIds.All(x => x != mutedRoleId))
            {
                throw new Exception("You cannot unmute a user who isn't muted.");
            }
            await ModuleMethods.InformSubjectAsync(Context.User, "Unmute", userToUnmute, reason);

            await userToUnmute.RemoveRoleAsync(Context.Guild.GetRole(mutedRoleId));

            MuteRepository.RemoveMute(userToUnmute.Id, Context.Guild.Id);
            await Logger.ModLog(Context, "Unmute", new Color(12, 255, 129), reason, userToUnmute);

            await ReplyAsync($"{Context.User.Mention} has successfully unmuted {userToUnmute.Mention}!");
        }
예제 #10
0
 private void AutoUnmute()
 {
     Timer t = new Timer(async method =>
     {
         foreach (Mute mute in await(await DEABot.Mutes.FindAsync("")).ToListAsync())
         {
             if (DateTime.UtcNow.Subtract(mute.MutedAt).TotalMilliseconds > mute.MuteLength)
             {
                 var guild = _client.GetGuild(mute.GuildId);
                 if (guild != null && guild.GetUser(mute.UserId) != null)
                 {
                     var guildData = GuildRepository.FetchGuild(guild.Id);
                     var mutedRole = guild.GetRole(guildData.MutedRoleId);
                     if (mutedRole != null && guild.GetUser(mute.UserId).Roles.Any(x => x.Id == mutedRole.Id))
                     {
                         var channel = guild.GetTextChannel(guildData.ModLogId);
                         if (channel != null && guild.CurrentUser.GuildPermissions.EmbedLinks &&
                             (guild.CurrentUser as IGuildUser).GetPermissions(channel as SocketTextChannel).SendMessages &&
                             (guild.CurrentUser as IGuildUser).GetPermissions(channel as SocketTextChannel).EmbedLinks)
                         {
                             await guild.GetUser(mute.UserId).RemoveRoleAsync(mutedRole);
                             var footer = new EmbedFooterBuilder()
                             {
                                 IconUrl = "http://i.imgur.com/BQZJAqT.png",
                                 Text    = $"Case #{guildData.CaseNumber}"
                             };
                             var builder = new EmbedBuilder()
                             {
                                 Color       = new Color(12, 255, 129),
                                 Description = $"**Action:** Automatic Unmute\n**User:** {guild.GetUser(mute.UserId)} ({guild.GetUser(mute.UserId).Id})",
                                 Footer      = footer
                             }.WithCurrentTimestamp();
                             GuildRepository.Modify(DEABot.GuildUpdateBuilder.Set(x => x.CaseNumber, ++guildData.CaseNumber), guild.Id);
                             await channel.SendMessageAsync("", embed: builder);
                         }
                     }
                 }
                 MuteRepository.RemoveMute(mute.UserId, mute.GuildId);
             }
         }
     },
                         null,
                         Config.AUTO_UNMUTE_COOLDOWN,
                         Timeout.Infinite);
 }
예제 #11
0
        private async Task HandleUserJoin(SocketGuildUser u)
        {
            await Logger.DetailedLog(u.Guild, "Event", "User Joined", "User", $"{u}", u.Id, new Color(12, 255, 129), false);

            var user      = u as IGuildUser;
            var mutedRole = user.Guild.GetRole(((GuildRepository.FetchGuild(user.Guild.Id)).MutedRoleId));

            if (mutedRole != null && u.Guild.CurrentUser.GuildPermissions.ManageRoles &&
                mutedRole.Position < u.Guild.CurrentUser.Roles.OrderByDescending(x => x.Position).First().Position)
            {
                await RankHandler.Handle(u.Guild, u.Id);

                if (MuteRepository.IsMuted(user.Id, user.Guild.Id) && mutedRole != null && user != null)
                {
                    await user.AddRoleAsync(mutedRole);
                }
            }
        }
예제 #12
0
        public async Task CustomMute(double hours, IGuildUser userToMute, [Remainder] string reason = "No reason.")
        {
            if (hours > 168)
            {
                throw new Exception("You may not mute a user for more than a week.");
            }
            if (hours < 1)
            {
                throw new Exception("You may not mute a user for less than 1 hour.");
            }
            using (var db = new DbContext())
            {
                string time = "hours";
                if (hours == 1)
                {
                    time = "hour";
                }
                var guildRepo = new GuildRepository(db);
                var guild     = await guildRepo.FetchGuildAsync(Context.Guild.Id);

                var mutedRole = Context.Guild.GetRole(guild.MutedRoleId);
                if (mutedRole == null)
                {
                    throw new Exception($"You may not mute users if the muted role is not valid.\nPlease use the " +
                                        $"{guild.Prefix}SetMutedRole command to change that.");
                }
                var muteRepo = new MuteRepository(db);
                if (await IsMod(userToMute))
                {
                    throw new Exception("You cannot mute another mod!");
                }
                await InformSubject(Context.User, "Mute", userToMute, reason);

                await userToMute.AddRoleAsync(mutedRole);

                await muteRepo.AddMuteAsync(userToMute.Id, Context.Guild.Id, TimeSpan.FromHours(hours), DateTime.Now);

                await Logger.ModLog(Context, "Mute", new Color(255, 114, 14), reason, userToMute, $"\n**Length:** {hours} {time}");
                await ReplyAsync($"{Context.User.Mention} has successfully muted {userToMute.Mention} for {hours} {time}!");
            }
        }
예제 #13
0
        public async Task Unmute(IGuildUser userToUnmute, [Remainder] string reason = "No reason.")
        {
            using (var db = new DbContext())
            {
                var guildRepo   = new GuildRepository(db);
                var mutedRoleId = (await guildRepo.FetchGuildAsync(Context.Guild.Id)).MutedRoleId;
                if (userToUnmute.RoleIds.All(x => x != mutedRoleId))
                {
                    throw new Exception("You cannot unmute a user who isn't muted.");
                }
                var muteRepo = new MuteRepository(db);
                await InformSubject(Context.User, "Unmute", userToUnmute, reason);

                await userToUnmute.RemoveRoleAsync(Context.Guild.GetRole(mutedRoleId));

                await muteRepo.RemoveMuteAsync(userToUnmute.Id, Context.Guild.Id);

                await Logger.ModLog(Context, "Unmute", new Color(12, 255, 129), reason, userToUnmute);
                await ReplyAsync($"{Context.User.Mention} has successfully unmuted {userToUnmute.Mention}!");
            }
        }
예제 #14
0
 public Moderation(MuteRepository muteRepo, ModerationService moderationService)
 {
     _muteRepo          = muteRepo;
     _moderationService = moderationService;
 }
예제 #15
0
        public async static Task RemindMutes()
        {
            MuteRepository repo   = new MuteRepository(ReadConfig.Config.ConnectionString);
            UserRepository urepo  = new UserRepository(ReadConfig.Config.ConnectionString);
            bool           result = repo.GetAll(out List <Mute> list);

            if (!result)
            {
                _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting all mutes in TimerTick.");
                return;
            }
            foreach (var item in list)
            {
                if (item.Timestamp + TimeSpan.FromDays(item.Duration) <= DateTime.Now)
                {
                    result = urepo.Read(item.Mod, out User dbMod);
                    if (!result)
                    {
                        _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting a mod from the database.");
                        continue;
                    }
                    DiscordMember mod = null;
                    try
                    {
                        mod = await DiscordObjectService.Instance.Lathland.GetMemberAsync(dbMod.DcID);
                    }
                    catch (NotFoundException)
                    {
                        _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting a mod from discord.");
                    }
                    result = urepo.Read(item.User, out User dbUser);
                    if (!result)
                    {
                        _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting a user from the database.");
                        continue;
                    }
                    DiscordMember user = null;
                    try
                    {
                        user = await DiscordObjectService.Instance.Lathland.GetMemberAsync(dbUser.DcID);
                    }
                    catch (NotFoundException)
                    {
                        _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting a user from discord.");
                    }
                    if (!(user is null) && user.Roles.Contains(DiscordObjectService.Instance.Lathland.GetRole(701446136208293969)))
                    {
                        if (!(await(await mod.CreateDmChannelAsync()).GetMessagesAsync(5)).Any(x => x.Content.Contains("You will be reminded again tomorrow.") && x.CreationTimestamp > DateTime.Now - TimeSpan.FromHours(24)))
                        {
                            await mod.SendMessageAsync($"The user {user.DisplayName}#{user.Discriminator} ({user.Id}) you muted at {item.Timestamp:yyyy-MM-dd hh:mm} for {item.Duration} days, is now muted for {(DateTime.Now - item.Timestamp):dd} days.\n" +
                                                       $"You will be reminded again tomorrow.");

                            if ((item.Duration < 8 && (item.Timestamp + TimeSpan.FromDays(item.Duration + 2)) < DateTime.Now) ||
                                (item.Duration > 7 && (item.Timestamp + TimeSpan.FromDays(item.Duration + 1)) < DateTime.Now) ||
                                (item.Duration == 14 && (item.Timestamp + TimeSpan.FromDays(item.Duration)) < DateTime.Now))
                            {
                                await DiscordObjectService.Instance.Lathland.GetChannel(722905404354592900).SendMessageAsync($"The user {user.DisplayName}#{user.Discriminator} ({user.Id}), muted by {mod.DisplayName}#{mod.Discriminator} ({mod.Id}) at {item.Timestamp:yyyy-MM-dd hh:mm} for {item.Duration} days, is now muted for {(DateTime.Now - item.Timestamp):dd} days.");
                            }
                        }
                    }
                    else if (user is null || !user.Roles.Contains(DiscordObjectService.Instance.Lathland.GetRole(701446136208293969)))
                    {
                        result = repo.Delete(item.Id);
                        if (!result)
                        {
                            _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error deleting a Mute from database.");
                            continue;
                        }
                    }
                }
            }