Exemplo n.º 1
0
        /// <summary>
        /// Creates a ban infraction for the user if they do not already have one.
        /// </summary>
        /// <param name="guild">The guild that the user was banned from.</param>
        /// <param name="user">The user who was banned.</param>
        /// <returns>
        /// A <see cref="Task"/> that will complete when the operation completes.
        /// </returns>
        private async Task TryCreateBanInfractionAsync(ISocketUser user, ISocketGuild guild)
        {
            if (await _moderationService.AnyInfractionsAsync(GetBanSearchCriteria(guild, user)))
            {
                return;
            }

            var restGuild = await _restClient.GetGuildAsync(guild.Id);

            var auditLogs = (await restGuild.GetAuditLogsAsync(10)
                             .FlattenAsync())
                            .Where(x => x.Action == ActionType.Ban && x.Data is BanAuditLogData)
                            .Select(x => (Entry: x, Data: (BanAuditLogData)x.Data));

            var banLog = auditLogs.FirstOrDefault(x => x.Data.Target.Id == user.Id);

            var reason = string.IsNullOrWhiteSpace(banLog.Entry.Reason)
                ? $"Banned by {banLog.Entry.User.GetFullUsername()}."
                : banLog.Entry.Reason;

            var guildUser = guild.GetUser(banLog.Entry.User.Id);

            await _authorizationService.OnAuthenticatedAsync(guildUser);

            await _moderationService.CreateInfractionAsync(guild.Id, banLog.Entry.User.Id, InfractionType.Ban, user.Id, reason, null);
        }
