Skip to content

Commit

Permalink
update pyrogram
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe authored Aug 6, 2022
1 parent 8f8803e commit a4e55b8
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 41 deletions.
21 changes: 15 additions & 6 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Client,
__version__
)
from pyrogram.enums import ParseMode
from . import (
API_HASH,
APP_ID,
Expand All @@ -34,33 +35,41 @@

class Bot(Client):
""" modded client for MessageDeletERoBot """
BOT_ID: int = None
USER: User = None
USER_ID: int = None

def __init__(self):
super().__init__(
TG_BOT_SESSION,
name=TG_BOT_SESSION,
api_hash=API_HASH,
api_id=APP_ID,
plugins={
"root": "bot/plugins"
"root": "bot.plugins",
"exclude": [
"oatc"
]
},
workers=TG_BOT_WORKERS,
bot_token=TG_BOT_TOKEN,
sleep_threshold=TG_SLEEP_THRESHOLD
sleep_threshold=TG_SLEEP_THRESHOLD,
parse_mode=ParseMode.HTML
)
self.LOGGER = LOGGER

async def start(self):
await super().start()
usr_bot_me = await self.get_me()
self.set_parse_mode("html")
usr_bot_me = self.me
self.BOT_ID = usr_bot_me.id
self.LOGGER(__name__).info(
f"@{usr_bot_me.username} based on Pyrogram v{__version__} "
)
self.USER, self.USER_ID = await User().start()
# hack to get the entities in-memory
await self.USER.send_message(usr_bot_me.username, "this is a hack")
await self.USER.send_message(
usr_bot_me.username,
"join https://t.me/SpEcHlDe/857"
)

async def stop(self, *args):
await super().stop()
Expand Down
3 changes: 2 additions & 1 deletion bot/helpers/custom_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from pyrogram import filters
from pyrogram.enums import ChatType
from pyrogram.types import Message


async def allowed_chat_filter_fn(_, __, m: Message):
return bool(m.chat and m.chat.type in {"channel", "supergroup"})
return bool(m.chat and m.chat.type in [ChatType.CHANNEL, ChatType.SUPERGROUP])


allowed_chat_filter = filters.create(allowed_chat_filter_fn)
8 changes: 4 additions & 4 deletions bot/helpers/get_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ async def get_messages(
limit=None
):
if (
min_message_id <= msg.message_id and
max_message_id >= msg.message_id
min_message_id <= msg.id and
max_message_id >= msg.id
):
if len(filter_type_s) > 0:
for filter_type in filter_type_s:
obj = getattr(msg, filter_type)
if obj:
messages_to_delete.append(msg.message_id)
messages_to_delete.append(msg.id)
else:
messages_to_delete.append(msg.message_id)
messages_to_delete.append(msg.id)
# append to the list, based on the condition
if len(messages_to_delete) > TG_MAX_SEL_MESG:
await mass_delete_messages(
Expand Down
6 changes: 4 additions & 2 deletions bot/helpers/gulmnek.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from pyrogram.types import Message
from pyrogram.enums import MessageEntityType
from .delall_bot_links import extract_c_m_ids


Expand All @@ -34,11 +35,12 @@ def knemblook(
)
if message and text and len(entities) > 0:
for one_entity in entities:
if one_entity.type == "url":
_url = None
if one_entity.type == MessageEntityType.URL:
_url = text[
one_entity.offset:one_entity.offset + one_entity.length
]
elif one_entity.type == "text_link":
elif one_entity.type == MessageEntityType.TEXT_LINK:
_url = one_entity.url
if _url:
chat_id, message_id = extract_c_m_ids(_url)
Expand Down
6 changes: 4 additions & 2 deletions bot/helpers/make_user_join_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.


from asyncio import sleep
from pyrogram.errors import (
InviteHashExpired,
InviteHashInvalid,
UserAlreadyParticipant
)
from pyrogram.enums import ChatMemberStatus
from pyrogram.types import Message
from bot.bot import Bot

Expand All @@ -36,8 +37,9 @@ async def make_chat_user_join(
pass
except (InviteHashExpired, InviteHashInvalid) as e:
return False, str(e)
await sleep(7)
_existing_permissions = await message.chat.get_member(user_id)
if _existing_permissions.status == "creator":
if _existing_permissions.status == ChatMemberStatus.OWNER:
return True, 140
if not _existing_permissions.can_delete_messages:
await message.chat.promote_member(
Expand Down
31 changes: 16 additions & 15 deletions bot/plugins/del_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from pyrogram import filters
from pyrogram.types import Message
from pyrogram.errors import (
ChatAdminRequired
)
from pyrogram.raw.functions.channels import DeleteHistory
from pyrogram.types import Message
from bot import (
BEGINNING_DEL_ALL_MESSAGE,
DEL_ALL_COMMAND,
Expand Down Expand Up @@ -80,21 +81,21 @@ async def del_all_command_fn(client: Bot, message: Message):
max_id=0
)
)
return

await get_messages(
client.USER,
message.chat.id,
0,
status_message.message_id if status_message else message.message_id,
[]
)

# leave the chat, after task is done
if SHTL_USR_HCAT_QO:
await client.USER.leave_chat(message.chat.id)
if SHTL_BOT_HCAT_QO:
await client.leave_chat(message.chat.id)
else:
await get_messages(
client.USER,
message.chat.id,
0,
status_message.id if status_message else message.id,
[]
)

# leave the chat, after task is done
if SHTL_USR_HCAT_QO:
await client.USER.leave_chat(message.chat.id)
if SHTL_BOT_HCAT_QO:
await client.leave_chat(message.chat.id)

# edit with channel message ads,
# after process is completed
Expand Down
2 changes: 1 addition & 1 deletion bot/plugins/del_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def del_from_command_fn(client: Bot, message: Message):
message.chat.id
][
DEL_FROM_COMMAND
] = message.reply_to_message.message_id
] = message.reply_to_message.id
if status_message:
await status_message.edit_text(
"saved starting message_id. "
Expand Down
5 changes: 3 additions & 2 deletions bot/plugins/del_selective.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ async def del_selective_command_fn(client: Bot, message: Message):
DEL_TO_COMMAND
)
except AttributeError:
max_message_id = status_message.message_id if \
status_message else message.message_id
max_message_id = status_message.id if \
status_message else message.id

await get_messages(
client.USER,
Expand All @@ -108,6 +108,7 @@ async def del_selective_command_fn(client: Bot, message: Message):
try:
if status_message:
await status_message.delete()
status_message = None
await message.delete()
except: # noqa: E722
pass
Expand Down
2 changes: 1 addition & 1 deletion bot/plugins/del_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def del_to_command_fn(client: Bot, message: Message):
message.chat.id
][
DEL_TO_COMMAND
] = message.reply_to_message.message_id
] = message.reply_to_message.id
if status_message:
await status_message.edit_text(
"saved ending message_id. "
Expand Down
15 changes: 9 additions & 6 deletions bot/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Client,
__version__
)
from pyrogram.enums import ParseMode
from . import (
API_HASH,
APP_ID,
Expand All @@ -35,23 +36,25 @@ class User(Client):

def __init__(self):
super().__init__(
TG_USER_SESSION,
name="DeleteUser",
in_memory=True,
session_string=TG_USER_SESSION,
api_hash=API_HASH,
api_id=APP_ID,
workers=TG_BOT_WORKERS,
sleep_threshold=TG_SLEEP_THRESHOLD
sleep_threshold=TG_SLEEP_THRESHOLD,
parse_mode=ParseMode.HTML
)
self.LOGGER = LOGGER

async def start(self):
await super().start()
usr_bot_me = await self.get_me()
self.set_parse_mode("html")
usr_bot_me = self.me
self.LOGGER(__name__).info(
f"@{usr_bot_me.username} based on Pyrogram v{__version__} "
f"{usr_bot_me} based on Pyrogram v{__version__} "
)
return (self, usr_bot_me.id)

async def stop(self, *args):
await super().stop()
self.LOGGER(__name__).info("Bot stopped. Bye.")
self.LOGGER(__name__).info("User stopped. Bye.")
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Pyrogram==1.4.15
Pyrogram==2.0.35
TgCrypto==1.2.3

python-dotenv>=0.10

0 comments on commit a4e55b8

Please sign in to comment.