示例#1
0
    /// <summary>
    /// Grants consent to store user data for a given user.
    /// </summary>
    /// <param name="discordUser">The user that has granted consent.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A task that must be awaited.</returns>
    public async Task <Result <UserConsent> > GrantUserConsentAsync
    (
        Snowflake discordUser,
        CancellationToken ct = default
    )
    {
        var getConsent = await GetUserConsentAsync(discordUser, ct);

        UserConsent userConsent;

        if (!getConsent.IsSuccess)
        {
            userConsent = _database.CreateProxy <UserConsent>(discordUser);
            _database.UserConsents.Update(userConsent);

            userConsent.HasConsented = true;
        }
        else
        {
            userConsent = getConsent.Entity;
            userConsent.HasConsented = true;
        }

        await _database.SaveChangesAsync(ct);

        return(userConsent);
    }
示例#2
0
        /// <summary>
        /// Adds a Discord user to the database.
        /// </summary>
        /// <param name="discordUser">The Discord user.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>The freshly created information about the user.</returns>
        /// <exception cref="ArgumentException">Thrown if the user already exists in the database.</exception>
        public async Task <RetrieveEntityResult <User> > AddUserAsync
        (
            IUser discordUser,
            CancellationToken ct = default
        )
        {
            if (discordUser.IsBot || discordUser.IsWebhook)
            {
                return(RetrieveEntityResult <User> .FromError
                       (
                           "Users cannot be viewed or created for bots or webhooks."
                       ));
            }

            if (await IsUserKnownAsync(discordUser, ct))
            {
                return(RetrieveEntityResult <User> .FromError
                       (
                           $"A user with the ID {discordUser.Id} has already been added to the database."
                       ));
            }

            var newUser = _database.CreateProxy <User>((long)discordUser.Id);

            _database.Users.Update(newUser);
            await _database.SaveChangesAsync(ct);

            return(newUser);
        }
示例#3
0
    /// <summary>
    /// Adds a Discord user to the database.
    /// </summary>
    /// <param name="discordUser">The Discord user.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>The freshly created information about the user.</returns>
    /// <exception cref="ArgumentException">Thrown if the user already exists in the database.</exception>
    public async Task <Result <User> > AddUserAsync
    (
        Snowflake discordUser,
        CancellationToken ct = default
    )
    {
        if (await IsUserKnownAsync(discordUser, ct))
        {
            return(new UserError
                   (
                       $"A user with the ID {discordUser} has already been added to the database."
                   ));
        }

        var newUser = _database.CreateProxy <User>(discordUser);

        _database.Users.Update(newUser);
        await _database.SaveChangesAsync(ct);

        return(newUser);
    }
        /// <summary>
        /// Adds a Discord server to the database.
        /// </summary>
        /// <param name="discordServer">The Discord server.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>The freshly created information about the server.</returns>
        /// <exception cref="ArgumentException">Thrown if the server already exists in the database.</exception>
        public async Task <RetrieveEntityResult <Server> > AddServerAsync
        (
            IGuild discordServer,
            CancellationToken ct = default
        )
        {
            if (await IsServerKnownAsync(discordServer, ct))
            {
                return(RetrieveEntityResult <Server> .FromError
                       (
                           $"A server with the ID {discordServer.Id} has already been added to the database."
                       ));
            }

            var server = _database.CreateProxy <Server>((long)discordServer.Id);

            _database.Servers.Update(server);

            server.IsNSFW = true;

            await _database.SaveChangesAsync(ct);

            return(RetrieveEntityResult <Server> .FromSuccess(server));
        }