public async Task <RuntimeResult> ModifyConditionAsync
                (
                    AutoroleConfiguration autorole,
                    long conditionID,
                    IRole role
                )
                {
                    var getCondition = _autoroles.GetCondition <RoleCondition>
                                       (
                        autorole,
                        conditionID
                                       );

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

                    var condition    = getCondition.Entity;
                    var modifyResult = await _autoroles.ModifyConditionAsync
                                       (
                        condition,
                        c =>
                    {
                        condition.RoleID = (long)role.Id;
                    }
                                       );

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

                    return(RuntimeCommandResult.FromSuccess("Condition updated."));
                }
Beispiel #2
0
                public async Task AddConditionAsync(AutoroleConfiguration autorole, IRole role)
                {
                    var condition = _autoroles.CreateConditionProxy <RoleCondition>
                                    (
                        role
                                    );

                    if (condition is null)
                    {
                        await _feedback.SendErrorAsync(this.Context, "Failed to create a condition object. Yikes!");

                        return;
                    }

                    var addCondition = await _autoroles.AddConditionAsync(autorole, condition);

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

                        return;
                    }

                    await _feedback.SendConfirmationAsync(this.Context, "Condition added.");
                }
Beispiel #3
0
        /// <summary>
        /// Explicitly denies an autorole assignment.
        /// </summary>
        /// <param name="autorole">The autorole.</param>
        /// <param name="discordUser">The user.</param>
        /// <returns>A modification result which may or may not have succeeded.</returns>
        public async Task <ModifyEntityResult> DenyAutoroleAsync
        (
            AutoroleConfiguration autorole,
            IUser discordUser
        )
        {
            if (!autorole.RequiresConfirmation)
            {
                return(ModifyEntityResult.FromError("The autorole doesn't require explicit affirmation."));
            }

            var getCondition = await GetOrCreateAutoroleConfirmationAsync(autorole, discordUser);

            if (!getCondition.IsSuccess)
            {
                return(ModifyEntityResult.FromError(getCondition));
            }

            var condition = getCondition.Entity;

            if (condition.IsConfirmed)
            {
                return(ModifyEntityResult.FromError
                       (
                           "The autorole assignment has already been denied, or has never been affirmed."
                       ));
            }

            condition.IsConfirmed = false;
            await _database.SaveChangesAsync();

            return(ModifyEntityResult.FromSuccess());
        }
                public async Task ModifyConditionAsync
                (
                    AutoroleConfiguration autorole,
                    long conditionID,
                    ITextChannel channel,
                    long count
                )
                {
                    var getCondition = _autoroles.GetCondition <MessageCountInChannelCondition>
                                       (
                        autorole,
                        conditionID
                                       );

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

                        return;
                    }

                    var condition = getCondition.Entity;

                    condition.RequiredCount = count;
                    condition.SourceID      = (long)channel.Id;

                    await _autoroles.SaveChangesAsync();

                    await _feedback.SendConfirmationAsync(this.Context, "Condition updated.");
                }
        public async Task <Result <FeedbackMessage> > ModifyConditionAsync
        (
            [DiscordTypeHint(TypeHint.Role)] AutoroleConfiguration autorole,
            long conditionID,
            long count
        )
        {
            var getCondition = Services.AutoroleService.GetCondition <MessageCountInGuildCondition>
                               (
                autorole,
                conditionID
                               );

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

            var condition    = getCondition.Entity;
            var modifyResult = await _autoroles.ModifyConditionAsync
                               (
                condition,
                c =>
            {
                c.RequiredCount = count;
                c.SourceID      = _context.GuildID.Value;
            }
                               );

            return(!modifyResult.IsSuccess
                ? Result <FeedbackMessage> .FromError(modifyResult)
                : new FeedbackMessage("Condition updated.", _feedback.Theme.Secondary));
        }
Beispiel #6
0
                public async Task <RuntimeResult> AddConditionAsync
                (
                    AutoroleConfiguration autorole,
                    [OverrideTypeReader(typeof(UncachedMessageTypeReader <IMessage>))]
                    IMessage message,
                    IEmote emote
                )
                {
                    var condition = _autoroles.CreateConditionProxy <ReactionCondition>
                                    (
                        message,
                        emote
                                    );

                    if (condition is null)
                    {
                        return(RuntimeCommandResult.FromError("Failed to create a condition object. Yikes!"));
                    }

                    var addCondition = await _autoroles.AddConditionAsync(autorole, condition);

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

                    return(RuntimeCommandResult.FromSuccess("Condition added."));
                }
