Esempio n. 1
0
async def get_chat_admins(chat_id):
    dump = redis.get('admins_cache_{}'.format(chat_id))
    if not dump:
        await update_admin_cache(chat_id)
        dump = redis.get('admins_cache_{}'.format(chat_id))

    admins = ujson.decode(dump)
    return admins
Esempio n. 2
0
async def flood_limit(event, command):
    if event.chat_id:
        chat_id = event.chat_id
    else:
        chat_id = event.from_id

    if not hasattr(event, 'from_id'):
        check = event.query.user_id
    else:
        check = event.from_id

    if check in SUDO:
        return True

    db_name = 'flood_command_{}_{}'.format(chat_id, command)
    redis.incr(db_name, 1)
    number = int(redis.get(db_name))
    redis.expire(db_name, 60)
    if number > 7:
        return False
        redis.expire(db_name, 120)
    if number > 6:
        return False
        await event.reply(
            '**Flood detected!**\nPlease wait 3 minutes before do this again!')
        redis.expire(db_name, 120)
    else:
        return True
Esempio n. 3
0
async def event(event):
    msg = await event.reply("Updating cache now...")
    await update_admin_cache(event.chat_id)
    dump = redis.get('admins_cache_{}'.format(event.chat_id))
    admins = ujson.decode(dump)
    text = '**Admin in this group:**\n'
    for admin in admins:
        H = mongodb.user_list.find_one({'user_id': admin})
        if H:
            text += '- {} ({})\n'.format(await user_link(H['user_id']), H['user_id'])

    await msg.edit(text)
Esempio n. 4
0
async def adminlist(message):
    msg = await message.reply("Updating cache now...")
    await update_admin_cache(message.chat.id)
    dump = redis.get('admins_cache_{}'.format(message.chat.id))
    admins = ujson.decode(dump)
    text = '<b>Admin in this group:</b>\n'
    for admin in admins:
        H = mongodb.user_list.find_one({'user_id': admin})
        if H:
            text += '- {} ({})\n'.format(await user_link_html(H['user_id']),
                                         H['user_id'])

    await msg.edit(text)
async def do_backup(chat_id, reply=False):
    await bot.send_message(chat_id,
                           "Dumping the DB, please wait...",
                           reply_to_message_id=reply)
    date = strftime("%Y-%m-%d_%H:%M:%S", gmtime())
    file_name = f"Backups/dump_{date}.7z"
    if not os.path.exists("Backups/"):
        os.mkdir("Backups/")
    await term(
        f"mongodump --uri \"{CONFIG['basic']['mongo_conn']}\" --out=Backups/tempbackup"
    )

    # Let's also save Redis cache
    with open('Backups/tempbackup/redis_keys.json', 'w+') as f:
        keys = redis.keys()
        new = {}
        for key in keys:
            key_type = redis.type(key)
            if key_type == 'string':
                new[key] = redis.get(key)
            elif key_type == 'list':
                new[key] = list(redis.lrange(key, 0, -1))
        f.write(ujson.dumps(new, indent=2))

    # Copy config file
    shutil.copyfile('data/bot_conf.json', 'Backups/tempbackup/bot_conf.json')

    await bot.send_message(chat_id,
                           "Compressing and uploading to Telegram...",
                           reply_to_message_id=reply)
    password = CONFIG['advanced']['backups_password']
    await term(
        f"cd Backups/tempbackup/; 7z a -mx9 ../../{file_name} * -p{password} -mhe=on"
    )
    shutil.rmtree('Backups/tempbackup')

    if not os.path.exists(file_name):
        await bot.send_message(chat_id, "Error!", reply_to_message_id=reply)
        return

    text = "<b>Backup created!</b>"
    size = convert_size(os.path.getsize(file_name))
    text += f"\nBackup name: <code>{file_name}</code>"
    text += f"\nSize: <code>{size}</code>"
    await tbot.send_file(chat_id,
                         file_name,
                         reply_to=reply,
                         caption=text,
                         parse_mode="html")
Esempio n. 6
0
def get_chat_lang(chat_id):
    r = redis.get('lang_cache_{}'.format(chat_id))
    if r:
        return r
    else:
        db_lang = mongodb.lang.find_one({'chat_id': chat_id})
        if db_lang:
            # Rebuild lang cache
            redis.set('lang_cache_{}'.format(chat_id), db_lang['lang'])
            return db_lang['lang']
        user_lang = mongodb.user_list.find_one({'user_id': chat_id})
        if user_lang and user_lang['user_lang'] in LANGS:
            # Add telegram language in lang cache
            redis.set('lang_cache_{}'.format(chat_id), user_lang['user_lang'])
            return user_lang['user_lang']
        else:
            return 'en'
Esempio n. 7
0
async def flood_limit(command, chat_id):

    if chat_id in SUDO:
        return True

    db_name = 'flood_command_{}_{}'.format(chat_id, command)
    redis.incr(db_name, 1)
    number = int(redis.get(db_name))
    redis.expire(db_name, 60)
    if number > 7:
        return False
        redis.expire(db_name, 120)
    if number > 6:
        return False
        redis.expire(db_name, 120)
    else:
        return True
Esempio n. 8
0
async def check_message(event):
    cache = redis.get('filters_cache_{}'.format(event.chat_id))
    try:
        lst = ujson.decode(cache)
    except TypeError:
        return
    if not lst:
        return
    text = event.text.split(" ")
    for filter in lst:
        for word in text:
            match = re.fullmatch(filter, word, flags=re.IGNORECASE)
            if not match:
                return
            H = mongodb.filters.find_one(
                {'chat_id': event.chat_id, "handler": {'$regex': str(filter)}})

            if H['action'] == 'note':
                await send_note(event.chat_id, event.chat_id, event.message.id,
                                H['arg'], show_none=True)
            elif H['action'] == 'delete':
                await event.delete()