Example #1
0
def text2malk(text, prob=0.5):
    if '```' in text:
        return text
    # code style look bad on mobile devices # '`', '**`', '_`', '_`',
    f_set = {
        '**',
        '***',
        '_',
        '_',
    }  # '~~', # italic more often then others
    esc = {
        '*',
        '_',
        '~',
        '`',
        '|',
        '\\',
    }
    dont_touch = {
        '#', '@', '&', '<', '>', '0', '1', '2', '3', '4', '5', '6', '7', '8',
        '9'
    }
    was_esc = False
    last_f = '0'

    new_text = []
    for i, symb in enumerate(text):  # type: int, str
        if symb in string__whitespace or symb in dont_touch:
            new_text.append(symb)
            continue
        elif was_esc and symb in esc:
            was_esc = False
            new_text.append(symb)
            last_f = symb
            continue
        elif symb == '\\':
            next_symb = text[i + 1:]
            if next_symb and next_symb[0] in esc:
                was_esc = True
                new_text.append(symb)
            continue
        elif symb in esc:
            continue

        was_esc = False
        if symb != '.' and other.rand() <= prob:
            prob2 = other.rand()
            new_s = symb.upper() if prob2 <= 0.5 else symb
            if prob2 > 0.1:
                f = other.choice({s for s in f_set if s[0] != last_f[0]})
                last_f = f
                new_text.append(f + new_s + f[::-1])
            else:
                last_f = new_s
                new_text.append(new_s)
        else:
            last_f = symb
            new_text.append(symb)

    return ''.join(new_text)
Example #2
0
def text2leet(text, prob=0.25):
    new_text = []
    esc = {
        '*',
        '_',
        '~',
        '`',
        '|',
        '\\',
    }
    dont_touch = {
        '#', '@', '&', '<', '>', '0', '1', '2', '3', '4', '5', '6', '7', '8',
        '9'
    }
    for symb in text:
        if symb in data_com.dct_leet and symb not in dont_touch and other.rand(
        ) <= prob:
            new_s = other.choice(data_com.dct_leet[symb])  # type: str
            esc_s = esc.intersection(new_s)
            if esc_s and symb not in esc:
                for s in esc_s:
                    new_s = new_s.replace(s, '\\' + s)
        else:
            new_s = symb
        new_text.append(new_s)
    return ''.join(new_text)
Example #3
0
def just_embrace_say(user, clan_name, get_text=False):
    if not (user or clan_name):
        return False

    pander = (clan_name == 'Pander')
    sir = None
    if not pander:
        clan_users = set()
        not_sire_ids = {user.id}.union(C.not_sir).union(ram.embrace_not)
        for mem in C.client.get_all_members():
            if other.find(mem.roles, id=C.roles[clan_name]) and mem.id not in not_sire_ids:
                clan_users.add(mem.id)
        if clan_users:
            embr_first = [userid for userid in ram.embrace_first if userid in clan_users]
            if embr_first:
                sir = embr_first[0]
                ram.embrace_first.remove(sir)
            else:
                sir = other.choice(list(clan_users))
            text = com.get_t('embrace_msg', sir=f'<@{sir}>', child=f'<@{user.id}>')
        else:
            text = com.get_t('embrace_unic', clan=f'<@&{C.roles[clan_name]}>', child=f'<@{user.id}>')
    else:
        text = com.get_t('embrace_pander', child=f'<@{user.id}>')

    if clan_name in C.sabbat_clans and not pander:
        text += "\n" + com.get_t('embrace_sabbat')

    if get_text:
        return text
    else:
        com.write_msg(C.main_ch, text)
        return sir
Example #4
0
def phrase_gt(gt=None, uid=None, add_id=None):
    if not gt:
        return False

    uid = uid or 'here'
    phr = other.choice(d2u.good_time[gt['g_key']][gt['g_type']]['response'])
    str_weather = ''
    if gt['g_key'] == 'g_morn':  # and uid in emj.morn_add:
        smile_ids = other.it2list(uid) + other.it2list(add_id)
        smiles = []
        for sm_id in smile_ids:
            smiles += emj.morn_add.get(sm_id, [])
        if smiles:
            phr += ' ' + other.choice(smiles)
    if uid == C.users['Natali'] and gt['g_key'] in ('g_morn', 'g_day'):
        try:
            log.I('try get_weather for Natali')
            str_weather = '\n:newspaper: ' + get_weather()
        except Exception as e:
            other.pr_error(e, 'get_weather')
    return other.name_phr(uid, phr) + str_weather
