Exemplo n.º 1
0
        private Embed CreateEmbedResponse(ModerationLogEntry logEntry)
        {
            EmbedBuilder builder = new EmbedBuilder();

            StringBuilder author      = new StringBuilder($"[{logEntry.ActionType.Humanize()}]");
            StringBuilder description = new StringBuilder();

            if (logEntry.HasTarget)
            {
                if (logEntry.Target != null)
                {
                    author.Append($" {logEntry.Target.ToString()}");
                }
                else
                {
                    author.Append($" {logEntry.TargetMention}");
                }
            }
            if (logEntry.Reason != Commands.ModerationModule.DefaultReason)
            {
                description.AppendLine(logEntry.Reason).AppendLine();
            }
            if (logEntry.Duration.HasValue)
            {
                description.AppendLine($"Duration: {logEntry.Duration.Value.Humanize(7)}");
            }

            return(builder
                   .WithAuthor(author.ToString(), logEntry.Target?.EnsureAvatarUrl())
                   .WithColor(GetColorForAction(logEntry.ActionType))
                   .WithDescription(description.ToString())
                   .Build());
        }
        public async Task CreateEntry(ModerationLogEntry logEntry, IMessageChannel replyChannel = null)
        {
            if (replyChannel != null)
            {
                await CreateEmbedResponse(logEntry).SendToChannel(replyChannel);
            }

            await _log.LogMessageAsync(new LogMessage(LogSeverity.Info, "ModLog", $"{logEntry.Moderator} performed {logEntry.ActionType}."));

            await PostToLogChannelAsync(logEntry);
        }
