Source code for bTagScript.block.control_block

from typing import Optional

from ..interface import verb_required_block
from ..interpreter import Context
from . import helper_parse_if, helper_parse_list_if, helper_split


def parse_into_output(payload: str, result: Optional[bool]) -> Optional[str]:
    """
    Parse a payload into an output.
    """
    if result is None:
        return
    try:
        output = helper_split(payload, False)
        if output is not None and len(output) == 2:
            if result:
                return output[0]
            else:
                return output[1]
        elif result:
            return payload
        else:
            return ""
    except:  # pylint: disable=bare-except
        return


ImplicitPPRBlock = verb_required_block(True, payload=True, parameter=True)


[docs]class AnyBlock(ImplicitPPRBlock): """ The any block checks that any of the passed expressions are true. Multiple expressions can be passed to the parameter by splitting them with ``|``. The payload is a required message that must be split by ``|``. If the expression evaluates true, then the message before the ``|`` is returned, else the message after is returned. **Usage:** ``{any(<expression|expression|...>):<message>}`` **Aliases:** ``or`` **Payload:** ``message`` **Parameter:** ``expression`` **Examples:** .. tagscript:: {any(hi=={args}|hello=={args}|heyy=={args}):Hello {user}!|How rude.} If {args} is hi Hello _Leg3ndary#0001! If {args} is what's up! How rude. """ ACCEPTED_NAMES = ("any", "or")
[docs] def process(self, ctx: Context) -> Optional[str]: """ Process the any block """ result = any(helper_parse_list_if(ctx.verb.parameter) or []) return parse_into_output(ctx.verb.payload, result)
[docs]class AllBlock(ImplicitPPRBlock): """ The all block checks that all of the passed expressions are true. Multiple expressions can be passed to the parameter by splitting them with ``|``. The payload is a required message that must be split by ``|``. If the expression evaluates true, then the message before the ``|`` is returned, else the message after is returned. **Usage:** ``{all(<expression|expression|...>):<message>}`` **Aliases:** ``and`` **Payload:** ``message`` **Parameter:** ``expression`` **Examples:** .. tagscript:: {all({args}>=100|{args}<=1000):You picked {args}.|You must provide a number between 100 and 1000.} # if {args} is 52 You must provide a number between 100 and 1000. # if {args} is 282 You picked 282. """ ACCEPTED_NAMES = ("all", "and")
[docs] def process(self, ctx: Context) -> Optional[str]: """ Process all the expressions """ result = all(helper_parse_list_if(ctx.verb.parameter) or []) return parse_into_output(ctx.verb.payload, result)
[docs]class IfBlock(ImplicitPPRBlock): """ The if block returns a message based on the passed expression to the parameter. An expression is represented by two values compared with an operator. The payload is a required message that must be split by ``|``. If the expression evaluates true, then the message before the ``|`` is returned, else the message after is returned. **Expression Operators:** +----------+--------------------------+---------+---------------------------------------------+ | Operator | Check | Example | Description | +==========+==========================+=========+=============================================+ | ``==`` | equality | a==a | value 1 is equal to value 2 | +----------+--------------------------+---------+---------------------------------------------+ | ``!=`` | inequality | a!=b | value 1 is not equal to value 2 | +----------+--------------------------+---------+---------------------------------------------+ | ``>`` | greater than | 5>3 | value 1 is greater than value 2 | +----------+--------------------------+---------+---------------------------------------------+ | ``<`` | less than | 4<8 | value 1 is less than value 2 | +----------+--------------------------+---------+---------------------------------------------+ | ``>=`` | greater than or equality | 10>=10 | value 1 is greater than or equal to value 2 | +----------+--------------------------+---------+---------------------------------------------+ | ``<=`` | less than or equality | 5<=6 | value 1 is less than or equal to value 2 | +----------+--------------------------+---------+---------------------------------------------+ **Usage:** ``{if(<expression>):<message>]}`` **Payload:** ``message`` **Parameter:** ``expression`` **Examples:** .. tagscript:: {if(63=={args}):You guessed it! The number I was thinking of was 63!|Too {if({args}<63):low|high}, try again.} If args is 63 You guessed it! The number I was thinking of was 63! If args is 73 Too low, try again. If args is 14 Too high, try again. """ ACCEPTED_NAMES = ("if",)
[docs] def process(self, ctx: Context) -> Optional[str]: """ Process the if block """ result = helper_parse_if(ctx.verb.parameter) return parse_into_output(ctx.verb.payload, result)