Beispiel #7
0
    /// <summary>
    /// Determines whether a given user is qualified for the given autorole.
    /// </summary>
    /// <param name="autorole">The autorole.</param>
    /// <param name="userID">The user.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>true if the user qualifies; otherwise, false.</returns>
    public async Task <Result <bool> > IsUserQualifiedForAutoroleAsync
    (
        AutoroleConfiguration autorole,
        Snowflake userID,
        CancellationToken ct = default
    )
    {
        foreach (var condition in autorole.Conditions)
        {
            var isFulfilledResult = await condition.IsConditionFulfilledForUserAsync
                                    (
                _serviceProvider,
                autorole.Server.DiscordID,
                userID,
                ct
                                    );

            if (!isFulfilledResult.IsSuccess)
            {
                return(new UserError("One or more conditions were indeterminate."));
            }

            var isFulfilled = isFulfilledResult.Entity;

            if (!isFulfilled)
            {
                return(false);
            }
        }

        return(true);
    }
Beispiel #8
0
    /// <summary>
    /// Explicitly affirms an autorole assignment for all currently qualifying users for the role.
    /// </summary>
    /// <param name="autorole">The autorole.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A modification result which may or may not have succeeded.</returns>
    public async Task <Result> AffirmAutoroleForAllAsync
    (
        AutoroleConfiguration autorole,
        CancellationToken ct = default
    )
    {
        if (!autorole.RequiresConfirmation)
        {
            return(new UserError("The autorole doesn't require explicit affirmation."));
        }

        var qualifyingUsers = await _database.AutoroleConfirmations.ServersideQueryAsync
                              (
            q => q
            .Where(a => a.Autorole == autorole)
            .Where(a => !a.IsConfirmed),
            ct
                              );

        foreach (var qualifyingUser in qualifyingUsers)
        {
            qualifyingUser.IsConfirmed = true;
        }

        await _database.SaveChangesAsync(ct);

        return(Result.FromSuccess());
    }
Beispiel #9
0
    /// <summary>
    /// Removes a condition with the given ID from the given autorole.
    /// </summary>
    /// <param name="autorole">The autorole.</param>
    /// <param name="conditionID">The ID of the condition.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A modification which may or may not have succeeded.</returns>
    public async Task <Result> RemoveConditionAsync
    (
        AutoroleConfiguration autorole,
        long conditionID,
        CancellationToken ct = default
    )
    {
        var condition = autorole.Conditions.FirstOrDefault(c => c.ID == conditionID);

        if (condition is null)
        {
            return(new UserError("The autorole doesn't have any condition with that ID."));
        }

        if (autorole.Conditions.Count == 1 && autorole.IsEnabled)
        {
            return(new UserError
                   (
                       "The autorole is still enabled, so it requires at least one condition to be present. " +
                       "Either disable the role, or add more conditions."
                   ));
        }

        autorole.Conditions.Remove(condition);
        await _database.SaveChangesAsync(ct);

        return(Result.FromSuccess());
    }
Beispiel #10
0
    /// <summary>
    /// Gets a condition of the specified ID and type from the given autorole.
    /// </summary>
    /// <param name="autorole">The autorole.</param>
    /// <param name="conditionID">The ID of the condition.</param>
    /// <typeparam name="TCondition">The type of the condition.</typeparam>
    /// <returns>A retrieval result which may or may not have succeeded.</returns>
    public static Result <TCondition> GetCondition <TCondition>
    (
        AutoroleConfiguration autorole,
        long conditionID
    )
        where TCondition : AutoroleCondition
    {
        var condition = autorole.Conditions.FirstOrDefault(c => c.ID == conditionID);

        if (condition is null)
        {
            return(new UserError
                   (
                       "The autorole doesn't have any condition with that ID."
                   ));
        }

        if (condition is not TCondition autoroleCondition)
        {
            return(new UserError
                   (
                       "The condition with that ID isn't this kind of condition."
                   ));
        }

        return(autoroleCondition);
    }
