def add(memb, status=''): """ :param C.Types.Member memb: :param str status: :return Usr: """ return Usr(memb.id, name=other.uname(memb), status=status, last_m=other.get_sec_total())
def comeback(self, memb=None, res=False): global usrs nm = self.name m = memb or other.find_member(C.vtm_server, self.id) if m: nm = other.uname(m) # if self.role != '0': # role = other.find(C.vtm_server.roles, id=self.role) # if role: # other.add_roles(m, role, 'comeback', 5) self.status = 'del' usr = Usr(self.id, name=nm, karma=self.karma, status='add', last_m=other.get_sec_total()) if res: return usr else: usrs[self.id] = usr
async def format_mess(msg, time=False, date=False, dbase=None): """ :type msg: C.Types.Message :type time: Bool :type date: Bool :type dbase: dict :rtype: str """ try: t_m = other.t2s(msg.timestamp) t_n = other.t2s() s_time = ('(from {0})'.format(t_m) if (time or (t_n[:-1] != t_m[:-1]) or (int(t_n[-1]) - int(t_m[-1]) > 1)) else '') if msg.channel.is_private: ch_name = '@#' + str(msg.channel.user) else: every_prm = msg.channel.overwrites_for(msg.server.default_role) if msg.server.id == C.vtm_server.id and every_prm.read_messages is False: ch_name = '##' + str(msg.channel.name) else: ch_name = '#' + str(msg.channel.name) t = ('(from {0})'.format(other.t2s(msg.timestamp, '%d|%m|%y %T')) if date else s_time) cont = msg.content or ( ('≤System≥ ' + msg.system_content) if msg.system_content else '') cont = cont.replace('\n', '\n\t') # type: str db = dbase if dbase is not None else {} # we need a=={} if it is if msg.author.id not in db: db[msg.author.id] = msg.author if msg.raw_mentions: for uid in msg.raw_mentions: if uid in db: usr = db[uid] else: usr = other.find_member(msg.server, uid) or other.find_user(uid) if not usr: usr = await C.client.get_user_info(uid) if usr: db[uid] = usr else: continue usr_name = other.uname(usr) cont = cont.replace('<@' + usr.id + '>', usr_name).replace('<@!' + usr.id + '>', usr_name) if msg.raw_channel_mentions: for chid in msg.raw_channel_mentions: ch = C.client.get_channel(chid) if ch: cont = cont.replace('<#' + ch.id + '>', '#' + str(ch)) if msg.raw_role_mentions and msg.server: for role_id in msg.raw_role_mentions: role = other.find(msg.server.roles, id=role_id) if not role: for s in C.client.servers: if s.id == msg.server.id: continue role = other.find(s.roles, id=role_id) if role: break if role: cont = cont.replace('<@&' + role.id + '>', '&' + str(role)) a_n = other.uname(msg.author) return '{t}<{ch}> {author}: {cont}'.format(t=t, ch=ch_name, author=a_n, cont=cont) except Exception as e: other.pr_error(e, 'log.format_mess', 'format_mess[253] error') return '<format error> ' + (msg.content or ( ('≤System≥ ' + msg.system_content) if msg.system_content else ''))
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)
async def check_now(): log.I('- start check people') s_mems = set() # noinspection PyTypeChecker for mem in C.vtm_server.members: # type: C.Types.Member s_mems.add(mem.id) uname = other.uname(mem) if mem.id not in usrs: if Usr.check_new(mem): if gone[mem.id].ban: log.pr_news(f'New user {uname} from ban!') else: log.pr_news(f'New user {uname} from gone!') else: log.pr_news(f'New user {uname}!') elif usrs[mem.id].name != uname: usrs[mem.id].set(name=uname, status='upd') online_now = str(mem.status) != 'offline' sec_now = other.get_sec_total() frm = '[%d.%m.%y]%H:%M:%S' if ( sec_now - usrs[mem.id].last_st) >= 86400 else '%H:%M:%S' s_now = f'~{other.sec2ts(sec_now, frm=frm)}~' if usrs[mem.id].online and online_now: users_online[mem.id] = [[ other.sec2ts(usrs[mem.id].last_st, frm=frm), s_now ]] elif online_now: users_online[mem.id] = [] elif usrs[mem.id].online: users_online[mem.id] = [[ other.sec2ts(usrs[mem.id].last_st, frm=frm) ]] else: users_online[mem.id] = [[f'{{{s_now}}}']] online_change(mem.id, status=str(mem.status), st_now=s_now) for usr in usrs: if usr not in s_mems: usrs[usr].go() log.pr_news('User ' + usrs[usr].name + ' disappeared! [Ban: ' + str(gone[usr].ban) + ']') for u_ban in bans: if u_ban.id not in gone: if Gn.check_new(u_ban): log.pr_news('New ban user ' + gone[u_ban.id].name + ' from users!') else: log.pr_news('New ban user ' + gone[u_ban.id].name + ' from somewhere!') else: if gone[u_ban.id].toban(True): log.pr_news('New ban user ' + gone[u_ban.id].name + ' from gone!') if bans_id: for gn in gone: if gn not in bans_id: if gone[gn].toban(False): log.pr_news('User ' + gone[gn].name + ' not in ban now!') log.I('+ finished check people')