Untitled

 avatar
rain
python
2 years ago
7.7 kB
0
Indexable
Never
from pyrogram import Client, idle, filters
from pyrogram.enums.message_service_type import MessageServiceType
from pyrogram.enums.chat_member_status import ChatMemberStatus
from datetime import datetime, timedelta
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from dotenv import load_dotenv
import os
import asyncio

load_dotenv()

scheduler = AsyncIOScheduler()

api_id = int(os.environ.get("API_ID"))
api_hash = os.environ.get("API_HASH")
bot_token = os.environ.get("BOT_TOKEN")
base_group = int(os.environ.get("BASE_GROUP"))
force_group = int(os.environ.get("FORCE_GROUP"))

app = Client('melanirainila', api_id=api_id, api_hash=api_hash, bot_token=bot_token)

async def kick_member(user_id):
    time_start = datetime.now()
    time_end = time_start + timedelta(minutes=5)

    the_user = await app.get_users(user_id)
    the_user_in_chat = await app.get_chat_member(base_group, user_id)

    if the_user_in_chat.status == ChatMemberStatus.ADMINISTRATOR or the_user_in_chat.status == ChatMemberStatus.OWNER:
        surprise_message = await app.send_message(base_group, f"tidak bisa di kick karena {the_user.mention()} adalah seorang admin")
        scheduler.add_job(delete_message, "date", run_date=time_end, args=[surprise_message.id], id=f"{the_user.id}surprise_message")
    else:
        await app.ban_chat_member(base_group, user_id, time_end)
        remove_user_message = await app.send_message(base_group, f"{the_user.mention()} telah di kick!!")
        scheduler.add_job(delete_message, "date", run_date=time_end, args=[remove_user_message.id], id=f"{the_user.id}remove_user_message")

async def delete_message(message_id):
    await app.delete_messages(base_group, message_id)

@app.on_message(filters.service)
async def chat_members_handle(client, message):
    chat_invitelink = await app.get_chat(force_group)
    invitelink = chat_invitelink.invite_link
    time_start = datetime.now()
    time_end = time_start + timedelta(minutes=5)

    if invitelink is None:
        invitelink = await app.export_chat_invite_link(force_group)

    if message.chat.id == base_group and message.service == MessageServiceType.NEW_CHAT_MEMBERS:
        try:
            await app.get_chat_member(force_group, message.from_user.id)
        except:
            if scheduler.get_job(f"{message.from_user.id}new_welcome_delete"):
                scheduler.modify_job(f"{message.from_user.id}new_welcome_delete", next_run_time=datetime.now())
                scheduler.remove_job(f"{message.from_user.id}new_welcome_kick")

            force_join_message = await app.send_message(base_group, f"welcome {message.from_user.mention()}\n\ncepat join grup ini {invitelink} atau lu akan terkick dalam waktu 5 menit")

            scheduler.add_job(delete_message, "date", run_date=time_end, args=[force_join_message.id], id=f"{message.from_user.id}new_welcome_delete")
            scheduler.add_job(kick_member, "date", run_date=time_end, args=[message.from_user.id], id=f"{message.from_user.id}new_welcome_kick")

    if message.chat.id == force_group and message.service == MessageServiceType.NEW_CHAT_MEMBERS:
        if scheduler.get_job(f"{message.from_user.id}new_welcome_delete"):
            scheduler.modify_job(f"{message.from_user.id}new_welcome_delete", next_run_time=datetime.now())
            scheduler.remove_job(f"{message.from_user.id}new_welcome_kick")

            if scheduler.get_job(f"{message.from_user.id}cancel_kick"):
                scheduler.modify_job(f"{message.from_user.id}cancel_kick", next_run_time=datetime.now())

            cancel_kick_message = await app.send_message(base_group, f"oke {message.from_user.mention()} anda tidak jadi di kick")
            scheduler.add_job(delete_message, "date", run_date=time_end, args=[cancel_kick_message.id], id=f"{message.from_user.id}cancel_kick")

        if scheduler.get_job(f"{message.from_user.id}again_welcome_delete"):
            scheduler.modify_job(f"{message.from_user.id}again_welcome_delete", next_run_time=datetime.now())
            scheduler.remove_job(f"{message.from_user.id}again_welcome_kick")

            if scheduler.get_job(f"{message.from_user.id}cancel_kick"):
                scheduler.modify_job(f"{message.from_user.id}cancel_kick", next_run_time=datetime.now())

            cancel_kick_message = await app.send_message(base_group, f"{message.from_user.mention()} lu jangan keluar lagi cug")
            scheduler.add_job(delete_message, "date", run_date=time_end, args=[cancel_kick_message.id], id=f"{message.from_user.id}cancel_kick")

    if message.chat.id == base_group and message.service == MessageServiceType.LEFT_CHAT_MEMBERS:
        if scheduler.get_job(f"{message.from_user.id}new_welcome_delete"):
            scheduler.modify_job(f"{message.from_user.id}new_welcome_delete", next_run_time=datetime.now())
            scheduler.remove_job(f"{message.from_user.id}new_welcome_kick")

            if scheduler.get_job(f"{message.from_user.id}cancel_kick"):
                scheduler.modify_job(f"{message.from_user.id}cancel_kick", next_run_time=datetime.now())

            cancel_kick_message = await app.send_message(base_group, f"ternyata {message.from_user.mention()} udah keluar guys")
            scheduler.add_job(delete_message, "date", run_date=time_end, args=[cancel_kick_message.id], id=f"{message.from_user.id}cancel_kick")

        if  scheduler.get_job(f"{message.from_user.id}again_welcome_delete"):
            scheduler.modify_job(f"{message.from_user.id}again_welcome_delete", next_run_time=datetime.now())
            scheduler.remove_job(f"{message.from_user.id}again_welcome_kick")

            if scheduler.get_job(f"{message.from_user.id}cancel_kick"):
                scheduler.modify_job(f"{message.from_user.id}cancel_kick", next_run_time=datetime.now())

            cancel_kick_message = await app.send_message(base_group, f"ternyata {message.from_user.mention()} udah keluar guys")
            scheduler.add_job(delete_message, "date", run_date=time_end, args=[cancel_kick_message.id], id=f"{message.from_user.id}cancel_kick")

    if message.chat.id == force_group and message.service == MessageServiceType.LEFT_CHAT_MEMBERS:
        left_chat_message_time_start = datetime.now()
        left_chat_message_time_end = left_chat_message_time_start + timedelta(minutes=15)

        try:
            left_chat_message = await app.send_message(int(os.environ.get("LOG_GROUP")), f"{message.from_user.mention()} telah left brody")
            scheduler.add_job(delete_message, "date", run_date=left_chat_message_time_end, args=left_chat_message.id, id=f"{message.from_user.id}left_chat_message")
        except:
            pass

        if scheduler.get_job(f"{message.from_user.id}cancel_kick"):
                scheduler.modify_job(f"{message.from_user.id}cancel_kick", next_run_time=datetime.now())
                
        try:
            await app.get_chat_member(base_group, message.from_user.id)

            force_join_message = await app.send_message(base_group, f"brody {message.from_user.mention()}\nkarena lu telah keluar maka lu akan otomatis gw kick dalam 5 menit yahaha\n\nuntuk membatalkan silahkan gabung kembali {invitelink}")

            scheduler.add_job(delete_message, "date", run_date=time_end, args=[force_join_message.id], id=f"{message.from_user.id}again_welcome_delete")
            scheduler.add_job(kick_member, "date", run_date=time_end, args=[message.from_user.id], id=f"{message.from_user.id}again_welcome_kick")
        except:
            pass

scheduler.start()

app.run()