async def antiflood(msg): if msg.get('chat') and msg['chat']['type'].endswith('group') and msg.get( 'from'): adm = await is_admin(msg['chat']['id'], msg['from']['id']) if not adm['user'] and adm['bot']: # Delete old rows. cursor.execute( 'DELETE FROM antiflood WHERE chat_id = ? AND unix_time < ?', (msg['chat']['id'], int(time.time()) - 5)) conn.commit() # Insert antiflood row. cursor.execute( 'INSERT INTO antiflood (chat_id, user_id, unix_time) VALUES (?,?,?)', (msg['chat']['id'], msg['from']['id'], int(time.time()))) conn.commit() # Get total rows count. cursor.execute( 'SELECT COUNT(*) FROM antiflood WHERE chat_id = ? AND user_id = ?', (msg['chat']['id'], msg['from']['id'])) msgs = cursor.fetchone()[0] if msgs == 5: await bot.sendMessage(msg['chat']['id'], 'Flood!') return True
def remove_user(chat_id, user_id): cursor.execute('SELECT antichato_list FROM chats WHERE chat_id = ?', (chat_id,)) user_list = json.loads(cursor.fetchall()[0][0]) for num, user in enumerate(user_list): if user == user_id: del user_list[num] cursor.execute('UPDATE chats SET antichato_list = ? WHERE chat_id = ?', (json.dumps(user_list), chat_id,)) conn.commit()
def add_user(chat_id, user_id): cursor.execute('SELECT antichato_list FROM chats WHERE chat_id = ?', (chat_id,)) user_list = json.loads(cursor.fetchall()[0][0]) if user_id not in user_list: user_list.append(user_id) cursor.execute('UPDATE chats SET antichato_list = ? WHERE chat_id = ?', (json.dumps(user_list), chat_id,)) conn.commit() return True
def add_warns(chat_id, user_id, number): cursor.execute('SELECT * FROM user_warns WHERE chat_id = ? AND user_id = ?', (chat_id, user_id)) if cursor.fetchall(): cursor.execute('UPDATE user_warns SET count = count + ? WHERE chat_id = ? AND user_id = ?', (number, chat_id, user_id)) conn.commit() else: cursor.execute('INSERT INTO user_warns (user_id, chat_id, count) VALUES (?,?,?)', (user_id, chat_id, number)) conn.commit() return True
async def is_admin(chat_id, user_id, reply_id=None): if int(chat_id) < 0: # Groups and supergoups IDs. dic = {} cursor.execute('SELECT cached_admins FROM chats WHERE chat_id = ?', (int(chat_id), )) adms = cursor.fetchone()[0] if adms: cached_admins = json.loads(adms) else: cached_admins = {'expires': 0} if cached_admins['expires'] > time.time(): adm_id = cached_admins['admins_list'] else: adms = await bot.getChatAdministrators(chat_id) adm_id = [] for ids in adms: adm_id.append(ids['user']['id']) cursor.execute( 'UPDATE chats SET cached_admins = ? WHERE chat_id = ?', (json.dumps( dict(admins_list=adm_id, expires=int(time.time()) + 1200)), chat_id)) conn.commit() if user_id in adm_id or user_id in sudoers: dic['user'] = True else: dic['user'] = False if reply_id in adm_id: dic['reply'] = True else: dic['reply'] = False if bot_id in adm_id: dic['bot'] = True else: dic['bot'] = False else: # User IDs. dic = dict(user=False, reply=False, bot=False) return dic
def disable_welcome(chat_id): cursor.execute('UPDATE chats SET welcome_enabled = ? WHERE chat_id = ?', (False, chat_id)) conn.commit()
def set_welcome(chat_id, welcome): cursor.execute('UPDATE chats SET welcome = ? WHERE chat_id = ?', (welcome, chat_id)) conn.commit()
def reset_warns(chat_id, user_id): cursor.execute('DELETE FROM user_warns WHERE chat_id = ? AND user_id = ?', (chat_id, user_id)) conn.commit() return True
def set_rules(chat_id, rules): cursor.execute('UPDATE chats SET rules = ? WHERE chat_id = ?', (rules, chat_id)) conn.commit()
def set_antipedro(chat_id, toggle): cursor.execute('UPDATE chats SET antipedro_enabled = ? WHERE chat_id = ?', (bool(toggle), chat_id)) conn.commit() return True