示例#1
0
def botinvite(bot, update):
    query = update.callback_query
    callback = callabck_parse.callback_parse(query.data)

    if query.message.text_html:
        edit_msg = query.message.text_html

    keyboard = None
    if callback.qact == 'leave':
        if bot.leave_chat(callback.qdata):
            edit_msg += f'\nleave by: {query.from_user.mention_html()}'
        else:
            edit_msg += f'\nfailed to leave group <code>{callback.qdata}</code>'
    elif callback.qact == 'pass':
        edit_msg += f'\npass by: {query.from_user.mention_html()}'
    else:
        pass

    try:
        reply_markup = None if keyboard is None else InlineKeyboardMarkup(
            keyboard)
    except UnboundLocalError:
        query.answer('這個按鈕似乎有問題...?')
        return

    bot.edit_message_text(text=edit_msg,
                          parse_mode='html',
                          chat_id=query.message.chat.id,
                          reply_markup=reply_markup,
                          message_id=query.message.message_id)
示例#2
0
def hint(bot, update):
    query = update.callback_query
    i18n(query).loads.install(True)

    mongo = db_tools.use_mongo()
    callback = callabck_parse.callback_parse(query.data)

    if callback.qact in ['groupconfig', 'guide']:
        if callback.qdata in emojitags().emoji_dict.keys():
            answer = emojitags().emoji_dict[callback.qdata]['hint']
        elif callback.qdata in generate.groupconfig_dict(1).keys():
            answer = generate.groupconfig_dict(1)[callback.qdata]['hint']

        query.answer(_('{answer}'.format(answer=answer)), show_alert=True)
示例#3
0
def quickban(bot, update):
    query = update.callback_query
    i18n(update).loads.install(True)
    callback = callabck_parse.callback_parse(query.data)
    user_id, msg_id = callback.qact.split(':')
    tags = callback.qdata

    if sage.michael(query.from_user.id) == False and sage.lucifer(
            query.from_user.id) == False:
        query.answer('權限不夠。')
        return
    if sage.in_shield(int(user_id)):
        text = query.message.text_html + '\n被保護ㄉ狀態。'
        query.edit_message_text(text, parse_mode='html')
        return
    try:
        sent = update.message.forward(config.getint('log', 'evidence'))
    except BaseException:
        sent = 2

    days = druation([tags])
    if days != 0:
        until = int(
            (datetime.now(taiwan_country) + timedelta(days=days)).timestamp())
    else:
        until = 0
    excalibur(bot,
              update,
              int(user_id), [tags],
              query.from_user.id,
              until=until,
              reason=tags)
    text = query.message.text_html + \
        f'\n{"="*23}' + \
        _(f'\n處刑人:{query.from_user.mention_html()}\n') + \
        _(f'標籤:<code>{tags}</code>')
    query.edit_message_text(text, parse_mode='html')
