Пример #1
0
    def handle_event(self, event):
        """Handle a slack event."""
        if event["type"] != "message" or "subtype" in event:
            # not interested in non messages
            return

        logger.debug("Handling message event: {}".format(event))

        if is_direct_mention(event["text"]):
            logger.debug("Handling command")
            user_id, message = parse_direct_mention(event["text"])
            if user_id == self.bot_id:
                self.handle_command(message)
        if is_invocation(event):
            logger.debug("Handling invocation")

            term, url = parse_invocation(event)
            logger.debug("term: {}, url: {}".format(term, url))

            error = is_violation(self.previous_term, term)
            if error is not None:
                self.send_message("Infraction: {}".format(error))
                return

            if url == self.previous_url:
                self.send_message("Giphysnap!")
                self.reset_state()
            else:
                self.previous_term = term
                self.previous_url = url

        logger.debug("Game State: {}, {}, {}".format(self.previous_player,
                                                     self.previous_term,
                                                     self.previous_url))
Пример #2
0
 def test_returns_error_if_illegal_word_used(self):
     """Returns error if an illegal word was used."""
     self.assertEqual(is_violation("one two", "three swift"), ILLEGAL_WORD)
Пример #3
0
 def test_returns_error_if_any_words_match(self):
     """Returns error if any of the words match the previous term."""
     self.assertEqual(is_violation("one two", "two three"), REPEAT_WORD)
Пример #4
0
 def test_returns_error_if_not_two_words(self):
     """Returns error if `current_term` is not 2 words."""
     self.assertEqual(is_violation("foo", "one two three"), TERM_LENGTH)
     self.assertEqual(is_violation("foo", "one"), TERM_LENGTH)
Пример #5
0
 def test_returns_error_if_repeats_last_turn(self):
     """Returns error if `previous_term` is the same as `current_term`."""
     self.assertEqual(is_violation("foo", "foo"), DUPLICATE_TERM)