Exemplo n.º 2
0
        private void DoGuildStats(
            string?counterName,
            ISocketGuild guild)
        {
            var tags = new[] { $"guild:{guild.Name}" };

            if (counterName is { })
Exemplo n.º 3
0
 public void AppendGuildInformation(StringBuilder stringBuilder, ISocketGuild guild)
 {
     stringBuilder
     .AppendLine(Format.Bold("\u276F Guild Information"))
     .AppendLine($"ID: {guild.Id}")
     .AppendLine($"Owner: {MentionUtils.MentionUser(guild.OwnerId)}")
     .AppendLine($"Created: {FormatUtilities.FormatTimeAgo(_utcNow, guild.CreatedAt)}")
     .AppendLine();
 }
        private Task OnUserBannedAsync(ISocketUser user, ISocketGuild guild)
        {
            try
            {
                MessageDispatcher.Dispatch(new UserBannedNotification(user, guild));
            }
            finally
            {
                UpdateGuildPopulationCounter(guild, "user_banned");
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a ban infraction for the user if they do not already have one.
        /// </summary>
        /// <param name="guild">The guild that the user was banned from.</param>
        /// <param name="user">The user who was banned.</param>
        /// <returns>
        /// A <see cref="Task"/> that will complete when the operation completes.
        /// </returns>
        private async Task TryCreateBanInfractionAsync(ISocketUser user, ISocketGuild guild)
        {
            if (await _moderationService.AnyInfractionsAsync(GetBanSearchCriteria(guild, user)))
            {
                return;
            }

            var restGuild = await _restClient.GetGuildAsync(guild.Id);

            var allAudits = await restGuild.GetAuditLogsAsync(10).FlattenAsync();

            var banLog = allAudits
                         .Where(x => x.Action == ActionType.Ban && x.Data is BanAuditLogData)
                         .Select(x => (Entry: x, Data: (BanAuditLogData)x.Data))
                         .FirstOrDefault(x => x.Data.Target.Id == user.Id);

            // We're in a scenario in where the guild does not have a Discord audit of the
            // ban MODiX just received, if that's the case something has gone wrong and we
            // need to investigate.
            if (banLog.Data is null ||
                banLog.Entry is null)
            {
                // Snapshot the most amount of information possible about this event
                // to log this incident and investigate further
                var mostRecentAudit = allAudits.OrderByDescending(x => x.CreatedAt).FirstOrDefault();

                Log.Error("No ban audit found when handling {message} for user {userId}, in guild {guild} - " +
                          "the most recent audit was created at {mostRecentAuditTime}: {mostRecentAuditAction} for user: {mostRecentAuditUserId}",
                          nameof(UserBannedNotification),
                          user.Id,
                          guild.Id,
                          mostRecentAudit?.CreatedAt,
                          mostRecentAudit?.Action,
                          mostRecentAudit?.User.Id);

                return;
            }

            var reason = string.IsNullOrWhiteSpace(banLog.Entry.Reason)
                ? $"Banned by {banLog.Entry.User.GetFullUsername()}."
                : banLog.Entry.Reason;

            var guildUser = guild.GetUser(banLog.Entry.User.Id);

            await _authorizationService.OnAuthenticatedAsync(guildUser);

            await _moderationService.CreateInfractionAsync(guild.Id, banLog.Entry.User.Id, InfractionType.Ban, user.Id, reason, null);
        }
Exemplo n.º 6
0
        public async Task AppendGuildParticipationAsync(StringBuilder stringBuilder, ISocketGuild guild)
        {
            var weekTotal = await _messageRepository.GetTotalMessageCountAsync(guild.Id, TimeSpan.FromDays(7));

            var monthTotal = await _messageRepository.GetTotalMessageCountAsync(guild.Id, TimeSpan.FromDays(30));

            var channelCounts = await _messageRepository.GetTotalMessageCountByChannelAsync(guild.Id, TimeSpan.FromDays(30));

            var orderedChannelCounts = channelCounts.OrderByDescending(x => x.Value);
            var mostActiveChannel    = orderedChannelCounts.FirstOrDefault();

            stringBuilder
            .AppendLine(Format.Bold("\u276F Guild Participation"))
            .AppendLine($"Last 7 days: {"message".ToQuantity(weekTotal, "n0")}")
            .AppendLine($"Last 30 days: {"message".ToQuantity(monthTotal, "n0")}")
            .AppendLine($"Avg. per day: {"message".ToQuantity(monthTotal / 30, "n0")}");

            if (mostActiveChannel is { })
Exemplo n.º 7
0
 /// <summary>
 /// Constructs a new <see cref="JoinedGuildNotification"/> from the given values.
 /// </summary>
 /// <param name="guild">The value to use for <see cref="Guild"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="guild"/>.</exception>
 public JoinedGuildNotification(ISocketGuild guild)
 {
     Guild = guild ?? throw new ArgumentNullException(nameof(guild));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Constructs a new <see cref="UserBannedNotification"/> from the given values.
 /// </summary>
 /// <param name="user">The value to use for <see cref="User"/>.</param>
 /// <param name="guild">The value to use for <see cref="Guild"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="user"/> and <paramref name="guild"/>.</exception>
 public UserBannedNotification(ISocketUser user, ISocketGuild guild)
 {
     User  = user ?? throw new ArgumentNullException(nameof(user));
     Guild = guild ?? throw new ArgumentNullException(nameof(guild));
 }
Exemplo n.º 9
0
        private Task OnUserBannedAsync(ISocketUser user, ISocketGuild guild)
        {
            MessageDispatcher.Dispatch(new UserBannedNotification(user, guild));

            return(Task.CompletedTask);
        }
Exemplo n.º 10
0
        private Task OnJoinedGuildAsync(ISocketGuild guild)
        {
            MessageDispatcher.Dispatch(new JoinedGuildNotification(guild));

            return(Task.CompletedTask);
        }
Exemplo n.º 11
0
        private Task OnGuildAvailableAsync(ISocketGuild guild)
        {
            MessageDispatcher.Dispatch(new GuildAvailableNotification(guild));

            return(Task.CompletedTask);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Constructs a new <see cref="GuildAvailableNotification"/> from the given values.
 /// </summary>
 /// <param name="guild">The value to use for <see cref="Guild"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="guild"/>.</exception>
 public GuildAvailableNotification(ISocketGuild guild)
 {
     Guild = guild ?? throw new ArgumentNullException(nameof(guild));
 }