iShotaBot/modules/anti_channel.py

107 lines
4.2 KiB
Python
Raw Normal View History

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
from init import logs, user_me
init()
@Client.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
if (not isinstance(update, types.UpdateNewChannelMessage) or
not isinstance(update.message.from_id, types.PeerChannel)):
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
if ((message.fwd_from and
message.fwd_from.saved_from_peer == message.fwd_from.from_id == message.from_id) or
channel_id == chat_id):
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,
)
)
)
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
2022-07-25 09:35:54 +00:00
@Client.on_message(filters.incoming & filters.group &
2021-12-08 09:10:31 +00:00
filters.command(["anti_channel_msg", f"anti_channel_msg@{user_me.username}"]))
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
data = await client.get_chat_member(message.chat.id, user_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:
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:
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