Example #1
0
        /// <summary>
        /// Creates a note for the given user.
        /// </summary>
        /// <param name="authorUser">The author of the note.</param>
        /// <param name="guildUser">The user.</param>
        /// <param name="content">The content of the note.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>A creation result which may or may not have succeeded.</returns>
        public async Task <CreateEntityResult <UserNote> > CreateNoteAsync
        (
            IUser authorUser,
            IGuildUser guildUser,
            string content,
            CancellationToken ct = default
        )
        {
            var getServer = await _servers.GetOrRegisterServerAsync(guildUser.Guild, ct);

            if (!getServer.IsSuccess)
            {
                return(CreateEntityResult <UserNote> .FromError(getServer));
            }

            var server = getServer.Entity;

            var getUser = await _users.GetOrRegisterUserAsync(guildUser, ct);

            if (!getUser.IsSuccess)
            {
                return(CreateEntityResult <UserNote> .FromError(getUser));
            }

            var user = getUser.Entity;

            var getAuthor = await _users.GetOrRegisterUserAsync(authorUser, ct);

            if (!getAuthor.IsSuccess)
            {
                return(CreateEntityResult <UserNote> .FromError(getAuthor));
            }

            var author = getAuthor.Entity;

            var note = _database.CreateProxy <UserNote>(server, user, author, string.Empty);

            _database.UserNotes.Update(note);

            var setContent = await SetNoteContentsAsync(note, content, ct);

            if (!setContent.IsSuccess)
            {
                return(CreateEntityResult <UserNote> .FromError(setContent));
            }

            await _database.SaveChangesAsync(ct);

            return(note);
        }
        /// <summary>
        /// Creates a warning for the given user.
        /// </summary>
        /// <param name="authorUser">The author of the warning.</param>
        /// <param name="guildUser">The user.</param>
        /// <param name="reason">The reason of the warning.</param>
        /// <param name="messageID">The message that caused the warning, if any.</param>
        /// <param name="expiresOn">The expiry date for the warning, if any.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>A creation result which may or may not have succeeded.</returns>
        public async Task <CreateEntityResult <UserWarning> > CreateWarningAsync
        (
            IUser authorUser,
            IGuildUser guildUser,
            string reason,
            long?messageID       = null,
            DateTime?expiresOn   = null,
            CancellationToken ct = default
        )
        {
            var getServer = await _servers.GetOrRegisterServerAsync(guildUser.Guild, ct);

            if (!getServer.IsSuccess)
            {
                return(CreateEntityResult <UserWarning> .FromError(getServer));
            }

            var server = getServer.Entity;

            var getUser = await _users.GetOrRegisterUserAsync(guildUser, ct);

            if (!getUser.IsSuccess)
            {
                return(CreateEntityResult <UserWarning> .FromError(getUser));
            }

            var user = getUser.Entity;

            var getAuthor = await _users.GetOrRegisterUserAsync(authorUser, ct);

            if (!getAuthor.IsSuccess)
            {
                return(CreateEntityResult <UserWarning> .FromError(getAuthor));
            }

            var author = getAuthor.Entity;

            var warning = _database.CreateProxy <UserWarning>(server, user, author, string.Empty, null, null);

            _database.UserWarnings.Update(warning);

            var setReason = await SetWarningReasonAsync(warning, reason, ct);

            if (!setReason.IsSuccess)
            {
                return(CreateEntityResult <UserWarning> .FromError(setReason));
            }

            if (!(messageID is null))
            {
                var setMessage = await SetWarningContextMessageAsync(warning, messageID.Value, ct);

                if (!setMessage.IsSuccess)
                {
                    return(CreateEntityResult <UserWarning> .FromError(setMessage));
                }
            }

            if (!(expiresOn is null))
            {
                var setExpiry = await SetWarningExpiryDateAsync(warning, expiresOn.Value, ct);

                if (!setExpiry.IsSuccess)
                {
                    return(CreateEntityResult <UserWarning> .FromError(setExpiry));
                }
            }

            await _database.SaveChangesAsync(ct);

            return(warning);
        }
Example #3
0
    /// <summary>
    /// Creates a ban for the given user.
    /// </summary>
    /// <param name="authorID">The ID of the author of the warning.</param>
    /// <param name="userID">The ID of the warned user.</param>
    /// <param name="guildID">The ID of the guild the user is on.</param>
    /// <param name="reason">The reason of the ban.</param>
    /// <param name="messageID">The message that caused the ban, if any.</param>
    /// <param name="expiresOn">The expiry date for the ban, if any.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A creation result which may or may not have succeeded.</returns>
    public async Task <Result <UserBan> > CreateBanAsync
    (
        Snowflake authorID,
        Snowflake userID,
        Snowflake guildID,
        string reason,
        Snowflake?messageID      = null,
        DateTimeOffset?expiresOn = null,
        CancellationToken ct     = default
    )
    {
        var getServer = await _servers.GetOrRegisterServerAsync(guildID, ct);

        if (!getServer.IsSuccess)
        {
            return(Result <UserBan> .FromError(getServer));
        }

        var server = getServer.Entity;

        var getUser = await _users.GetOrRegisterUserAsync(userID, ct);

        if (!getUser.IsSuccess)
        {
            return(Result <UserBan> .FromError(getUser));
        }

        var user = getUser.Entity;

        var getAuthor = await _users.GetOrRegisterUserAsync(authorID, ct);

        if (!getAuthor.IsSuccess)
        {
            return(Result <UserBan> .FromError(getAuthor));
        }

        var author = getAuthor.Entity;

        var ban = _database.CreateProxy <UserBan>(server, user, author, string.Empty, null !, null !);

        _database.UserBans.Update(ban);

        var setReason = await SetBanReasonAsync(ban, reason, ct);

        if (!setReason.IsSuccess)
        {
            return(Result <UserBan> .FromError(setReason));
        }

        if (messageID is not null)
        {
            var setMessage = await SetBanContextMessageAsync(ban, messageID.Value, ct);

            if (!setMessage.IsSuccess)
            {
                return(Result <UserBan> .FromError(setMessage));
            }
        }

        if (expiresOn is not null)
        {
            var setExpiry = await SetBanExpiryDateAsync(ban, expiresOn.Value, ct);

            if (!setExpiry.IsSuccess)
            {
                return(Result <UserBan> .FromError(setExpiry));
            }
        }

        await _database.SaveChangesAsync(ct);

        return(ban);
    }