/// <summary>
    /// Retrieves the moderation log channel.
    /// </summary>
    /// <param name="guildID">The guild to grab the channel from.</param>
    /// <returns>A retrieval result which may or may not have succeeded.</returns>
    private async Task <Result <Snowflake> > GetModerationLogChannelAsync(Snowflake guildID)
    {
        var getSettings = await _moderation.GetOrCreateServerSettingsAsync(guildID);

        if (!getSettings.IsSuccess)
        {
            return(Result <Snowflake> .FromError(getSettings));
        }

        var settings = getSettings.Entity;

        if (settings.ModerationLogChannel is null)
        {
            return(new UserError("No configured channel."));
        }

        return(settings.ModerationLogChannel.Value);
    }
        public async Task <RuntimeResult> AddWarningAsync
        (
            IGuildUser user,
            string reason,
            TimeSpan?expiresAfter = null
        )
        {
            DateTime?expiresOn = null;

            if (!(expiresAfter is null))
            {
                expiresOn = DateTime.Now.Add(expiresAfter.Value);
            }

            var addWarning = await _warnings.CreateWarningAsync(this.Context.User, user, reason, expiresOn : expiresOn);

            if (!addWarning.IsSuccess)
            {
                return(addWarning.ToRuntimeResult());
            }

            var warning     = addWarning.Entity;
            var getSettings = await _moderation.GetOrCreateServerSettingsAsync(this.Context.Guild);

            if (!getSettings.IsSuccess)
            {
                return(getSettings.ToRuntimeResult());
            }

            var settings = getSettings.Entity;

            var notifyResult = await _logging.NotifyUserWarningAddedAsync(warning);

            if (!notifyResult.IsSuccess)
            {
                return(notifyResult.ToRuntimeResult());
            }

            var warnings = await _warnings.GetWarningsAsync(user);

            if (warnings.Count >= settings.WarningThreshold)
            {
                await _feedback.SendWarningAsync
                (
                    this.Context, $"The warned user now has {warnings.Count} warnings. Consider further action."
                );
            }

            return(RuntimeCommandResult.FromSuccess($"Warning added (ID {warning.ID})."));
        }