def groupconfig_callback(bot, update):
    query = update.callback_query
    i18n(update).loads.install(True)

    mongo = db_tools.use_mongo()
    callback = callabck_parse.callback_parse(query.data)
    query_group = mongo.group.find_one({'chat.id': query.message.chat.id})
    group = db_parse.group()
    group.parse(query_group)
    if sage.lucifer(query.from_user.id) or is_admin(
            bot, update, (query.message.chat.id, query.from_user.id)):
        pass
    else:
        text = '你又不是管理員 😘'
        query.answer(text, show_alert=True)
        return

    if group.config is None:
        return

    if callback.qact == 'keyboard' and callback.qdata == 'close':
        text = _('<code>[設定完成]</code>\n\n') + \
            _(f'<code>{escape(query.message.chat.title)}</code>\n') + \
            _('📋 已訂閱黑名單列表:\n')
        emoji_list = emojitags().emoji_dict
        sub = ''
        plugin_ = ''
        for emoji in emoji_list:
            if emoji_list[emoji]['emoji'][0] in group.config.sub_ban_list:
                sub += '{title}\n'.format(title=_(emoji_list[emoji]['title']))
        text += f'<pre>{sub}</pre>'

        for groupconfig_mcro in group.config_list_k:
            if groupconfig_mcro:
                plugin_ += '{title}\n'.format(title=generate.groupconfig_dict(
                    1)[groupconfig_mcro]['title'])
        if plugin_:
            text += _('⚙️ 已啟動附加功能:\n') + f'<pre>{plugin_}</pre>'

        query.edit_message_text(text=text, parse_mode='html')

    elif callback.qact == 'keyboard':
        if callback.qdata == '0':
            text = f'<code>{escape(query.message.chat.title)}</code>\n' + \
                _('📋 訂閱黑名單列表\n') + \
                _('本清單預設開啟 "兒童色情內容" \n') + \
                _('✅ - 開啟訂閱\n') + \
                _('❌ - 關閉訂閱')
            keyboard = generate.inline_groupconfig(bot, update, 0)
            query.edit_message_text(text=text,
                                    reply_markup=keyboard,
                                    parse_mode='html')

        elif callback.qdata == '1':
            text = f'<code>{escape(query.message.chat.title)}</code>\n' + \
                _('⚙️ 附加功能設定\n') + \
                _('✅ - 開啟訂閱\n') + \
                _('❌ - 關閉訂閱')
            keyboard = generate.inline_groupconfig(bot, update, 1)
            query.edit_message_text(text=text,
                                    reply_markup=keyboard,
                                    parse_mode='html')

        elif callback.qdata == '2':
            text = f'<code>{escape(query.message.chat.title)}</code>\n' + \
                '🌐 語言設定/Language Settings\n' + \
                _('✅ - Choosen\n')
            keyboard = generate.inline_groupconfig(bot, update, page=2)
            query.edit_message_text(text=text,
                                    reply_markup=keyboard,
                                    parse_mode='html')

    elif callback.qact == 'sub':
        # group.config.sub_ban_list
        click = to_emoji([callback.qdata])
        sub_total = len(emojitags().emoji_dict.keys()) - 1

        if click not in group.config.sub_ban_list and callback.qdata != 'spam':
            group.config.sub_ban_list.extend(click)
            if sub_total == len(group.config.sub_ban_list):
                group.config.sub_ban_list.append('💩')
            mongo.group.find_one_and_update(
                {'chat.id': query.message.chat.id}, {
                    '$set': {
                        'chat.config.sub_ban_list': group.config.sub_ban_list
                    }
                })
        elif click in group.config.sub_ban_list and callback.qdata != 'spam':
            group.config.sub_ban_list.remove(click[0])
            if sub_total == len(group.config.sub_ban_list):
                group.config.sub_ban_list.remove('💩')
            mongo.group.find_one_and_update(
                {'chat.id': query.message.chat.id}, {
                    '$set': {
                        'chat.config.sub_ban_list': group.config.sub_ban_list
                    }
                })

        if callback.qdata == 'spam':
            if sub_total > len(group.config.sub_ban_list):
                group.config.sub_ban_list = to_emoji(
                    list(emojitags().emoji_dict.keys()))
                mongo.group.find_one_and_update(
                    {'chat.id': query.message.chat.id}, {
                        '$set': {
                            'chat.config.sub_ban_list':
                            list(group.config.sub_ban_list)
                        }
                    })
            else:
                group.config.sub_ban_list = []
                mongo.group.find_one_and_update(
                    {'chat.id': query.message.chat.id}, {
                        '$set': {
                            'chat.config.sub_ban_list':
                            group.config.sub_ban_list
                        }
                    })

        keyboard = generate.inline_groupconfig(bot, update, 0)
        query.edit_message_reply_markup(reply_markup=keyboard)
        query.answer('Done.')

    elif callback.qact == 'set':
        # group.config.ml_nsfw
        if callback.qdata in group.config_list.keys():
            if group.config_list[callback.qdata]:
                mongo.group.find_one_and_update(
                    {'chat.id': query.message.chat.id},
                    {'$set': {
                        f'chat.config.{callback.qdata}': False
                    }},
                    upsert=True)
            else:
                mongo.group.find_one_and_update(
                    {'chat.id': query.message.chat.id},
                    {'$set': {
                        f'chat.config.{callback.qdata}': True
                    }},
                    upsert=True)
        else:
            if callback.qdata == 'all':
                group_config = query_group['chat']['config'].copy()
                for settings in generate.groupconfig_dict(1):
                    group_config[settings] = True
                if group_config == query_group['chat']['config']:
                    query.answer('>:(')
                    return
                mongo.group.find_one_and_update(
                    {'chat.id': query.message.chat.id},
                    {'$set': {
                        f'chat.config': group_config
                    }},
                    upsert=True)
            else:
                mongo.group.find_one_and_update(
                    {'chat.id': query.message.chat.id},
                    {'$set': {
                        f'chat.config.{callback.qdata}': True
                    }},
                    upsert=True)

        keyboard = generate.inline_groupconfig(bot, update, 1)
        try:
            query.edit_message_reply_markup(reply_markup=keyboard)
            query.answer('Done.')
        except Exception as e:
            logger.warning(e)

    elif callback.qact == 'langset':
        mongo.group.find_one_and_update(
            {'chat.id': query.message.chat.id},
            {'$set': {
                'chat.config.lang_code': callback.qdata
            }})
        keyboard = generate.inline_groupconfig(bot, update, 2)
        try:
            query.edit_message_reply_markup(reply_markup=keyboard)
            query.answer('Done.')
        except Exception as e:
            logger.warning(e)