Beispiel #11
0
        /// <summary>
        /// Determines whether a given user is qualified for the given autorole.
        /// </summary>
        /// <param name="autorole">The autorole.</param>
        /// <param name="user">The user.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>true if the user qualifies; otherwise, false.</returns>
        public async Task <RetrieveEntityResult <bool> > IsUserQualifiedForAutoroleAsync
        (
            AutoroleConfiguration autorole,
            IGuildUser user,
            CancellationToken ct = default
        )
        {
            foreach (var condition in autorole.Conditions)
            {
                var isFulfilledResult = await condition.IsConditionFulfilledForUserAsync(_serviceProvider, user, ct);

                if (!isFulfilledResult.IsSuccess)
                {
                    return(RetrieveEntityResult <bool> .FromError("One or more conditions were indeterminate."));
                }

                var isFulfilled = isFulfilledResult.Entity;

                if (!isFulfilled)
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #12
0
        /// <summary>
        /// Gets a condition of the specified ID and type from the given autorole.
        /// </summary>
        /// <param name="autorole">The autorole.</param>
        /// <param name="conditionID">The ID of the condition.</param>
        /// <typeparam name="TCondition">The type of the condition.</typeparam>
        /// <returns>A retrieval result which may or may not have succeeded.</returns>
        public RetrieveEntityResult <TCondition> GetCondition <TCondition>
        (
            AutoroleConfiguration autorole,
            long conditionID
        )
            where TCondition : AutoroleCondition
        {
            var condition = autorole.Conditions.FirstOrDefault(c => c.ID == conditionID);

            if (condition is null)
            {
                return(RetrieveEntityResult <TCondition> .FromError
                       (
                           "The autorole doesn't have any condition with that ID."
                       ));
            }

            if (!(condition is TCondition))
            {
                return(RetrieveEntityResult <TCondition> .FromError
                       (
                           "The condition with that ID isn't this kind of condition."
                       ));
            }

            return((TCondition)condition);
        }
Beispiel #13
0
                public async Task ModifyConditionAsync
                (
                    AutoroleConfiguration autorole,
                    long conditionID,
                    IRole role
                )
                {
                    var getCondition = _autoroles.GetCondition <RoleCondition>
                                       (
                        autorole,
                        conditionID
                                       );

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

                        return;
                    }

                    var condition = getCondition.Entity;

                    condition.RoleID = (long)role.Id;

                    await _autoroles.SaveChangesAsync();

                    await _feedback.SendConfirmationAsync(this.Context, "Condition updated.");
                }
Beispiel #14
0
    /// <summary>
    /// Sets whether the given autorole requires external affirmation.
    /// </summary>
    /// <param name="autorole">The autorole.</param>
    /// <param name="requireAffirmation">Whether external affirmation is required.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A modification result which may or may not have succeeded.</returns>
    public async Task <Result> SetAffirmationRequiredAsync
    (
        AutoroleConfiguration autorole,
        bool requireAffirmation,
        CancellationToken ct = default
    )
    {
        switch (autorole.RequiresConfirmation)
        {
        case true when requireAffirmation:
        {
            return(new UserError("The autorole already requires affirmation."));
        }

        case false when !requireAffirmation:
        {
            return(new UserError("The autorole already doesn't require affirmation."));
        }
        }

        autorole.RequiresConfirmation = requireAffirmation;
        await _database.SaveChangesAsync(ct);

        return(Result.FromSuccess());
    }
Beispiel #15
0
                public async Task ModifyConditionAsync
                (
                    AutoroleConfiguration autorole,
                    long conditionID,
                    TimeSpan time
                )
                {
                    var getCondition = _autoroles.GetCondition <TimeSinceJoinCondition>
                                       (
                        autorole,
                        conditionID
                                       );

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

                        return;
                    }

                    var condition = getCondition.Entity;

                    condition.RequiredTime = time;

                    await _autoroles.SaveChangesAsync();

                    await _feedback.SendConfirmationAsync(this.Context, "Condition updated.");
                }
Beispiel #16
0
        /// <summary>
        /// Deletes an autorole configuration from the database.
        /// </summary>
        /// <param name="autorole">The role to delete the configuration for.</param>
        /// <returns>A deletion result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteAutoroleAsync(AutoroleConfiguration autorole)
        {
            _database.Autoroles.Remove(autorole);
            await _database.SaveChangesAsync();

            return(DeleteEntityResult.FromSuccess());
        }
