Skip to content

Commit

Permalink
few changes in alive + pmpermit (UsergeTeam#218)
Browse files Browse the repository at this point in the history
* few changes in alive

* added cmd to disbale inline pmpermit

* forgot comma

* wrong ext
  • Loading branch information
Krishna-Singhal authored Dec 26, 2020
1 parent 8ee5496 commit 4b106d5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
49 changes: 36 additions & 13 deletions userge/plugins/tools/alive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# All rights reserved.

import re
import os
import asyncio
from typing import Tuple, Optional

Expand Down Expand Up @@ -105,18 +106,7 @@ async def _send_alive(message: Message,
await _refresh_id(message)
should_mark = None if _IS_STICKER else reply_markup
if _IS_TELEGRAPH:
try:
await message.client.send_document(chat_id=message.chat.id,
document=Config.ALIVE_MEDIA,
caption=text,
reply_markup=should_mark)
except SlowmodeWait as s_m:
await asyncio.sleep(s_m.x)
text = f'<b>{str(s_m).replace(" is ", " was ")}</b>\n\n{text}'
return await _send_alive(message, text, reply_markup)
except Exception:
_LOG.error('Telegraph Link Invalid')
_set_data(True)
await _send_telegraph(message, text, reply_markup)
else:
try:
await message.client.send_cached_media(chat_id=message.chat.id,
Expand Down Expand Up @@ -159,7 +149,7 @@ def _set_data(errored: bool = False) -> None:
global _CHAT, _MSG_ID, _IS_TELEGRAPH # pylint: disable=global-statement

pattern_1 = r"^(http(?:s?):\/\/)?(www\.)?(t.me)(\/c\/(\d+)|:?\/(\w+))?\/(\d+)$"
pattern_2 = r"^(http(?:s?):\/\/)?(www\.)?telegra\.ph\/([A-Za-z0-9\-]*)$"
pattern_2 = r"^(http(?:s?):\/\/)?(www\.)?telegra\.ph\/file\/([A-Za-z0-9\-]*)$"
if Config.ALIVE_MEDIA and not errored:
if Config.ALIVE_MEDIA.lower().strip() == "nothing":
_CHAT = "text_format"
Expand All @@ -184,3 +174,36 @@ def _set_data(errored: bool = False) -> None:
match = re.search(pattern_1, _DEFAULT)
_CHAT = match.group(6)
_MSG_ID = int(match.group(7))


async def _send_telegraph(msg: Message, text: str, reply_markup: Optional[InlineKeyboardMarkup]):
link = Config.ALIVE_MEDIA
try:
dls_loc = os.path.join(
Config.DOWN_PATH,
os.path.basename(await msg.client.download_media(link, Config.DOWN_PATH))
)
if dls_loc.name.lower().endswith((".jpg", ".jpeg", ".png", ".bmp")):
await msg.client.send_photo(
chat_id=msg.chat.id,
photo=dls_loc,
caption=text,
reply_markup=reply_markup
)
elif dls_loc.name.lower().endswith((".mkv", ".mp4", ".webm")):
await msg.client.send_video(
chat_id=msg.chat.id,
video=dls_loc,
caption=text,
reply_markup=reply_markup
)
else:
await msg.client.send_document(
chat_id=msg.chat.id,
document=dls_loc,
caption=text,
reply_markup=reply_markup
)
os.remove(dls_loc)
except Exception as err:
await msg.err(str(err), log=True)
27 changes: 25 additions & 2 deletions userge/plugins/utils/pmpermit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ALLOWED_COLLECTION = get_collection("PM_PERMIT")

pmCounter: Dict[int, int] = {}
_IS_INLINE = True
allowAllFilter = filters.create(lambda _, __, ___: Config.ALLOW_ALL_PMS)
noPmMessage = bk_noPmMessage = ("Hello {fname} this is an automated message\n"
"Please wait until you get approved to direct message "
Expand All @@ -29,12 +30,15 @@


async def _init() -> None:
global noPmMessage, blocked_message # pylint: disable=global-statement
global noPmMessage, blocked_message, _IS_INLINE # pylint: disable=global-statement
async for chat in ALLOWED_COLLECTION.find({"status": 'allowed'}):
Config.ALLOWED_CHATS.add(chat.get("_id"))
_pm = await SAVED_SETTINGS.find_one({'_id': 'PM GUARD STATUS'})
if _pm:
Config.ALLOW_ALL_PMS = bool(_pm.get('data'))
i_pm = await SAVED_SETTINGS.find_one({'_id': 'INLINE_PM_PERMIT'})
if i_pm:
_IS_INLINE = bool(i_pm.get('data'))
_pmMsg = await SAVED_SETTINGS.find_one({'_id': 'CUSTOM NOPM MESSAGE'})
if _pmMsg:
noPmMessage = _pmMsg.get('data')
Expand Down Expand Up @@ -129,6 +133,25 @@ async def pmguard(message: Message):
{'_id': 'PM GUARD STATUS'}, {"$set": {'data': Config.ALLOW_ALL_PMS}}, upsert=True)


@userge.on_cmd(
"ipmguard", about={
'header': "Switchs the Inline pm permiting module on",
'description': "This is switched off in default.",
'usage': "{tr}ipmguard"},
allow_channels=False)
async def ipmguard(message: Message):
""" enable or disable inline pmpermit """
global _IS_INLINE # pylint: disable=global-statement
if _IS_INLINE:
_IS_INLINE = False
await message.edit("`Inline PM_guard activated`", del_in=3, log=__name__)
else:
_IS_INLINE = True
await message.edit("`Inline PM_guard deactivated`", del_in=3, log=__name__)
await SAVED_SETTINGS.update_one(
{'_id': 'INLINE_PM_PERMIT'}, {"$set": {'data': _IS_INLINE}}, upsert=True)


@userge.on_cmd("setpmmsg", about={
'header': "Sets the reply message",
'description': "You can change the default message which userge gives on un-invited PMs",
Expand Down Expand Up @@ -245,7 +268,7 @@ async def uninvitedPmHandler(message: Message):
"Please wait until you get approved to pm !", del_in=5)
else:
pmCounter.update({message.from_user.id: 1})
if userge.has_bot:
if userge.has_bot and _IS_INLINE:
try:
bot_username = (await userge.bot.get_me()).username
k = await userge.get_inline_bot_results(bot_username, "pmpermit")
Expand Down

0 comments on commit 4b106d5

Please sign in to comment.