예제 #1
0
파일: model.py 프로젝트: acmfi/discord-bot
    def __init__(self, user_message, question, flags):
        """
        It will create the poll model. It will be invoked from MultipleOptionPollModel or YesOrNoPollModel. 

        Args:
            user_message (Message):     A discord class for the messages containing the original message  
            question (string):          Question of the poll
            flags (PollFlagsCommand[]): Flags for the poll containing
        """

        self.user_message = user_message
        self.question = question
        self.options = []
        self.flags = flags
        self.poll_str = ""
        self.bot_message = None
        self.created_at = time.time()
        self.is_active = False

        time_flag = list(
            filter(lambda flag: flag.argument == "time", self.flags))
        if len(time_flag) == 0:
            # Check if no-time flag is set
            no_time_flag = list(
                filter(lambda flag: flag.argument == "no-time", self.flags))
            # If no-time flag is set, duration is -1 otherwise we use default value for duration for the poll
            self.duration = PollFlagsCommand.parse_flag_value(
                None, "time",
                DEFAULT_DURATION) if len(no_time_flag) == 0 else -1
        else:
            self.duration = time_flag[0].value
예제 #2
0
    def parser(self, input_args, message):
        """
        Given the input_args, it will create the appropriate PollModel. If the poll contains more than two options, 
        then it will create a MultipleOptionPollModel. If the options are empty, then it will create a YesOrNoPollModel.
        If the options for the poll are other, a InvalidInputException will be raised. The library getopt will be used
        for parsing the flags.
        An example of empty options array would be: /poll --time 10m "Is JS the best language"
        An example of options array filled would be: /poll --time 10m "Is JS the best language" "Yes" "No" "I don't know JS"


        Args:
            input_args (string[]):  Array with the content given by the user. As example:
                                        User inputs: /poll --time 10m "Is JS the best language" (In this case options are empty)
                                        input_args will contain:
                                        input_args = ["/poll", "--time", "10m", "Is JS the best language"]
            message (Message):      A discord class for the messages containing the original message 
        Returns:
            PollModel: A poll model object
        """

        flags = PollFlagsCommand._FLAGS

        if len(input_args) == 1 and input_args[0] == "/poll":
            raise InvalidInputException()

        try:
            flags_input, args = getopt.getopt(
                input_args, "", ["help"] +
                [f + "=" if flags[f]["needs_value"] else f for f in flags])
        except getopt.GetoptError:
            raise InvalidFlagException()

        flags_name = [f[0] for f in flags_input]
        if "--help" in flags_name:
            return None, True
        if "--time" in flags_name and "--no-time" in flags_name:
            raise InvalidFlagException(
                "Cannot use --time and --no-time at the same time")

        # Flags given by the user
        user_flags = []
        for (k, v) in flags_input:
            f = flags[k[2:]]
            flags_name.append(k[2:])
            user_flags.append(
                PollFlagsCommand(
                    f["needs_value"], k[2:], f["description"], f["examples"],
                    v, f["default_value"] if "default_value" in f else None))

        if len(args) in [0, 2] or len(args[1:]) > 10:
            raise InvalidInputException()
        if len(args) == 1:
            return YesOrNoPollModel(message, args[0], user_flags), None
        else:
            return MultipleOptionPollModel(message, args[0], args[1:],
                                           user_flags), None
예제 #3
0
def get_variables(question, options, given_flags=[], need_expected_poll=True):
    flags = [
        PollFlagsCommand(f[1] != '', f[0], "", "",
                         None if f[1] == '' else f[1]) for f in given_flags
    ]
    discord_format_args = get_discord_format_args(question, options, flags)
    ctx = get_ctx(question, options, flags)
    poll, is_help = PollCommand().parser(discord_format_args, ctx.message)
    if need_expected_poll:
        expected_poll = get_expected_poll(question, options, flags)
        return poll, is_help, expected_poll
    else:
        return poll, is_help
예제 #4
0
def test_invalid_flag2():
    flag = PollFlagsCommand(True, "", "", [], None, "default_value")
    assert flag.value_input == "default_value"
예제 #5
0
def test_invalid_flag1():
    with pytest.raises(InvalidFlagException):
        PollFlagsCommand(True, "", "", [], None, None)
예제 #6
0
def test_flags5():
    with pytest.raises(InvalidFlagException):
        PollFlagsCommand.parse_flag_value(None, "time", "8ns")
예제 #7
0
def test_flags4():
    assert PollFlagsCommand.parse_flag_value(None, "time",
                                             "1d") == 1 * 24 * 60 * 60
예제 #8
0
def test_flags3():
    assert PollFlagsCommand.parse_flag_value(None, "time", "3h") == 3 * 60 * 60
예제 #9
0
def test_flags2():
    assert PollFlagsCommand.parse_flag_value(None, "time", "2m") == 2 * 60
예제 #10
0
def test_flags1():
    assert PollFlagsCommand.parse_flag_value(None, "time", "57s") == 57