Beispiel #17
0
        public async Task <Result <FeedbackMessage> > ModifyConditionAsync
        (
            [DiscordTypeHint(TypeHint.Role)] AutoroleConfiguration autorole,
            long conditionID,
            IMessage message,
            IEmoji emote
        )
        {
            var getCondition = Services.AutoroleService.GetCondition <ReactionCondition>
                               (
                autorole,
                conditionID
                               );

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

            var condition    = getCondition.Entity;
            var modifyResult = await _autoroles.ModifyConditionAsync
                               (
                condition,
                c =>
            {
                c.MessageID = message.ID;
                c.EmoteName = emote.Name ?? emote.ID.ToString() ?? throw new InvalidOperationException();
            }
                               );

            return(!modifyResult.IsSuccess
                ? Result <FeedbackMessage> .FromError(modifyResult)
                : new FeedbackMessage("Condition updated.", _feedback.Theme.Secondary));
        }
Beispiel #18
0
        public async Task <Result <FeedbackMessage> > ModifyConditionAsync
        (
            [DiscordTypeHint(TypeHint.Role)] AutoroleConfiguration autorole,
            long conditionID,
            IRole role
        )
        {
            var getCondition = Services.AutoroleService.GetCondition <RoleCondition>
                               (
                autorole,
                conditionID
                               );

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

            var condition    = getCondition.Entity;
            var modifyResult = await _autoroles.ModifyConditionAsync
                               (
                condition,
                c => { c.RoleID = role.ID; }
                               );

            return(!modifyResult.IsSuccess
                ? Result <FeedbackMessage> .FromError(modifyResult)
                : new FeedbackMessage("Condition updated.", _feedback.Theme.Secondary));
        }
                public async Task <RuntimeResult> ModifyConditionAsync
                (
                    AutoroleConfiguration autorole,
                    long conditionID,
                    TimeSpan time
                )
                {
                    var getCondition = _autoroles.GetCondition <TimeSinceLastActivityCondition>
                                       (
                        autorole,
                        conditionID
                                       );

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

                    var condition    = getCondition.Entity;
                    var modifyResult = await _autoroles.ModifyConditionAsync
                                       (
                        condition,
                        c =>
                    {
                        condition.RequiredTime = time;
                    }
                                       );

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

                    return(RuntimeCommandResult.FromSuccess("Condition updated."));
                }
Beispiel #20
0
    /// <summary>
    /// Explicitly denies an autorole assignment.
    /// </summary>
    /// <param name="autorole">The autorole.</param>
    /// <param name="discordUserID">The user.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A modification result which may or may not have succeeded.</returns>
    public async Task <Result> DenyAutoroleAsync
    (
        AutoroleConfiguration autorole,
        Snowflake discordUserID,
        CancellationToken ct = default
    )
    {
        if (!autorole.RequiresConfirmation)
        {
            return(new UserError("The autorole doesn't require explicit affirmation."));
        }

        var getCondition = await GetOrCreateAutoroleConfirmationAsync(autorole, discordUserID, ct);

        if (!getCondition.IsSuccess)
        {
            return(Result.FromError(getCondition));
        }

        var condition = getCondition.Entity;

        if (!condition.IsConfirmed)
        {
            return(new UserError
                   (
                       "The autorole assignment has already been denied, or has never been affirmed."
                   ));
        }

        condition.IsConfirmed = false;
        await _database.SaveChangesAsync(ct);

        return(Result.FromSuccess());
    }
Beispiel #21
0
        /// <summary>
        /// Explicitly affirms an autorole assignment.
        /// </summary>
        /// <param name="autorole">The autorole.</param>
        /// <param name="discordUser">The user.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>A modification result which may or may not have succeeded.</returns>
        public async Task <ModifyEntityResult> AffirmAutoroleAsync
        (
            AutoroleConfiguration autorole,
            IUser discordUser,
            CancellationToken ct = default
        )
        {
            if (!autorole.RequiresConfirmation)
            {
                return(ModifyEntityResult.FromError("The autorole doesn't require explicit affirmation."));
            }

            var getCondition = await GetOrCreateAutoroleConfirmationAsync(autorole, discordUser, ct);

            if (!getCondition.IsSuccess)
            {
                return(ModifyEntityResult.FromError(getCondition));
            }

            var condition = getCondition.Entity;

            if (condition.IsConfirmed)
            {
                return(ModifyEntityResult.FromError("The autorole assignment has already been affirmed."));
            }

            condition.IsConfirmed = true;
            await _database.SaveChangesAsync(ct);

            return(ModifyEntityResult.FromSuccess());
        }
