0

Please tell me there is a code

invite_link = await bot.create_chat_invite_link(update.chat.id)

creates, for everyone, a permanent invitation link, it is necessary that the user shares the invitation link and only then, as he subscribes, a new member to the telegram group, for example, from 5 new members, restrictions will be lifted, the user whose link was subscribed to, according to it, to the group.

I registered, here is such a code, there are no errors, but restrictions are not removed, when subscribing, by invitation link.

connection = sqlite3.connect("invite_link.db")
cursor = connection.cursor()
connection.execute('''CREATE TABLE IF NOT EXISTS USERS
        (id INTEGER PRIMARY KEY NOT NULL, user_id INTEGER, chat_id INTEGER, name TEXT, username TEXT, invite_new_chat_members INTEGER)''')
connection.commit()

class SQLither:
    def __init__(self, db_file):
        self.connection = sqlite3.connect(db_file)
        self.cursor = self.connection.cursor()

    def user_exists(self, user_id, chat_id):
        with self.connection:
            result = self.cursor.execute("SELECT * FROM users WHERE user_id=? and chat_id=?", (user_id,chat_id,)).fetchall()
        return bool(len(result))
        
    def user_added_new_user(self, user_id, chat_id):
        with self.connection:
            self.cursor.execute("UPDATE users SET invite_new_chat_members = invite_new_chat_members + 1 WHERE user_id=? and chat_id=?", (user_id,chat_id,))

    def add_user(self, user_id, chat_id, name, username, invite_new_chat_members):
        self.cursor.execute("INSERT INTO users(user_id, chat_id, name, username, invite_new_chat_members) VALUES(?,?,?,?,?)", (user_id, chat_id, name, username, invite_new_chat_members))
        self.connection.commit()
        
    def check_new_users(self, user_id, chat_id):
        with self.connection:
            result = self.cursor.execute("SELECT invite_new_chat_members FROM users WHERE user_id=? and chat_id=?", (user_id,chat_id,)).fetchone()
            return result and result[0] >= 2

db = SQLither("invite_link.db")

    
@dp.message_handler(content_types=['text', 'photo', 'video', 'document', 'audio', 'video_note', 'voice', 'animation', 'contact', 'dice', 'location', 'poll', 'sticker', 'venue'])
async def get_handler(message: types.Message):
    user_id = message.from_user.id
    username = message.from_user.username
    chat_id = message.chat.id
    first_name = message.from_user.first_name
    last_name = message.from_user.last_name
    name = message.from_user.full_name

    invite_link = await bot.create_chat_invite_link(message.chat.id)
    ChatMemberUpdated.invite_link = await bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id)

    if not invite_link in ChatMemberUpdated.invite_link:
            if not db.check_new_users(user_id, chat_id):
                    try:
                        await message.reply(f"<a href=\"tg://user?id={user_id}\">{name}</a>. \n<b> invitation link: {invite_link['invite_link']}</b>".format(message.from_user.id), disable_web_page_preview=True, parse_mode='html', reply_markup=keyboard)
                        await asyncio.sleep(2)
                        await bot.restrict_chat_member(message.chat.id, message.from_user.id, ChatPermissions(can_send_messages=False, can_send_media_messages=False, can_send_other_messages=False, can_add_web_page_previews=False, can_send_polls=False, can_pin_messages=False, can_invite_users=True))
                        check_mess.clear()
                    except Exception as e:
                        print(e)
            if not db.check_new_users(user_id, chat_id):
                    try:
                        await message.delete()
                    except Exception as e:
                        print(e)
                        break


@dp.message_handler(content_types=['new_chat_members'])
async def new_user_joined(message: types.Message):
    user_id = message.from_user.id
    username = message.from_user.username
    name = message.from_user.full_name
    chat_id = message.chat.id
    invite_new_chat_members = len(message.new_chat_members)
    group_id = [-1001955925217]

    invite_link = await bot.create_chat_invite_link(message.chat.id)
    ChatMemberUpdated.new_chat_member = await bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id)


    for i in range(0, len(group_id)):
        if invite_link in ChatMemberUpdated.new_chat_member:
            if not db.user_exists(user_id, chat_id):
                db.user_added_new_user(user_id, chat_id)
            db.add_user(user_id, chat_id, name, username, invite_new_chat_members)
            if db.check_new_users(user_id, chat_id):
                try:
                    await bot.restrict_chat_member(chat_id=group_id[i], user_id=message.from_user.id, permissions=types.ChatPermissions(can_send_messages=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True, can_send_polls=True, can_pin_messages=True, can_invite_users=True, can_change_info=True, until_date=None))
                except Exception as e:
                    print(e)
                    break
                    
                    
                    
@dp.chat_member_handler()
async def chat_member_handler(update: types.ChatMemberUpdated):
    user_id = update.from_user.id
    username = update.from_user.username
    name = update.from_user.full_name
    chat_id = update.chat.id
    invite_new_chat_members = update.invite_link
    group_id = [-1001955925217]

    invite_link = await bot.create_chat_invite_link(update.chat.id)
    ChatMemberUpdated.new_chat_member = await bot.get_chat_member(chat_id=update.chat.id, user_id=update.from_user.id)


    for i in range(0, len(group_id)):
        print(invite_link)
        if invite_link in ChatMemberUpdated.new_chat_member:
                try:
                    await bot.restrict_chat_member(chat_id=group_id[i], user_id=update.from_user.id, permissions=types.ChatPermissions(can_send_messages=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True, can_send_polls=True, can_pin_messages=True, can_invite_users=True, can_change_info=True, until_date=None))
                except Exception as e:
                    print(e)
                    break

def register_handlers_client(dp: Dispatcher):
    dp.register_message_handler(get_handler, content_types=['photo', 'text'], state=FSMReport.photo)
    dp.register_message_handler(get_handler, content_types=['video', 'text'], state=FSMReport.video)
    dp.chat_member_handlers(get_handler, content_types=['photo', 'video', 'text'])
    dp.chat_member_handlers(chat_member_handler, types.ChatInviteLink, content_types=['photo', 'video', 'text'])
    dp.chat_member_handlers(chat_member_handler, types.ChatMemberUpdated, content_types=['photo', 'video', 'text'])
    dp.chat_join_request_handlers(join_request, types.ChatInviteLink, content_types=['photo', 'video', 'text'])
    dp.register_chat_join_request_handler(join_request, types.ChatInviteLink, content_types=['photo', 'video', 'text'])
    dp.register_chat_member_handler(chat_member_handler, types.ChatMemberUpdated, content_types=['photo', 'video', 'text'])
    

if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True, allowed_updates=types.AllowedUpdates.all())

Tell me how to remove restrictions, when subscribing, by invitation, for a group user, for a link creator.

Miki
  • 1
  • 1

0 Answers0