Example #5
0
async def just_embrace(user, clan_name=None):
    if not user:
        return False

    if not clan_name:
        for r in user.roles:
            if r.id in C.clan_ids:
                clan_name = C.role_by_id[r.id]
                break
    clan_name = clan_name or other.choice(C.clan_names)
    roles = [r for r in {other.find(C.vtm_server.roles, id=C.roles[clan_name])} if r]
    if clan_name in C.sabbat_clans:
        roles.append(other.find(C.vtm_server.roles, id=C.roles['Sabbat']))

    other.add_roles(user, roles, 'do_embrace')

    return clan_name
Example #6
0
async def on_member_update_u(before: C.Types.Member, after: C.Types.Member):
    # it's triggers on changing status, game playing, avatar, nickname or roles
    #
    if after.id == C.users['bot']:
        return

    smth_happend = False
    a_n = other.uname(after) + f' [{after.id}]'

    if before.display_name != after.display_name or before.name != after.name:
        smth_happend = True
        b_n = other.uname(before)
        log.pr_news(f'<on_member_update> {b_n} change nickname to {a_n}.')

    if before.game != after.game:
        smth_happend = True
        if before.game and after.game:
            log.D(
                f'<on_member_update> {a_n} go play from {before.game.name} to {after.game.name}.'
            )
        elif before.game:
            log.D(f'<on_member_update> {a_n} stop play {before.game.name}.')
        elif after.game:
            log.D(f'<on_member_update> {a_n} start play {after.game.name}.')
        else:
            log.D(
                f'<on_member_update> {{???}} {a_n} - game change, but there are no games...'
            )

        if after.id == C.users['Natali']:
            if before.game and C.prm_server.me.game.name == before.game.name:
                await other.set_game('')
            if after.game and not C.prm_server.me.game:
                await other.set_game(after.game.name)

        user_g = user_games.pop(after.id, {'name': '', 'h': 0})
        # degradation
        if False and other.rand() < 0.5 and (
                before.game and before.game.name
                and after.id not in ram.ignore_users and people.was_writing(
                    after.id, 48) and user_g['h'] >= TMR_IN_H):
            phr = com.get_t('game',
                            user=f'<@{after.id}>',
                            game=f"«{user_g['name']}»")
            com.write_msg(C.main_ch, phr)

        if after.game and after.game.name:
            user_games[after.id] = {'name': after.game.name, 'h': 0}

    if before.avatar_url != after.avatar_url:
        smth_happend = True
        urls = []
        for url in (before.avatar_url, after.avatar_url):
            urls.append(' ?'.join(url.split('?', maxsplit=1)))
        b_url, a_url = urls

        if before.avatar_url and after.avatar_url:
            await log.pr_avs(
                f'<on_member_update> {a_n} change avatar from \n{a_url} \nto\n{b_url}'
            )
        elif before.avatar_url:
            await log.pr_avs(
                f'<on_member_update> {a_n} delete avatar: \n{b_url}')
        elif after.avatar_url:
            await log.pr_avs(f'<on_member_update> {a_n} set avatar: \n{a_url}')
        else:
            log.I(
                f'<on_member_update> {{???}} {a_n} - avatar change, but there are no avatar_urls...'
            )

        # small degradation
        if (other.rand() < 0.01 and after.avatar_url
                and after.id not in ram.ignore_users
                and people.was_writing(after.id, 48)):
            phr = com.get_t('new_avatar', user=f'<@{after.id}>')
            com.write_msg(C.main_ch, phr)

    if before.roles != after.roles:
        smth_happend = True
        old_roles = [('@&' + r.name) for r in before.roles
                     if r not in after.roles]
        new_roles = [('@&' + r.name) for r in after.roles
                     if r not in before.roles]
        if old_roles:
            log.pr_news(
                f'<on_member_update> {a_n} lost role(s): {", ".join(old_roles)}.'
            )
        if new_roles:
            log.pr_news(
                f'<on_member_update> {a_n} get role(s): {", ".join(new_roles)}.'
            )
            new_role_ids = {r.id for r in after.roles if r not in before.roles}
            new_clan_roles = C.clan_ids.intersection(new_role_ids)
            has_clan_before = other.has_roles(before, C.clan_ids)
            if after.id not in not_embrace and new_clan_roles and not has_clan_before:
                clan_id = other.choice(new_clan_roles)
                clan_name = C.role_by_id[clan_id]
                log.pr_news(
                    f'<on_member_update> {a_n} get new clan role "{clan_name}" => call do_embrace.'
                )
                manager.just_embrace_say(after, clan_name=clan_name)
                # if sir_id:
                #     if clan_id in C.clan_channels:
                #         clan_ch = C.clan_channels[clan_id]
                #         phr = com.get_t(all_keys=('clan_welcome', clan_ch), user=f'<@{after.id}>', sir=f'<@{sir_id}>')
                #         com.write_msg(clan_ch, phr)

            elif has_clan_before and C.roles['Pander'] in new_clan_roles:
                log.jI(
                    f'<on_member_update> {a_n} go to Pander => delete other clan roles if it\'s exist.'
                )
                del_clans_id = C.clan_ids.difference({C.roles['Pander']})
                rem_roles = {r for r in after.roles if r.id in del_clans_id}
                if rem_roles:
                    other.rem_roles(after, rem_roles, 'on_member_update_u[1]')
                    str_rem_r = f"<@&{'>, <@&'.join(r.id for r in rem_roles)}>"
                    phr = com.get_t('to_Pander',
                                    user=f'<@{after.id}>',
                                    old_clans=str_rem_r,
                                    pander=f"<@&{C.roles['Pander']}>")
                    com.write_msg(C.main_ch, phr)
            elif C.roles['Mortal'] in new_role_ids:
                if has_clan_before:
                    # go to food
                    rem_roles = {
                        r
                        for r in after.roles
                        if r.id not in C.mortal_can_have_roles
                    }
                    other.rem_roles(after, rem_roles, 'go to Mortal(food)')
                    log.pr_news(f'<on_member_update> {a_n} go to food')
                else:
                    # new user
                    await C.client.send_message(C.main_ch,
                                                com.welcome_msg(before.id))
                    log.pr_news(f'<on_member_update> {a_n} welcome')

            if C.roles['Sabbat'] in new_role_ids:
                clan_ch = C.clan_channels[C.roles['Sabbat']]
                phr = com.get_t(all_keys=('clan_welcome', clan_ch),
                                user=f'<@{after.id}>')
                com.write_msg(clan_ch, phr)

    if before.status != after.status or not smth_happend:
        people.online_change(after.id,
                             after.status,
                             force=before.status == after.status)
        # small degradation
        _check_status_change(after)

    if (smth_happend
            or people.is_online(after.id)) and before.roles == after.roles:
        on_user_life_signs(after.id)