Beispiel #22
0
    /// <summary>
    /// Deletes an autorole configuration from the database.
    /// </summary>
    /// <param name="autorole">The role to delete the configuration for.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A deletion result which may or may not have succeeded.</returns>
    public async Task <Result> DeleteAutoroleAsync
    (
        AutoroleConfiguration autorole,
        CancellationToken ct = default
    )
    {
        _database.Autoroles.Remove(autorole);
        await _database.SaveChangesAsync(ct);

        return(Result.FromSuccess());
    }
Beispiel #23
0
    public async Task <Result <FeedbackMessage> > DisableAutoroleAsync
    (
        [DiscordTypeHint(TypeHint.Role)] AutoroleConfiguration autorole
    )
    {
        var disableAutorole = await _autoroles.DisableAutoroleAsync(autorole);

        return(!disableAutorole.IsSuccess
            ? Result <FeedbackMessage> .FromError(disableAutorole)
            : new FeedbackMessage("Autorole disabled.", _feedback.Theme.Secondary));
    }
        public async Task <RuntimeResult> DisableAutoroleAsync(AutoroleConfiguration autorole)
        {
            var disableAutorole = await _autoroles.DisableAutoroleAsync(autorole);

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

            return(RuntimeCommandResult.FromSuccess("Autorole disabled."));
        }
Beispiel #25
0
    public async Task <Result <FeedbackMessage> > AffirmAutoroleForAllAsync
    (
        [DiscordTypeHint(TypeHint.Role)] AutoroleConfiguration autorole
    )
    {
        var affirmResult = await _autoroles.AffirmAutoroleForAllAsync(autorole);

        return(!affirmResult.IsSuccess
            ? Result <FeedbackMessage> .FromError(affirmResult)
            : new FeedbackMessage("Qualifications confirmed.", _feedback.Theme.Secondary));
    }
Beispiel #26
0
        public async Task DisableAutoroleAsync(AutoroleConfiguration autorole)
        {
            var disableAutorole = await _autoroles.DisableAutoroleAsync(autorole);

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

            await _feedback.SendConfirmationAsync(this.Context, "Autorole disabled.");
        }
        public async Task <RuntimeResult> AffirmAutoroleForAllAsync(AutoroleConfiguration autorole)
        {
            var affirmResult = await _autoroles.AffirmAutoroleForAllAsync(autorole);

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

            return(RuntimeCommandResult.FromSuccess("Qualifications affirmed."));
        }
Beispiel #28
0
    public async Task <Result <FeedbackMessage> > DenyAutoroleForUserAsync
    (
        [DiscordTypeHint(TypeHint.Role)] AutoroleConfiguration autorole,
        IUser user
    )
    {
        var denyResult = await _autoroles.DenyAutoroleAsync(autorole, user.ID);

        return(!denyResult.IsSuccess
            ? Result <FeedbackMessage> .FromError(denyResult)
            : new FeedbackMessage("Qualification denied.", _feedback.Theme.Secondary));
    }
Beispiel #29
0
        public async Task <Result <FeedbackMessage> > RemoveConditionAsync
        (
            [DiscordTypeHint(TypeHint.Role)] AutoroleConfiguration autorole,
            long conditionID
        )
        {
            var removeCondition = await _autoroles.RemoveConditionAsync(autorole, conditionID);

            return(!removeCondition.IsSuccess
                ? Result <FeedbackMessage> .FromError(removeCondition)
                : new FeedbackMessage("Condition removed.", _feedback.Theme.Secondary));
        }
Beispiel #30
0
        /// <summary>
        /// Disables the given autorole.
        /// </summary>
        /// <param name="autorole">The autorole.</param>
        /// <returns>A modification result which may or may not have succeeded.</returns>
        public async Task <ModifyEntityResult> DisableAutoroleAsync(AutoroleConfiguration autorole)
        {
            if (!autorole.IsEnabled)
            {
                return(ModifyEntityResult.FromError("The autorole is already disabled."));
            }

            autorole.IsEnabled = false;

            await _database.SaveChangesAsync();

            return(ModifyEntityResult.FromSuccess());
        }