def reg_squad(**kwargs): """ Add new squad with source and token """ # TeamLeaders only roles = [0, 1, 3] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return com = kwargs['msg']['text'].split() if len(com) != 3: vk_api.send(kwargs['chat'], "Wrong arguments, squad and token needed") return if len(com[1]) > 2: vk_api.send(kwargs['chat'], "Use initials of squad (xx) for squad name") return com[1] = com[1].upper() if len(com[2]) != 128 or not hw_api.check(com[1], com[2]): vk_api.send(kwargs['chat'], "Wrong token or squad") return else: squads.reg_squad(com[1], com[2]) vk_api.send(kwargs['chat'], "Squad registered successfully") return
def blocked(chat): """ Just for keep bot safe :param chat: int, [peer_id] :return: None """ vk_api.send(chat, "This function is temporary unavailable") return
def internal_error(*args): """ Send in errors chat info about exception and continue working Something like except: pass :return: response('ok', 200) """ vk_api.send(settings.errors_chat, str(format_exc(-5))) return make_response('ok', 200)
def home(**kwargs): """ Return to home page if bot broke """ if kwargs['chat'] > 2000000000: return kb = kbd_list.main vk_api.send(kwargs['chat'], "Home", kb) return
def kill(**kwargs): """ Raises Exception to kill bot. Used to check stability and error logging """ roles = [0] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return raise Exception
def ping(**kwargs): """ Send in vk message that bot is working """ roles = [0, 1, 3] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return vk_api.send(kwargs['chat'], "Still Alive from " + str(int(start_time))) return
def page(**kwargs): """ Send in vk new keyboard """ pl = kwargs['payload'] kbd = keyboard(pl, kwargs['role_id'], kwargs['msg']['from_id']) # kbd = getattr(kbd_list, pl) # TODO: +-Edit kbd if role # TODO: +-Make page titles (Battle_Push page looks not safe) msg = kwargs.get('flag', page_text(pl, kwargs['msg'])) vk_api.send(kwargs['chat'], msg, kbd) return
def squad_list(**kwargs): """ Send in vk squad list """ roles = [0, 1, 3] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return msg = ', '.join(squads.get_squads()) vk_api.send(kwargs['chat'], msg) return
def del_squad(**kwargs): """ Delete squad from DB """ # TeamLeaders only roles = [0, 1, 3] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return com = kwargs['msg']['text'].split() if len(com) != 2: vk_api.send(kwargs['chat'], "Wrong argument, source is needed") return if com[1] not in squads.get_squads(): vk_api.send(kwargs['chat'], "Squad already doesn't exists") return # TODO: Check for user in squad squads.del_squad(com[1]) vk_api.send(kwargs['chat'], "Squad deleted") return
def kbda(**kwargs): """ Set target 0 for all squads [WARNING] Long requests, can works double """ # TeamLeaders only roles = [0, 1, 3] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return hw_api.remove_all() vk_api.send(kwargs['chat'], "Keyboards are removed") return
def get_time(fwd, chat_id, role_id): """ Send in vk [date] of forward """ roles = [0] # maybe add testers if role_id not in roles: return if 'fwd_messages' in fwd[0].keys(): if 'fwd_messages' in fwd[0]['fwd_messages'][0].keys(): res = fwd[0]['fwd_messages'][0]['date'] else: res = fwd[0]['date'] else: res = fwd[0]['date'] vk_api.send(chat_id, str(res)) return
def cmd_list(**kwargs): """ Send in vk list of commands """ roles = [0] user = kwargs['user'] if user['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return com_list = cmd() msg = 'There are ' + str(len(com_list)) + ' commands:\n' msg += '\n'.join(com_list) vk_api.send(kwargs['chat'], msg) return
def distribution(fwd, chat_id, role_id, user_id): """ Start distribution of battle results for subscribed """ roles = [role_id] # maybe here will be role limit if role_id not in roles: return message = str() for i in range(7): if fwd[i]['from_id'] == hw_id: message += str(fwd[i]['text']).replace('\n\n', '\n') + '\n\n' else: vk_api.send( errors_chat, "@id" + str(user_id) + " tried to distribute and have error\nMessage is:\n" + message) return distribute(message) vk_api.send(chat_id, "Request to distribute sent!") return
def parse(msg, fwd): roles = [0] user_id = msg['from_id'] role_id = users.get_role(user_id) chat_id = msg['peer_id'] if len(fwd) == 1: # check some things if fwd[0]['from_id'] == hw_id: txt = str(fwd[0]['text']).encode( 'cp1251', 'xmlcharrefreplace').decode('cp1251') battle_flag = 'Результаты битвы за' in txt profile_flag = '🛌' in txt # bed emoji share_flag = '💻' in txt # lvl emoji / notebook emoji inventory_flag = '🎒' in txt # backpack emoji if battle_flag: battle(user_id, txt, role_id) elif inventory_flag: return elif profile_flag: profile(user_id, txt, role_id) elif share_flag: share(user_id, txt, role_id) elif role_id in roles: get_time(fwd, chat_id, role_id) else: if user_id == chat_id: vk_api.send(chat_id, "А зачем мне это..?") return else: if role_id in roles: get_time(fwd, chat_id, role_id) else: if user_id == chat_id: vk_api.send(chat_id, "А зачем мне это..?") return elif len(fwd) == 7: if chat_id < 2000000000: distribution(fwd, chat_id, role_id, user_id) return else: return return
def delete(**kwargs): """ Delete user and register him by forward or reply """ roles = [0] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return if 'reply_message' in kwargs['msg'].keys(): user_id = kwargs['msg']['reply_message']['from_id'] else: if len(kwargs['msg']['fwd_messages']) == 1: user_id = kwargs['msg']['fwd_messages'][0]['from_id'] else: vk_api.send(kwargs['chat'], "No reply or wrong forward") return if user_id < 0: vk_api.send(kwargs['chat'], "Only users can have roles") return users.del_user(user_id) users.reg_user(user_id, kwargs['msg']['date']) return
def target(**kwargs): """ Set target buttons """ roles = [0, 1, 3, 5, 7] # TODO: in multiThreading (or somehow after request) if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return # TeamLeader if kwargs['role_id'] in roles[0:3]: set_target(str(fraction), kwargs['payload']) # Squad Leader else: source = users.get_squad(kwargs['msg']['from_id']) set_target(source, kwargs['payload']) vk_api.send(kwargs['chat'], "target sent!") return
def id(**kwargs): """ Send in vk id of user if reply or forward? else send chat id """ roles = [0] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return if 'reply_message' in kwargs['msg'].keys(): vk_api.send(kwargs['chat'], kwargs['msg']['reply_message']['from_id']) return elif len(kwargs['msg']['fwd_messages']) == 1: vk_api.send(kwargs['chat'], kwargs['msg']['fwd_messages'][0]['from_id']) return else: vk_api.send(kwargs['chat'], kwargs['msg']['peer_id']) return
def role_list(**kwargs): """ Send in vk list of all roles """ roles = [0] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return r_list = users.get_roles() message = '' for i in r_list: message = message + i + '\n' vk_api.send(kwargs['msg']['from_id'], message) vk_api.send(kwargs['chat'], "List sent in your chat") return
def db_query(**kwargs): """ Execute query directly into DB and send result into chat !!! [DANGER] !!! Use it only if you 101% sure what are you doing """ roles = [0] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return q = kwargs['msg']['text'].replace('/query ', '') res = query(q) vk_api.send(kwargs['msg']['from_id'], res) vk_api.send(kwargs['chat'], "Result is in your chat") return
def kbdd(**kwargs): """ Send in vk empty keyboard """ # TeamLeaders only roles = [0, 1, 3] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return if kwargs['chat'] < 2000000000: vk_api.send(kwargs['chat'], "This is not conversation!") return keyboard = {'one_time': True, 'buttons': []} # We need to modify keyboard without affecting to original vk_api.send(kwargs['chat'], "Keyboard removed!", keyboard) return
def kbd(**kwargs): """ Send in vk keyboard for setting targets """ # TeamLeaders only roles = [0, 1, 3] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return if kwargs['chat'] < 2000000000: vk_api.send(kwargs['chat'], "This is not conversation!") return # We need to modify keyboard without affecting to original keyboard = copy.deepcopy(kbd_list.target) keyboard['buttons'].pop() vk_api.send(kwargs['chat'], "Your keyboard!", keyboard) return
def battle(user_id, text, role_id): """ Parses battle report and write it into DB """ roles = [0, 1, 3, 5, 7, 9, 11] if role_id not in roles: return date_row = text.split(sep='\n\n')[0] date_pattern = r"\d{2}.\d{2}.\d{4}" date = re.search(date_pattern, date_row)[0] date = datetime.datetime.strptime(date, '%d.%m.%Y').date() # DD.MM.YYYY format tomorrow = datetime.date.today().replace(day=datetime.date.today().day + 1) if date > tomorrow: vk_api.send(user_id, "Future reports are not allowed") return # TODO: Add check for date limit (for every squad or 3 days) last_report = users.get_report(user_id)['date'] last_report = datetime.datetime.strptime(last_report, '%Y-%m-%d').date() if date < last_report: vk_api.send(user_id, "This is old report, send more actual") return money = r"\d+" emoji_pattern = r"&#\d+;" msg = text.split(sep='\n\n')[1:-1] income = 0 pure = 0 target = 0 result = str() for block in range(len(msg)): res = msg[block].split(sep='\n') if len(res) == 1: target = 0 result = result + 'AFK' + '\n' elif len(res) == 2: tran = int(re.search(money, res[1])[0]) result = result + "Transaction:" + str(tran) + '\n' income += tran pure += tran elif len(res) == 3: loss = int(re.search(money, res[2])[0]) result = result + "Lost from def:" + str(loss) + '\n' pure -= loss elif len(res) == 4: if re.search(emoji_pattern, res[0]): target = fractions[re.search(emoji_pattern, res[0])[0]] else: target = 7 main = int(re.search(money, res[2])[0]) result = result + "Failed attack: " + str(main) + '\n' pure -= main elif len(res) == 5: if re.search(emoji_pattern, res[0]): target = fractions[re.search(emoji_pattern, res[0])[0]] else: target = 7 main = int(re.search(money, res[2])[0]) result = result + "Attack: " + str(main) + '\n' pure += main income += main result = result + "Income:" + str(income) + '\nPure:' + str( pure) + '\nTarget:' + str(target) vk_api.send(user_id, result) users.set_report(user_id, date, income, pure, target) vk_api.send(user_id, "Battle result successfully written") return
def share(user_id, text, role_id): """ Parses share and write into DB """ user_info = users.get_user(user_id) shar = text.split(sep='\n\n') tl_flag = False sl_flag = False nick_pattern = r"\b[\w ]+\b(?=\s\()" squad_pattern = r"\b\w\w\b" emoji_pattern = r"&#\d+;" stat_pattern = r"\s\d+" """ Parse block """ row = shar[0].split(sep='\n')[0] nick = re.search(nick_pattern, row)[0].strip() sl = '▫' tl = '🔸' if sl in row: sl_flag = True elif tl in row: tl_flag = True else: pass f = fractions[re.findall(emoji_pattern, row)[-1]] if re.search(squad_pattern, row): squad = re.search(squad_pattern, row)[0] else: squad = f # TODO: Do smth with fraction prc_val = int(re.findall(stat_pattern, shar[1])[0]) teo_val = int(re.findall(stat_pattern, shar[1])[1]) hit_val = int(re.findall(stat_pattern, shar[1])[2]) mud_val = int(re.findall(stat_pattern, shar[1])[3]) # Check share with profile if nick != user_info['nickname']: vk_api.send(user_id, "Wrong Nickname, re-send profile to confirm") return if squad != user_info['squad']: vk_api.send(user_id, "Re-send profile to confirm squad") return if f != fraction: vk_api.send(user_id, "Send profile to confirm fraction") return message = str() message = message + nick + '\n' if tl_flag: message = message + "TL" + '\n' elif sl_flag: message = message + "SL" + '\n' else: message = message + "No Status" + '\n' message = message + str(f) + '\n' message = message + str(squad) + '\n' message = message + str(prc_val) + ', ' + str(teo_val) + '\n' message = message + str(hit_val) + ', ' + str(mud_val) + '\n' # If all ok, write new data in db users.set_profile(user_id, nick, squad, prc_val, teo_val, hit_val, mud_val) vk_api.send(user_id, "Share accepted!\n" + message) return
def role(**kwargs): """ Apply new role to user by reply or forward """ roles = [0] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return command = kwargs['msg']['text'].split() if len(command) != 2: vk_api.send(kwargs['chat'], "Wrong arguments, role id needed") return if 'reply_message' in kwargs['msg'].keys(): user_id = kwargs['msg']['reply_message']['from_id'] else: if len(kwargs['msg']['fwd_messages']) == 1: user_id = kwargs['msg']['fwd_messages'][0]['from_id'] else: vk_api.send(kwargs['chat'], "No reply or wrong forward") return if user_id < 0: vk_api.send(kwargs['chat'], "Only users can have roles") return squads_count = squads.count_squads() - 1 ''' 1 Creator, 1 TeamLeader, up to 5 other TeamLeaders SquadLeaders = count squads without fraction, up to 20 Squad members per squad, including leader ''' role_limit = {0: 1, 1: 1, 2: 5, 5: squads_count, 9: squads_count * 19} role_id = int(command[1]) if role_id < 0 or role_id > 13: vk_api.send(kwargs['chat'], "Wrong role id") return try: limit = users.count_role(role_id) < role_limit[role_id] except KeyError: limit = True if limit: users.set_role(user_id, role_id) vk_api.send(kwargs['chat'], "Role set!") else: vk_api.send(kwargs['chat'], "Limit of Role!") return
def target(**kwargs): """ Send target to HW API """ roles = [0, 1, 3, 5, 7] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return com = kwargs['msg']['text'].split() # TeamLeader if kwargs['role_id'] in roles[0:3]: if len(com) == 3: try: com[2] = int(com[2]) except ValueError: vk_api.send(kwargs['chat'], "Wrong target, should be number") return if 0 <= com[2] <= 7: if com[2] == fraction: vk_api.send(kwargs['chat'], 'Attack on yourself') return else: # target = -1 or 9 vk_api.send(kwargs['chat'], 'Wrong target') return if com[1] != 'all': com[1] = com[1].upper() if com[1] not in squads.get_squads(): vk_api.send(kwargs['chat'], "Wrong source") return if com[1] in ignored_squads: vk_api.send(kwargs['chat'], "Dead squad") return hw_api.set_target(com[1], com[2]) else: for source in squads.get_squads(): if source in ignored_squads: pass else: hw_api.set_target(source, com[2]) vk_api.send(kwargs['chat'], "Target sent!") return elif len(com) == 2: try: com[1] = int(com[1]) except ValueError: vk_api.send(kwargs['chat'], "Wrong target, should be number") return if 0 <= com[1] <= 7: if com[1] == fraction: vk_api.send(kwargs['chat'], 'Attack on yourself') return else: # target = -1 or 9 vk_api.send(kwargs['chat'], 'Wrong target') return hw_api.set_target(str(fraction), com[1]) vk_api.send(kwargs['chat'], "target sent!") return else: vk_api.send(kwargs['chat'], "Wrong arguments, (source and) target needed") return # Squad Leader else: if len(com) != 2: vk_api.send(kwargs['chat'], "Wrong arguments, target needed") return try: com[1] = int(com[1]) except ValueError: vk_api.send(kwargs['chat'], "Wrong target, should be number") return if 0 <= com[1] <= 7: if com[1] == fraction: vk_api.send(kwargs['chat'], 'Attack on yourself') return else: # target = -1 or 9 vk_api.send(kwargs['chat'], 'Wrong target') return source = users.get_squad(kwargs['msg']['from_id']) hw_api.set_target(source, com[1]) vk_api.send(kwargs['chat'], "target sent!") return
def profile(user_id, text, role_id): """ Parses profile and write into DB """ prof = text.split(sep='\n\n')[-3:-1] tl_flag = False sl_flag = False squad_flag = False nick_pattern = r"\b[\w ]+\b(?=\s\()" squad_pattern = r"\b\w\w\b" emoji_pattern = r"&#\d+;" full_pattern = r"\(\d+\)" short_pattern = r"\s\d+" """ Parse block """ row = prof[0].split(sep='\n')[0] nick = re.search(nick_pattern, row)[0].strip() sl = '▫' tl = '🔸' if sl in row: sl_flag = True elif tl in row: tl_flag = True else: pass f = fractions[re.findall(emoji_pattern, row)[-1]] if re.search(squad_pattern, row): squad = re.search(squad_pattern, row)[0] squad_flag = True else: squad = f if len(re.findall(full_pattern, prof[1])) == 0: prc_val = int(re.findall(short_pattern, prof[1])[0]) teo_val = int(re.findall(short_pattern, prof[1])[1]) hit_val = int(re.findall(short_pattern, prof[1])[2]) mud_val = int(re.findall(short_pattern, prof[1])[3]) else: prc_val = int(re.findall(full_pattern, prof[1])[0][1:-1]) teo_val = int(re.findall(full_pattern, prof[1])[1][1:-1]) hit_val = int(re.findall(full_pattern, prof[1])[2][1:-1]) mud_val = int(re.findall(full_pattern, prof[1])[3][1:-1]) message = str() message = message + nick + '\n' if tl_flag: message = message + "TL" + '\n' elif sl_flag: message = message + "SL" + '\n' else: message = message + "No Status" + '\n' message = message + str(f) + '\n' message = message + str(squad) + '\n' message = message + str(prc_val) + ', ' + str(teo_val) + '\n' message = message + str(hit_val) + ', ' + str(mud_val) + '\n' # If all ok, write new data in db users.set_profile(user_id, nick, squad, prc_val, teo_val, hit_val, mud_val) roles = [0, 3, 4, 7, 8] # Creator and associate leaders (no signs in profile for him) if tl_flag: if f == fraction: role = 1 else: role = 2 elif sl_flag: if f == fraction: role = 5 else: role = 6 elif squad_flag: role = 9 elif f == fraction: role = 11 else: role = 12 if role_id != role and role_id not in roles: users.set_role(user_id, role) vk_api.send(user_id, "Profile accepted!\n" + message) return
def message(msg): """ Main processing with message :param msg: dict, message object from vk [object][message] :return: None """ time = int(msg['date']) text = str(msg['text']) chat = int(msg['peer_id']) user = int(msg['from_id']) if user > 0: pl = 'payload' in msg.keys() fwd = len(msg['fwd_messages']) != 0 com = text.startswith('/') if user != chat: if pl or com: pass else: return else: if pl or com or fwd: pass else: return try: get_user(user) except ValueError: reg_user(user, time - 1) if user == settings.creator: set_role(user, 0) vk_api.send(user, "You became a creator") if time == get_msg(user): vk_api.send(chat, "2fast4me") return else: update_msg(user, time) else: return # keyboards if pl: pl = json.loads(msg['payload']) payload(msg, pl) return # forwards if fwd and text == '': forward = msg['fwd_messages'] forwards(msg, forward) return # commands if com: command = msg['text'].split() command[0] = command[0].replace('/', '') if command[0] in cmd(): commands(msg, command[0]) else: vk_api.send(chat, '\"/' + str(command[0]) + '\" not in list') return return
def user(**kwargs): """ Send in vk list of squad's user or list of leaders (associates and leaders) """ roles = [0, 1, 3, 5, 7] if kwargs['role_id'] not in roles: vk_api.send(kwargs['chat'], "Access Denied") return # TeamLeader if kwargs['role_id'] in roles[0:3]: com = kwargs['msg']['text'].split() if len(com) == 2: com[1] = com[1].upper() if com[1] not in squads.get_squads(): vk_api.send(kwargs['chat'], "Wrong squad") return squad_users = squads.squad_users(com[1]) msg = '' for i in squad_users: msg = msg + '[id' + str(i) + '|' + str(squad_users[i]) + ']\n' if msg == '': vk_api.send(kwargs['chat'], "Empty...") else: vk_api.send(kwargs['chat'], msg) return elif len(com) == 1: leaders = list() for squad in squads.get_squads(): for lead in squads.get_leaders(squad): leaders.append(lead) msg = '' for i in leaders: msg = msg + '[id' + str(i) + '|' + users.get_profile( i)['nickname'] + ']: ' + str(users.get_role(i)) + '\n' if msg == '': vk_api.send(kwargs['chat'], "Empty...") else: vk_api.send(kwargs['chat'], msg) return else: # TODO vk_api.send(kwargs['chat'], "Wrong arguments, (source) is needed") # SquadLeader else: squad_users = squads.squad_users( users.get_squad(kwargs['msg']['from_id'])) msg = '' for i in squad_users: msg = msg + '[id' + str(i) + '|' + str(squad_users[i]) + ']\n' if msg == '': vk_api.send(kwargs['chat'], "Empty...") else: vk_api.send(kwargs['chat'], msg) return
def test(**kwargs): """ Ummmm... Nothing... Really """ vk_api.send(kwargs['chat'], kwargs['msg']) return