示例#5
0
def namechecker(bot, update):
    """
    query_data rule
    category action data:data2

    bot leave  -100123456789 # bot leave group
    bot pass -100123456789 # keep bot in group
    user kick {update.message.chat.id}:{new_member.id}
    user pass {update.message.chat.id}:{new_member.id}
    user bypass {update.message.chat.id}:+{from_user.id}:-{new_member.id}
    +: keep, -: kick
    user back {update.message.chat.id}:+{from_user.id}:-{new_member.id}
    +: nothing, -: unban
    """
    query = update.callback_query
    callback = callabck_parse.callback_parse(query.data)

    if query.message.text_html:
        edit_msg = query.message.text_html
    else:
        edit_msg = query.message.caption_html

    if callback.qtype == 'user':
        target_chat_id, *targets = callback.qdata.split(':')
        if callback.qact == 'bypass':
            callback_data = f'user back {target_chat_id}'
            for target in targets:
                target_act, target_id = target[:1], target[1:]
                if target_act == '+':  # keep in group
                    edit_msg += f'\npass <code>{target_id}</code> by: {query.from_user.mention_html()}'
                    if target_id:
                        callback_data += f':+{target_id}'
                else:
                    try:
                        bot.kick_chat_member(target_chat_id, target_id)
                        edit_msg += f'\nkicked by: {query.from_user.mention_html()}'
                    except (BadRequest, Unauthorized) as e:
                        callback_data += f':+{target_id}'
                        try:
                            edit_msg += f'\nfailed to kick <code>{target_id}</code> by: {query.from_user.mention_html()}'
                        except TypeError:
                            query.answer('bot 並不在該群。', show_alert=True)
                            callback_data = None
                            return
                        if e.message == 'User is an administrator of the chat':
                            query.answer('你嘗試 bang 掉 admin。',
                                         show_alert=True)
                        elif e.message == 'Not enough rights to restrict/unrestrict chat member':
                            query.answer('Bot 在該群組並非 admin。',
                                         show_alert=True)
                            leave_group_msg = f'偵測到 <a href="tg://user?id={target_id}">spam 帳號</a>\n' \
                                '為 bot 正常運作請給予 admin 權限,請給予必要權限後再嘗試使用。\n' \
                                '有問題請至 @hexjudge 詢問。'
                            try:
                                bot.send_message(
                                    target_chat_id, leave_group_msg, parse_mode='html').done.wait()
                            except TelegramError as e:
                                edit_msg += '\n' + e.message
                                if e.message == 'Have no rights to send a message':
                                    pass
                            callback_data = None
                        elif e.message == 'Forbidden: bot is not a member of the supergroup chat':
                            query.answer('bot 並不在該群。', show_alert=True)
                            callback_data = None
                        else:
                            query.answer('發生了一些錯誤,一群訓練有素的猴子們正在圍觀。')
                            bot.send_message(
                                config.getint(
                                    'log',
                                    'error'),
                                f'<code>{escape(e.message)}</code>',
                                parse_mode='html')

                if callback_data is None:
                    keyboard = None
                else:
                    keyboard = [[InlineKeyboardButton(
                        '回去', callback_data=callback_data), ]]
        elif callback.qact == 'back':
            edit_msg += f'\nback by: {query.from_user.mention_html()}'
            if len(targets) == 2:
                for target in targets:
                    target_act, target_id = target[:1], target[1:]
                    if target_act == '-':
                        try:
                            bot.unban_chat_member(
                                target_chat_id, target_id)
                        except BadRequest as e:
                            if e.message == 'Not enough rights to restrict/unrestrict chat member':
                                query.answer('沒有力量去對那個人上下其手。')
                                return
                callback_data = [
                    f'user bypass {target_chat_id}:+{targets[0][1:]}:-{targets[1][1:]}',
                    f'user bypass {target_chat_id}:-{targets[0][1:]}:+{targets[1][1:]}',
                    f'user bypass {target_chat_id}:+{targets[0][1:]}:+{targets[1][1:]}',
                    f'user bypass {target_chat_id}:-{targets[0][1:]}:-{targets[1][1:]}',
                ]
                keyboard = [
                    [InlineKeyboardButton("掐死被邀人", callback_data=callback_data[0]),  # kick from_user group
                        InlineKeyboardButton("掐死邀請人", callback_data=callback_data[1])],  # kick new_member group
                    [InlineKeyboardButton("先放過", callback_data=callback_data[2]),  # keep both
                        InlineKeyboardButton("殉情", callback_data=callback_data[3])]]  # kick both
            else:
                for target in targets:
                    target_act, target_id = target[:1], target[1:]
                    if target_act == '-':
                        bot.unban_chat_member(target_chat_id, target_id)
                callback_data = [
                    f'user bypass {target_chat_id}:-{targets[0][1:]}',
                    f'user bypass {target_chat_id}:+{targets[0][1:]}']
                keyboard = [
                    [
                        InlineKeyboardButton(
                            '阿拉花瓜',
                            callback_data=callback_data[0]),
                        InlineKeyboardButton(
                            '他還只是個孩子啊',
                            callback_data=callback_data[1])]]

    try:
        reply_markup = None if keyboard is None else InlineKeyboardMarkup(
            keyboard)
    except UnboundLocalError:
        query.answer('這個按鈕似乎有問題...?')
        return

    bot.edit_message_text(text=edit_msg,
                          parse_mode='html',
                          chat_id=query.message.chat.id,
                          reply_markup=reply_markup,
                          message_id=query.message.message_id)