Пример #1
0
    def translate(self, channel=None, function=None):
        """Register a translation function

        (decorator) Will register the decorated function in the series of
        translation functions that get called on message posts.  These
        functions are meant for modifying messages in specific channels (or
        all channels).

        :param channel: single or list of channel names to apply this
         translator to

        Usage: ::

            @bot.translate(channel="#bot_test")
            def self_reference(text):
                return text.replace(bot.user, "me")

        """
        if channel is None:
            channel = current_plugin().channels

        if not isinstance(channel, list) and channel is not None:
            channel = [channel]

        def decorated(function):
            self._translations.append((channel, function))
            return function

        return decorated(function) if function else decorated
Пример #2
0
    def listen(self, filter, channel=None, regex=False):
        """Register a message event listener

        (decorator) Will register the decorated function to be called for all
        Message events that match the filter settings. The filter should be a
        string that will either be a literal match (default), a ``*`` to match
        any string, or a custom regex string (with the ``regex`` flag to True),
        see <regex logic> for more information on the options for the string.
        The filter will be checked for all message events that happen and if
        the logic passes, the data will be passed to the decorated function.

        For more information see #Filter#

        :param filter: message format to match against the message text
        :param channel: list of channel names that this filter should check
         against
        :param regex: boolean flag on whether the filter text should be
         treated as a regex string

        Usage: ::

            @bot.listen("@me: hi")
            def greetings(user, channel):
                bot.post(channel, text="{!s}, greetings".format(user))

        """
        if channel is None:
            channel = current_plugin().channels

        def decorated(function):
            if isinstance(function, Filter):
                function.add_filter(filter, channels=channel, regex=regex)
                return function
            else:
                new_filter = wraps(function)(Filter(
                    match_txt=filter,
                    handler=function,
                    channels=channel,
                    regex=regex
                ))
                self._hooks[events.MESSAGE].append(new_filter)
                return new_filter

        return decorated