2022-07-25 09:35:54 +00:00
|
|
|
import contextlib
|
2021-12-08 09:10:31 +00:00
|
|
|
from pyrogram import Client, filters, ContinuePropagation, errors
|
2022-07-25 09:39:02 +00:00
|
|
|
from pyrogram.enums import ChatMemberStatus
|
2021-12-08 09:10:31 +00:00
|
|
|
from pyrogram.types import Message
|
|
|
|
from pyrogram.raw import types, functions
|
|
|
|
from pyrogram.raw.base import Update
|
|
|
|
|
|
|
|
from asyncio import sleep
|
|
|
|
|
|
|
|
from defs.anti_channel import init, add, get_status, check_status, clean
|
2023-06-23 15:37:55 +00:00
|
|
|
from init import logs, bot
|
2021-12-08 09:10:31 +00:00
|
|
|
|
|
|
|
init()
|
|
|
|
|
|
|
|
|
2023-06-23 15:37:55 +00:00
|
|
|
@bot.on_raw_update(filters.incoming & filters.group)
|
2022-07-25 09:35:54 +00:00
|
|
|
async def anti_channel_msg(client: Client, update: Update, _, __: dict):
|
2021-12-08 09:10:31 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
# Check for message that are from channel
|
2023-01-12 13:19:54 +00:00
|
|
|
if not isinstance(update, types.UpdateNewChannelMessage) or not isinstance(
|
|
|
|
update.message.from_id, types.PeerChannel
|
|
|
|
):
|
2021-12-08 09:10:31 +00:00
|
|
|
raise ContinuePropagation
|
|
|
|
|
|
|
|
# Basic data
|
|
|
|
message = update.message
|
|
|
|
chat_id = int(f"-100{message.peer_id.channel_id}")
|
|
|
|
|
|
|
|
# Check if open
|
|
|
|
if not get_status(chat_id):
|
|
|
|
raise ContinuePropagation
|
|
|
|
|
|
|
|
channel_id = int(f"-100{message.from_id.channel_id}")
|
|
|
|
|
|
|
|
# Check for linked channel
|
2023-01-12 13:19:54 +00:00
|
|
|
if (
|
|
|
|
message.fwd_from
|
|
|
|
and message.fwd_from.saved_from_peer
|
|
|
|
== message.fwd_from.from_id
|
|
|
|
== message.from_id
|
|
|
|
) or channel_id == chat_id:
|
2021-12-08 09:10:31 +00:00
|
|
|
raise ContinuePropagation
|
|
|
|
|
|
|
|
# Check if blocked
|
|
|
|
if check_status(chat_id, channel_id):
|
|
|
|
raise ContinuePropagation
|
|
|
|
|
|
|
|
# Delete the message sent by channel and ban it.
|
2022-07-25 09:35:54 +00:00
|
|
|
await client.invoke(
|
2021-12-08 09:10:31 +00:00
|
|
|
functions.channels.EditBanned(
|
|
|
|
channel=await client.resolve_peer(chat_id),
|
|
|
|
participant=await client.resolve_peer(channel_id),
|
|
|
|
banned_rights=types.ChatBannedRights(
|
|
|
|
until_date=0,
|
|
|
|
view_messages=True,
|
|
|
|
send_messages=True,
|
|
|
|
send_media=True,
|
|
|
|
send_stickers=True,
|
|
|
|
send_gifs=True,
|
|
|
|
send_games=True,
|
|
|
|
send_polls=True,
|
2023-01-12 13:19:54 +00:00
|
|
|
),
|
2021-12-08 09:10:31 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
await client.delete_messages(chat_id, message.id)
|
|
|
|
add(chat_id, channel_id)
|
|
|
|
raise ContinuePropagation
|
|
|
|
except errors.FloodWait as e:
|
2022-07-25 09:35:54 +00:00
|
|
|
logs.debug(f"{e}, retry after {e.value} seconds...")
|
|
|
|
await sleep(e.value)
|
|
|
|
except errors.ChatAdminRequired as e:
|
|
|
|
with contextlib.suppress(NameError):
|
2021-12-08 09:10:31 +00:00
|
|
|
clean(chat_id) # noqa
|
2022-07-25 09:35:54 +00:00
|
|
|
raise ContinuePropagation from e
|
|
|
|
except ContinuePropagation as e:
|
|
|
|
raise ContinuePropagation from e
|
2021-12-08 09:10:31 +00:00
|
|
|
|
|
|
|
|
2023-06-23 15:37:55 +00:00
|
|
|
@bot.on_message(
|
2023-01-12 13:19:54 +00:00
|
|
|
filters.incoming
|
|
|
|
& filters.group
|
2023-06-23 15:37:55 +00:00
|
|
|
& filters.command(["anti_channel_msg", f"anti_channel_msg@{bot.me.username}"])
|
2023-01-12 13:19:54 +00:00
|
|
|
)
|
2021-12-08 09:10:31 +00:00
|
|
|
async def switch_anti_channel_msg(client: Client, message: Message):
|
|
|
|
# Check user
|
|
|
|
if message.from_user:
|
|
|
|
data = await client.get_chat_member(message.chat.id, message.from_user.id)
|
2022-07-25 09:39:02 +00:00
|
|
|
if data.status not in [ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER]:
|
2021-12-08 09:10:31 +00:00
|
|
|
await message.reply("You are not an admin of this chat.")
|
|
|
|
raise ContinuePropagation
|
|
|
|
# Check self
|
2023-06-23 15:37:55 +00:00
|
|
|
data = await client.get_chat_member(message.chat.id, bot.me.id)
|
2022-07-25 09:39:02 +00:00
|
|
|
if data.status not in [ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER]:
|
2021-12-08 09:10:31 +00:00
|
|
|
await message.reply("I'm not an admin of this chat.")
|
|
|
|
raise ContinuePropagation
|
2022-07-25 09:35:54 +00:00
|
|
|
switch = len(message.text.split(" ")) > 1
|
|
|
|
if get_status(message.chat.id):
|
2021-12-08 09:10:31 +00:00
|
|
|
if switch:
|
|
|
|
clean(message.chat.id)
|
|
|
|
await message.reply("Anti-channel is now disabled.")
|
|
|
|
else:
|
2023-01-12 13:19:54 +00:00
|
|
|
await message.reply(
|
|
|
|
"Anti-channel is already enabled.\n"
|
|
|
|
"\n"
|
|
|
|
"Tips: Use `/anti_channel_msg true` to enable or disable it."
|
|
|
|
)
|
2022-07-25 09:35:54 +00:00
|
|
|
elif switch:
|
|
|
|
add(message.chat.id, message.chat.id)
|
|
|
|
await message.reply("Anti-channel is now enabled.")
|
|
|
|
else:
|
2023-01-12 13:19:54 +00:00
|
|
|
await message.reply(
|
|
|
|
"Anti-channel is already disabled.\n"
|
|
|
|
"\n"
|
|
|
|
"Tips: Use `/anti_channel_msg true` to enable or disable it."
|
|
|
|
)
|
2021-12-08 09:10:31 +00:00
|
|
|
raise ContinuePropagation
|