/// <summary>
    /// Creates the settings for the given Discord guild.
    /// </summary>
    /// <param name="guildID">The ID of the guild.</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 <ServerModerationSettings> > CreateServerSettingsAsync
    (
        Snowflake guildID,
        CancellationToken ct = default
    )
    {
        var existingEntity = await GetServerSettingsAsync(guildID, ct);

        if (existingEntity.IsSuccess)
        {
            return(new UserError("That server already has settings."));
        }

        var getServer = await _servers.GetOrRegisterServerAsync(guildID, ct);

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

        var server = getServer.Entity;

        var settings = _database.CreateProxy <ServerModerationSettings>(server);

        _database.ServerSettings.Update(settings);

        await _database.SaveChangesAsync(ct);

        return(settings);
    }
Example #2
0
        /// <summary>
        /// Creates the settings for the given Discord guild.
        /// </summary>
        /// <param name="discordServer">The server.</param>
        /// <returns>A creation result which may or may not have succeeded.</returns>
        public async Task <CreateEntityResult <ServerModerationSettings> > CreateServerSettingsAsync
        (
            IGuild discordServer
        )
        {
            var existingEntity = await GetServerSettingsAsync(discordServer);

            if (existingEntity.IsSuccess)
            {
                return(CreateEntityResult <ServerModerationSettings> .FromError("That server already has settings."));
            }

            var getServer = await _servers.GetOrRegisterServerAsync(discordServer);

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

            var server   = getServer.Entity;
            var settings = new ServerModerationSettings(server);

            _database.ServerSettings.Update(settings);
            await _database.SaveChangesAsync();

            return(settings);
        }
Example #3
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>
        /// <returns>A creation result which may or may not have succeeded.</returns>
        public async Task <CreateEntityResult <UserNote> > CreateNoteAsync
        (
            IUser authorUser,
            IGuildUser guildUser,
            string content
        )
        {
            var getServer = await _servers.GetOrRegisterServerAsync(guildUser.Guild);

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

            var server = getServer.Entity;

            var getUser = await _users.GetOrRegisterUserAsync(guildUser);

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

            var user = getUser.Entity;

            var getAuthor = await _users.GetOrRegisterUserAsync(authorUser);

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

            var author = getAuthor.Entity;

            var note = new UserNote(server, user, author, string.Empty);

            var setContent = await SetNoteContentsAsync(note, content);

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

            _database.UserNotes.Update(note);

            await _database.SaveChangesAsync();

            // Requery the database
            var getNote = await GetNoteAsync(guildUser.Guild, note.ID);

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

            return(CreateEntityResult <UserNote> .FromSuccess(getNote.Entity));
        }
Example #4
0
    /// <summary>
    /// Creates a note 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="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 <Result <UserNote> > CreateNoteAsync
    (
        Snowflake authorID,
        Snowflake userID,
        Snowflake guildID,
        string content,
        CancellationToken ct = default
    )
    {
        var getServer = await _servers.GetOrRegisterServerAsync(guildID, ct);

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

        var server = getServer.Entity;

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

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

        var user = getUser.Entity;

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

        if (!getAuthor.IsSuccess)
        {
            return(Result <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(Result <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 #6
0
        /// <summary>
        /// Creates a ban for the given user.
        /// </summary>
        /// <param name="authorUser">The author of the ban.</param>
        /// <param name="guildUser">The user.</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>
        /// <returns>A creation result which may or may not have succeeded.</returns>
        public async Task <CreateEntityResult <UserBan> > CreateBanAsync
        (
            IUser authorUser,
            IGuildUser guildUser,
            string reason,
            long?messageID     = null,
            DateTime?expiresOn = null
        )
        {
            var getServer = await _servers.GetOrRegisterServerAsync(guildUser.Guild);

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

            var server = getServer.Entity;

            var getUser = await _users.GetOrRegisterUserAsync(guildUser);

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

            var user = getUser.Entity;

            var getAuthor = await _users.GetOrRegisterUserAsync(authorUser);

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

            var author = getAuthor.Entity;

            var ban = new UserBan(server, user, author, string.Empty);

            var setReason = await SetBanReasonAsync(ban, reason);

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

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

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

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

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

            _database.UserBans.Update(ban);

            await _database.SaveChangesAsync();

            // Requery the database
            var getBan = await GetBanAsync(guildUser.Guild, ban.ID);

            if (!getBan.IsSuccess)
            {
                return(CreateEntityResult <UserBan> .FromError(getBan));
            }

            return(CreateEntityResult <UserBan> .FromSuccess(getBan.Entity));
        }
Example #7
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);
    }
        /// <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>
        /// <returns>A creation result which may or may not have succeeded.</returns>
        public async Task <CreateEntityResult <UserWarning> > CreateWarningAsync
        (
            [NotNull] IUser authorUser,
            [NotNull] IGuildUser guildUser,
            [NotNull] string reason,
            [CanBeNull] long?messageID     = null,
            [CanBeNull] DateTime?expiresOn = null
        )
        {
            var getServer = await _servers.GetOrRegisterServerAsync(guildUser.Guild);

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

            var server = getServer.Entity;

            var getUser = await _users.GetOrRegisterUserAsync(guildUser);

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

            var user = getUser.Entity;

            var getAuthor = await _users.GetOrRegisterUserAsync(authorUser);

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

            var author = getAuthor.Entity;

            var warning = new UserWarning(server, user, author, string.Empty);

            var setReason = await SetWarningReasonAsync(warning, reason);

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

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

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

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

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

            _database.UserWarnings.Update(warning);

            await _database.SaveChangesAsync();

            // Requery the database
            var getWarning = await GetWarningAsync(guildUser.Guild, warning.ID);

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

            return(CreateEntityResult <UserWarning> .FromSuccess(getWarning.Entity));
        }