Example #7
0
def get_text_obj(any_keys=None, all_keys=None):
    any_keys = {any_keys} if isinstance(
        any_keys, str) else (set(any_keys) if any_keys else set())
    all_keys = {all_keys} if isinstance(
        all_keys, str) else (set(all_keys) if all_keys else set())
    any_k = (any_keys if any_keys else all_keys).copy()
    error_ans = {'text': '', 'last_key': ''}

    if not any_k:
        # log.E('<com.get_text_obj[1]> There are no keys!')
        return error_ans

    texts = set()
    for k in any_k:
        texts.update({
            h
            for h in d2u.resp_values.get(k, set())
            if d2u.resp_data[h]['keys'].issuperset(all_keys)
        })

    if not texts:
        # log.E('<com.get_text_obj[2]> There are no keys!')
        return error_ans

    not_used_texts = texts.difference(d2u.data_used)
    # log.D(f'<com.get_text_obj> txts: {len(texts)}, not_used_t: {len(not_used_texts)}, data_used: {len(d2u.data_used)}.')

    if not not_used_texts:
        # log.D(f'<com.get_text_obj> answers for any({", ".join(any_keys)}) and all({", ".join(all_keys)}) '
        #       f'were all used, clear this texts in data_used.')
        d2u.data_used = [k for k in d2u.data_used if k not in texts
                         ]  # we need order in data_used, so not .difference
        not_used_texts = texts

    ans_h = other.choice(not_used_texts)
    ans = d2u.resp_data[ans_h]
    last_key = other.choice(
        ans['keys'].intersection(any_keys if any_keys else all_keys))
    ans['last_key'] = last_key

    if not_used_texts != texts:
        for k in ans['keys']:
            topic_t = d2u.resp_values[k]
            topic_unused = topic_t.difference(d2u.data_used)
            if len(topic_t) == len(topic_unused):
                continue
            count_unused = len(topic_unused) - 1
            # log.D(f'<com.get_text_obj> topic_t: {len(topic_t)}, count_unused: {count_unused}.')
            # if left one phrase -> free all of used
            if count_unused < 2:
                # log.D(f'<com.get_text_obj> free all topic_t in data_used ')
                d2u.data_used = [k for k in d2u.data_used if k not in topic_t]
            # if left less then 1/4 of phrases -> free half (early) of used
            elif count_unused <= len(topic_t) >> 2:
                # log.D(f'<com.get_text_obj> free half topic_t in data_used ')
                # cycle by data_used, because we need order by time of adding
                old_data = [k for k in d2u.data_used if k in topic_t]
                data_to_free = old_data[
                    0:len(old_data) >> 1]  # early half of used
                d2u.data_used = [
                    k for k in d2u.data_used if k not in data_to_free
                ]

    d2u.data_used.append(ans_h)
    # log.D(f'<com.get_text_obj> data_used: {repr(d2u.data_used)}')
    return ans