Exemplo n.º 3
0
 private Embed CreateEmbedLogEntry(ModerationLogEntry logEntry)
 => new EmbedBuilder()
 .WithAuthor(logEntry.ActionType.Humanize(), logEntry.Target?.EnsureAvatarUrl())
 .WithColor(GetColorForAction(logEntry.ActionType))
 .AddFieldConditional(logEntry.HasTarget, "User", logEntry.TargetMention, true)
 .AddField("Moderator", logEntry.Moderator.Mention, true)
 .AddFieldConditional(!string.IsNullOrEmpty(logEntry.Reason), "Reason", logEntry.Reason, true)
 .AddFieldConditional(logEntry.Channel != null, "Channel", logEntry.Channel?.Mention, true)
 .AddFieldConditional(logEntry.Duration != null, "Duration", (logEntry.Duration ?? TimeSpan.Zero).Humanize(7), true)
 .AddFieldConditional(logEntry.AdditionalInfo != null, "Additional info", logEntry.AdditionalInfo)
 .WithFooter($"{logEntry.Time.ToTimeString()} | {logEntry.Time.ToDateString()}")
 .Build();
        private async Task PostToLogChannelAsync(ModerationLogEntry logEntry)
        {
            ulong moderationLogChannelID = _data.Configuration.ModerationLogChannelID;

            if (moderationLogChannelID == 0)
            {
                throw new Exception("Invalid moderation log channel ID.");
            }

            Embed embed = CreateEmbedLogEntry(logEntry);

            IGuild       guild   = _client.GetGuild(_data.Configuration.GuildID);
            ITextChannel channel = await guild.GetTextChannelAsync(moderationLogChannelID);

            await embed.SendToChannel(channel);
        }
        private Embed CreateEmbedResponse(ModerationLogEntry logEntry)
        {
            EmbedBuilder builder = new EmbedBuilder();

            StringBuilder author    = new StringBuilder($"[{logEntry.ActionType.Humanize()}]");
            List <string> footnotes = new List <string>();

            if (logEntry.HasTarget)
            {
                if (logEntry.Target != null)
                {
                    author.Append($" {logEntry.Target.Username}#{logEntry.Target.Discriminator}");
                }
                else
                {
                    author.Append($" {logEntry.TargetID}");
                }

                footnotes.Add(logEntry.TargetID.ToString());
            }

            if (logEntry.InfractionId > -1)
            {
                footnotes.Add($"Infraction ID: {logEntry.InfractionId}");
            }

            // Split user and infraction ID by bullet point
            builder.WithFooter(string.Join(" \u2022 ", footnotes));

            // First display the Reason, then any additional fields; looks better.
            builder.AddFieldConditional(!string.IsNullOrWhiteSpace(logEntry.Reason), "Reason", logEntry.Reason);

            // AddFieldConditional won't work because the logEntry.Duration.Value will be resolved first, which can be a NullReferenceException because
            //  it is not guarantueed Duration will be non-null.
            if (logEntry.Duration.HasValue)
            {
                builder.AddField("Duration", logEntry.Duration.Value.Humanize(7));
            }

            return(builder
                   .WithAuthor(author.ToString(), logEntry.Target?.EnsureAvatarUrl())
                   .WithColor(GetColorForAction(logEntry.ActionType))
                   .Build());
        }
        private Embed CreateEmbedResponse(ModerationLogEntry logEntry)
        {
            EmbedBuilder builder = new EmbedBuilder();

            StringBuilder author = new StringBuilder($"[{logEntry.ActionType.Humanize()}]");

            if (logEntry.HasTarget)
            {
                if (logEntry.Target != null)
                {
                    author.Append($" {logEntry.Target.ToString()}");
                }
                else
                {
                    author.Append($" {logEntry.TargetMention}");
                }
            }

            if (logEntry.InfractionId > -1)
            {
                builder.WithFooter($"Infraction ID: {logEntry.InfractionId}");
            }

            // First display the Reason, then any additional fields; looks better.
            builder.AddField("Reason", logEntry.Reason);

            // AddFieldConditional won't work because the logEntry.Duration.Value will be resolved first, which can be a NullReferenceException because
            //  it is not guarantueed Duration will be non-null.
            if (logEntry.Duration.HasValue)
            {
                builder.AddField("Duration", logEntry.Duration.Value.Humanize(7));
            }

            return(builder
                   .WithAuthor(author.ToString(), logEntry.Target?.EnsureAvatarUrl())
                   .WithColor(GetColorForAction(logEntry.ActionType))
                   .Build());
        }
        private void CheckTemporaryInfractions()
        {
            DateTime    now   = DateTime.UtcNow;
            SocketGuild guild = _client.GetGuild(_data.Configuration.GuildID);

            int resolvedCounter = 0;

            foreach (UserData user in _data.UserData.GetUsersWithTemporaryInfractions())
            {
                if (user.HasTemporaryInfraction(TemporaryInfractionType.TempBan))
                {
                    TemporaryInfraction infraction = user.TemporaryInfractions.First(t => t.Type == TemporaryInfractionType.TempBan);
                    if (infraction.Expire <= now)
                    {
                        guild.RemoveBanAsync(user.ID);

                        _ = _modLog.CreateEntry(ModerationLogEntry.New
                                                .WithActionType(ModerationActionType.Unban)
                                                .WithTarget(user.ID)
                                                .WithReason("Temporary ban timed out.")
                                                .WithTime(DateTimeOffset.Now)
                                                .WithModerator(_client.CurrentUser));

                        user.TemporaryInfractions.RemoveAll(i => i.Type == TemporaryInfractionType.TempBan);
                        resolvedCounter++;
                    }
                }
                if (user.HasTemporaryInfraction(TemporaryInfractionType.TempMute))
                {
                    TemporaryInfraction infraction = user.TemporaryInfractions.First(t => t.Type == TemporaryInfractionType.TempMute);
                    if (infraction.Expire <= now)
                    {
                        IRole mutedRole = guild.GetRole(_data.Configuration.MutedRoleID);

                        // If the user is no longer in the server, just remove the entry, but don't attempt to remove his role.
                        IGuildUser guildUser = guild.GetUser(user.ID);
                        guildUser?.RemoveRoleAsync(mutedRole);

                        user.Muted = false;
                        _data.SaveUserData();

                        ModerationLogEntry entry = ModerationLogEntry.New
                                                   .WithActionType(ModerationActionType.Unmute)
                                                   .WithReason("Temporary mute timed out.")
                                                   .WithTime(DateTimeOffset.Now)
                                                   .WithModerator(_client.CurrentUser);

                        if (guildUser != null)
                        {
                            entry = entry.WithTarget(guildUser);
                        }
                        else
                        {
                            entry = entry.WithTarget(user.ID);
                        }

                        _ = _modLog.CreateEntry(entry);

                        user.TemporaryInfractions.RemoveAll(i => i.Type == TemporaryInfractionType.TempMute);
                        resolvedCounter++;
                    }
                }
            }

            if (resolvedCounter > 0)
            {
                _log.LogMessageAsync(new LogMessage(LogSeverity.Info, "TemporaryInfractions", $"Resolved {resolvedCounter} temporary infraction(s)."));
                _data.SaveUserData();
            }
        }