Beispiel #3
0
        public async Task <IResult> ShowServerSettingsAsync()
        {
            var getGuild = await _guildAPI.GetGuildAsync(_context.GuildID.Value);

            if (!getGuild.IsSuccess)
            {
                return(getGuild);
            }

            var guild = getGuild.Entity;

            var getSettings = await _moderation.GetOrCreateServerSettingsAsync(guild.ID);

            if (!getSettings.IsSuccess)
            {
                return(getSettings);
            }

            var settings = getSettings.Entity;

            var getGuildIcon = CDN.GetGuildIconUrl(guild);

            var embedFields = new List <EmbedField>();
            var eb          = new Embed
            {
                Colour    = _feedback.Theme.Secondary,
                Title     = guild.Name,
                Thumbnail = getGuildIcon.IsSuccess
                    ? new EmbedThumbnail(getGuildIcon.Entity.ToString())
                    : default(Optional <IEmbedThumbnail>),
                Fields = embedFields
            };

            var moderationLogChannelName = settings.ModerationLogChannel.HasValue
                ? $"<#{settings.ModerationLogChannel}>"
                : "None";

            embedFields.Add(new EmbedField("Moderation Log Channel", moderationLogChannelName));

            var monitoringChannelName = settings.MonitoringChannel.HasValue
                ? $"<#{settings.MonitoringChannel}>"
                : "None";

            embedFields.Add(new EmbedField("Event Monitor Channel", monitoringChannelName));

            embedFields.Add(new EmbedField("Warning Threshold", settings.WarningThreshold.ToString()));

            return(await _feedback.SendContextualEmbedAsync(eb));
        }
        public async Task AddWarningAsync
        (
            [NotNull] IGuildUser user,
            [NotNull] string content,
            TimeSpan?expiresAfter = null
        )
        {
            DateTime?expiresOn = null;

            if (!(expiresAfter is null))
            {
                expiresOn = DateTime.Now.Add(expiresAfter.Value);
            }

            var addWarning = await _warnings.CreateWarningAsync(this.Context.User, user, content, expiresOn : expiresOn);

            if (!addWarning.IsSuccess)
            {
                await _feedback.SendErrorAsync(this.Context, addWarning.ErrorReason);

                return;
            }

            var warning = addWarning.Entity;
            await _feedback.SendConfirmationAsync(this.Context, $"Warning added (ID {warning.ID}).");

            await _logging.NotifyUserWarningAdded(warning);

            var getSettings = await _moderation.GetOrCreateServerSettingsAsync(this.Context.Guild);

            if (!getSettings.IsSuccess)
            {
                return;
            }

            var settings = getSettings.Entity;

            var warningCount = await _warnings.GetWarnings(user).CountAsync();

            if (warningCount >= settings.WarningThreshold)
            {
                await _feedback.SendWarningAsync
                (
                    this.Context, $"The warned user now has {warningCount} warnings. Consider further action."
                );
            }
        }
            public async Task ShowServerSettingsAsync()
            {
                var guild = this.Context.Guild;

                var getSettings = await _moderation.GetOrCreateServerSettingsAsync(guild);

                if (!getSettings.IsSuccess)
                {
                    await _feedback.SendErrorAsync(this.Context, getSettings.ErrorReason);

                    return;
                }

                var settings = getSettings.Entity;

                var eb = _feedback.CreateEmbedBase();

                eb.WithTitle(guild.Name);
                eb.WithThumbnailUrl(guild.IconUrl);

                var moderationLogChannelName = settings.ModerationLogChannel.HasValue
                    ? MentionUtils.MentionChannel((ulong)settings.ModerationLogChannel)
                    : "None";

                eb.AddField("Moderation Log Channel", moderationLogChannelName);

                var monitoringChannelName = settings.MonitoringChannel.HasValue
                    ? MentionUtils.MentionChannel((ulong)settings.MonitoringChannel)
                    : "None";

                eb.AddField("Event Monitor Channel", monitoringChannelName);

                eb.AddField("Warning Threshold", settings.WarningThreshold);

                await _feedback.SendEmbedAsync(this.Context.Channel, eb.Build());
            }
    public async Task <Result <FeedbackMessage> > AddWarningAsync
    (
        IUser user,
        string reason,
        TimeSpan?expiresAfter = null
    )
    {
        DateTimeOffset?expiresOn = null;

        if (expiresAfter is not null)
        {
            expiresOn = DateTimeOffset.UtcNow.Add(expiresAfter.Value);
        }

        var addWarning = await _warnings.CreateWarningAsync
                         (
            _context.User.ID,
            user.ID,
            _context.GuildID.Value,
            reason,
            expiresOn : expiresOn
                         );

        if (!addWarning.IsSuccess)
        {
            return(Result <FeedbackMessage> .FromError(addWarning));
        }

        var warning     = addWarning.Entity;
        var getSettings = await _moderation.GetOrCreateServerSettingsAsync(_context.GuildID.Value);

        if (!getSettings.IsSuccess)
        {
            return(Result <FeedbackMessage> .FromError(getSettings));
        }

        var settings = getSettings.Entity;

        var notifyResult = await _logging.NotifyUserWarningAddedAsync(warning);

        if (!notifyResult.IsSuccess)
        {
            return(Result <FeedbackMessage> .FromError(notifyResult));
        }

        var warnings = await _warnings.GetWarningsAsync(user.ID);

        if (warnings.Count < settings.WarningThreshold)
        {
            return(new FeedbackMessage
                   (
                       $"Warning added (ID {warning.ID}): {warning.Reason}.", _feedback.Theme.Secondary
                   ));
        }

        var sendAlert = await _feedback.SendContextualWarningAsync
                        (
            $"The warned user now has {warnings.Count} warnings. Consider further action.",
            _context.User.ID
                        );

        return(sendAlert.IsSuccess
            ? new FeedbackMessage($"Warning added (ID {warning.ID}): {warning.Reason}.", _feedback.Theme.Secondary)
            : Result <FeedbackMessage> .FromError(sendAlert));
    }