def refuse(msg: catbot.Message):
    try:
        operator = bot.get_chat_member(config['group'], msg.from_.id)
    except catbot.UserNotFoundError:
        return
    if not (operator.status == 'creator' or operator.status == 'administrator'):
        return

    if msg.reply:
        refused_id = msg.reply_to_message.from_.id
    else:
        user_input_token = msg.text.split()
        if len(user_input_token) < 2:
            bot.send_message(msg.chat.id, text=config['messages']['general_prompt'], reply_to_message_id=msg.id)
            return
        try:
            refused_id = int(user_input_token[1])
        except ValueError:
            bot.send_message(msg.chat.id, text=config['messages']['telegram_id_error'], reply_to_message_id=msg.id)
            return

    try:
        refused_user = bot.get_chat_member(config['group'], refused_id)
    except catbot.UserNotFoundError:
        restricted_until = 0
    else:
        if refused_user.status == 'restricted':
            restricted_until = refused_user.until_date
            if restricted_until == 0:
                restricted_until = -1
        else:
            restricted_until = 0

    with t_lock:
        ac_list, rec = bot.secure_record_fetch('ac', list)
        for i in range(len(ac_list)):
            entry = Ac.from_dict(ac_list[i])
            if entry.telegram_id == refused_id:
                refused_index = i
                break
        else:
            entry = Ac(refused_id)
            ac_list.append(entry)
            refused_index = -1

        if restricted_until != -1:
            entry.restricted_until = restricted_until
        entry.confirmed = False
        entry.confirming = False
        entry.refused = True
        ac_list[refused_index] = entry.to_dict()
        rec['ac'] = ac_list
        json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)

    log(config['messages']['refuse_log'].format(tg_id=refused_id, refuser=html_refer(operator.name)))

    silence_trial(entry)
def new_member(msg: catbot.ChatMemberUpdate):
    if msg.new_chat_member.status == 'restricted':
        restricted_until = msg.new_chat_member.until_date
        if restricted_until == 0:
            restricted_until = -1  # Restricted by bot, keep entry.restricted_until unchanged later
    elif msg.new_chat_member.status == 'creator' or \
            msg.new_chat_member.status == 'administrator' or \
            msg.new_chat_member.status == 'kicked':
        return
    else:
        restricted_until = 0

    try:
        bot.silence_chat_member(config['group'], msg.new_chat_member.id)
    except catbot.InsufficientRightError:
        bot.send_message(config['group'], text=config['messages']['insufficient_right'])
        return

    with t_lock:
        ac_list, rec = bot.secure_record_fetch('ac', list)
        for i in range(len(ac_list)):
            entry = Ac.from_dict(ac_list[i])
            if entry.telegram_id == msg.new_chat_member.id:
                user_index = i
                break
        else:
            entry = Ac(msg.from_.id)
            ac_list.append(entry)
            user_index = -1

        if restricted_until != -1:
            entry.restricted_until = restricted_until
        ac_list[user_index] = entry.to_dict()
        rec['ac'] = ac_list
        json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)

    if entry.confirmed or entry.whitelist_reason:
        lift_restriction_trial(entry, config['group'])
    else:
        with t_lock:
            last_id, rec = bot.secure_record_fetch('last_welcome', int)
            cur = bot.send_message(config['group'],
                                   text=config['messages']['new_member_hint'].format(
                                       tg_id=msg.new_chat_member.id,
                                       tg_name=html_refer(msg.new_chat_member.name)),
                                   parse_mode='HTML')
            rec['last_welcome'] = cur.id
            json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
            try:
                bot.delete_message(config['group'], last_id)
            except catbot.DeleteMessageError:
                pass