Пример #1
0

NICK_STORE = Kiritostore('nicknames.json', str)


def get_name(who):
    # if this entity is nicknamed, return the nickname
    if who in NICK_STORE:
        logger.info('returning nickname for {}'.format(who))
        return NICK_STORE[who]

    # otherwise return the entity's name
    return get_first_name(get_entity_cached(who))


@client.on(events.NewMessage(outgoing=True, pattern=re.compile('^!nick(.*)$')))
def on_message(event):
    if event.forward:
        return

    nickname = event.pattern_match.group(1).strip()
    who = get_target(event)
    if not who:
        return
    if nickname:
        logger.info('setting nickname for {} to {}'.format(who, nickname))
        NICK_STORE[who] = nickname
    else:
        logger.info('clearing nickname for {}'.format(who))
        del NICK_STORE[who]
    raise events.StopPropagation
Пример #2
0
    for message in client.iter_messages(event.input_chat, limit=20):
        if message.out:
            return message


def add_read_action(entity, target, action):
    if is_read(entity, target):
        action()
    else:
        read_actions.add(
            MessageAction(chat_id=get_peer_id(entity),
                          message_id=target.id,
                          action=action))


@client.on(events.NewMessage(outgoing=True, pattern=re.compile(r'^!delete$')))
def add_delete(event):
    event.delete()
    target = get_target_message(event)
    if target:
        add_read_action(
            event.input_chat, target,
            lambda: client.delete_messages(event.input_chat, target))
    raise events.StopPropagation


@client.on(events.NewMessage(outgoing=True,
                             pattern=re.compile(r'^!edit(.*)$')))
def add_edit(event):
    event.delete()
    target = get_target_message(event)
Пример #3
0
import logging
import re

from garry import events, utils

from __main__ import client
from kiritostore import Kiritostore

logger = logging.getLogger("Kiritoborg@{}".format(__name__))

snips = Kiritostore('snips.json', str)


@client.on(events.NewMessage(outgoing=True, pattern=re.compile(r'^!(\w+)$')))
def snip(event):
    snippet = snips.get(event.pattern_match.group(1))
    if snippet is not None:
        event.delete()
        client.send_message(event.input_chat,
                            snippet,
                            reply_to=event.message.reply_to_msg_id)
        raise events.StopPropagation


@client.on(
    events.NewMessage(outgoing=True, pattern=re.compile(r'^!snip (\w+)(.*)$')))
def snip_add(event):
    name = event.pattern_match.group(1)
    value = event.pattern_match.group(2).strip()

    if value:
Пример #4
0
        self.groups = groups

    def group(self, i):
        return self.groups[i]


class FakeMatcher:
    def match(self, *args, **kwargs):
        m = DEFAULT_URL_RE.match(*args, **kwargs)
        if m:
            return m
        m = SUBREDDIT_RE.match(*args, **kwargs)
        if m:
            return FakeMatch(m, '', '/' + m.group(1),
                             'reddit.com/' + m.group(1))


markdown.DEFAULT_URL_RE = FakeMatcher()

md_patterns = [DEFAULT_URL_RE, SUBREDDIT_RE]
for delimiter in markdown.DEFAULT_DELIMITERS:
    md_patterns.append(re.compile('{0}.+?{0}'.format(re.escape(delimiter))))


@client.on(events.MessageEdited(outgoing=True))
@client.on(events.NewMessage(outgoing=True))
def reparse(event):
    if any(p.search(event.raw_text) for p in md_patterns):
        event.edit(event.text, link_preview=bool(event.message.media))
        raise events.StopPropagation
Пример #5
0
faces = ['(・`ω´・)', ';;w;;', 'OwO', 'UwU', '>w<', '^w^']
patterns = [
    (r'(?i)r|l', case_matcher('w')),
    (r'(?i)(n)([aeiou])', case_matcher('{1}y{2}', 1)),
    (r'(?i)ove', case_matcher('uv')),
    (r'(?i)(?<=[^aeiou])y\b', case_matcher('ie')),
    (r'(?i)th\B', case_matcher('tw')),
    (r'(?i)too', case_matcher('twoo')),
    ('!', lambda m: ' ' + random.choice(faces))
]
patterns = [(re.compile(pattern), sub) for pattern, sub in patterns]
original_texts = {}


@client.on(events.NewMessage(outgoing=True, pattern=re.compile(r'^[^!]')))
def owo(event):
    new_text = event.text
    for pattern, sub in patterns:
        new_text = pattern.sub(sub, new_text)
    if new_text != event.text:
        original_texts[event.message.id] = event.text
        event.edit(new_text, link_preview=bool(event.message.media))
        raise events.StopPropagation


@client.on(events.MessageRead(inbox=False))
def ninja(event):
    edited = []
    for message_id in original_texts:
        if event.is_read(message_id):