Skip to content

Commit

Permalink
optimize pmpermit , afk , term and this -> closes UsergeTeam#76, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Jun 22, 2020
1 parent 7af37c6 commit 8b4e34d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 29 deletions.
1 change: 1 addition & 0 deletions userge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Config:
WELCOME_DELETE_TIMEOUT = 120
AUTOPIC_TIMEOUT = 300
ALLOWED_CHATS = Filters.chat([])
ALLOW_ALL_PMS = True
SUDO_USERS: Set[int] = set()
ALLOWED_COMMANDS: Set[str] = set()
UPSTREAM_REMOTE = 'upstream'
Expand Down
21 changes: 11 additions & 10 deletions userge/plugins/misc/gdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ def _get_file_id(self, filter_str: bool = False) -> tuple:
link = self._message.input_str
if filter_str:
link = self._message.filtered_input_str
found = re.search(r'https://drive.google.com/[\w\?\./&=]+([-\w]{33}|/0A[-\w]{17})', link)
found = re.search(
r'https://drive.google.com/[\w\?\./&=]+([-\w]{33}|(?<=/)0A[-\w]{17})', link)
if found and 'folder' in link:
out = (found.group(1), "folder")
elif found:
Expand Down Expand Up @@ -618,11 +619,11 @@ async def confirm_setup(self) -> None:
_AUTH_FLOW = None
await asyncio.gather(
_set_creds(cred),
self._message.edit("`Saved GDrive Creds!`", del_in=3, log=True))
self._message.edit("`Saved GDrive Creds!`", del_in=3, log=__name__))

async def clear(self) -> None:
""" Clear Creds """
await self._message.edit(await _clear_creds(), del_in=3, log=True)
await self._message.edit(await _clear_creds(), del_in=3, log=__name__)

async def set_parent(self) -> None:
""" Set Parent id """
Expand Down Expand Up @@ -672,7 +673,7 @@ async def make_folder(self) -> None:
await self._message.err(h_e._get_reason())
return
await self._message.edit(f"**Folder Created Successfully**\n\n{out}",
disable_web_page_preview=True, log=True)
disable_web_page_preview=True, log=__name__)

@creds_dec
async def list_folder(self) -> None:
Expand Down Expand Up @@ -807,7 +808,7 @@ async def upload(self) -> None:
out = self._output
else:
out = "`failed to upload.. check logs?`"
await self._message.edit(out, disable_web_page_preview=True, log=True)
await self._message.edit(out, disable_web_page_preview=True, log=__name__)

@creds_dec
async def download(self) -> None:
Expand Down Expand Up @@ -837,7 +838,7 @@ async def download(self) -> None:
out = self._output
else:
out = "`failed to download.. check logs?`"
await self._message.edit(out, disable_web_page_preview=True, log=True)
await self._message.edit(out, disable_web_page_preview=True, log=__name__)

@creds_dec
async def copy(self) -> None:
Expand Down Expand Up @@ -868,7 +869,7 @@ async def copy(self) -> None:
out = self._output
else:
out = "`failed to copy.. check logs?`"
await self._message.edit(out, disable_web_page_preview=True, log=True)
await self._message.edit(out, disable_web_page_preview=True, log=__name__)

@creds_dec
async def move(self) -> None:
Expand All @@ -885,7 +886,7 @@ async def move(self) -> None:
await self._message.err(h_e._get_reason())
else:
await self._message.edit(
f"`{file_id}` **Moved Successfully**\n\n{link}", log=True)
f"`{file_id}` **Moved Successfully**\n\n{link}", log=__name__)

@creds_dec
async def delete(self) -> None:
Expand All @@ -899,7 +900,7 @@ async def delete(self) -> None:
await self._message.err(h_e._get_reason())
else:
await self._message.edit(
f"`{file_id}` **Deleted Successfully**", del_in=5, log=True)
f"`{file_id}` **Deleted Successfully**", del_in=5, log=__name__)

@creds_dec
async def empty(self) -> None:
Expand All @@ -912,7 +913,7 @@ async def empty(self) -> None:
await self._message.err(h_e._get_reason())
else:
await self._message.edit(
"`Empty the Trash Successfully`", del_in=5, log=True)
"`Empty the Trash Successfully`", del_in=5, log=__name__)

@creds_dec
async def get(self) -> None:
Expand Down
8 changes: 4 additions & 4 deletions userge/plugins/misc/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ async def split_(message: Message) -> None:
m_s = (end_t - start_t).seconds
await message.edit(
f"**split** `{file_path}` into `{s_obj.final_file_path}` "
f"in `{m_s}` seconds.", log=True)
f"in `{m_s}` seconds.", log=__name__)


@userge.on_cmd('combine', about={
Expand Down Expand Up @@ -580,7 +580,7 @@ async def combine_(message: Message) -> None:
m_s = (end_t - start_t).seconds
await message.edit(
f"**combined** `{file_path}` into `{c_obj.final_file_path}` "
f"in `{m_s}` seconds.", log=True)
f"in `{m_s}` seconds.", log=__name__)


@userge.on_cmd('zip', about={
Expand Down Expand Up @@ -638,7 +638,7 @@ async def _pack_helper(message: Message, tar: bool = False) -> None:
m_s = (end_t - start_t).seconds
await message.edit(
f"**packed** `{file_path}` into `{p_obj.final_file_path}` "
f"in `{m_s}` seconds.", log=True)
f"in `{m_s}` seconds.", log=__name__)


@userge.on_cmd('unpack', about={
Expand Down Expand Up @@ -688,7 +688,7 @@ async def unpack_(message: Message) -> None:
m_s = (end_t - start_t).seconds
await message.edit(
f"**unpacked** `{file_path}` into `{p_obj.final_file_path}` "
f"in `{m_s}` seconds.", log=True)
f"in `{m_s}` seconds.", log=__name__)


@userge.on_cmd('packinfo', about={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import io
import sys
import shlex
import asyncio
import traceback
from getpass import getuser
Expand Down Expand Up @@ -194,8 +193,8 @@ async def worker(self) -> None:

@classmethod
async def execute(cls, cmd: str) -> 'Term':
process = await asyncio.create_subprocess_exec(
*shlex.split(cmd), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
process = await asyncio.create_subprocess_shell(
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
t_obj = cls(process)
asyncio.get_event_loop().create_task(t_obj.worker())
return t_obj
3 changes: 2 additions & 1 deletion userge/plugins/utils/afk.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ async def active_afk(message: Message) -> None:


@userge.on_filters(IS_AFK_FILTER & ~Filters.me & ~Filters.bot & (
Filters.mentioned | (Filters.private & ~Filters.service & Config.ALLOWED_CHATS)))
Filters.mentioned | (Filters.private & ~Filters.service & (
Filters.create(lambda _, __: Config.ALLOW_ALL_PMS) | Config.ALLOWED_CHATS))))
async def handle_afk_incomming(message: Message) -> None:
""" handle incomming messages when you afk """
user_id = message.from_user.id
Expand Down
57 changes: 46 additions & 11 deletions userge/plugins/utils/pmpermit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@
SAVED_SETTINGS = get_collection("CONFIGS")
ALLOWED_COLLECTION = get_collection("PM_PERMIT")

allowAllPms = True
pmCounter: Dict[int, int] = {}
allowAllFilter = Filters.create(lambda _, query: bool(allowAllPms))
allowAllFilter = Filters.create(lambda _, __: Config.ALLOW_ALL_PMS)
noPmMessage = ("Hello {fname} this is an automated message\n"
"Please wait untill you get approved to direct message "
"And please dont spam untill then ")
blocked_message = "**You were automatically blocked**"


async def _init() -> None:
global allowAllPms, noPmMessage
global noPmMessage, blocked_message
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:
allowAllPms = bool(_pm.get('data'))
Config.ALLOW_ALL_PMS = bool(_pm.get('data'))
_pmMsg = await SAVED_SETTINGS.find_one({'_id': 'CUSTOM NOPM MESSAGE'})
if _pmMsg:
noPmMessage = _pmMsg.get('data')
_blockPmMsg = await SAVED_SETTINGS.find_one({'_id': 'CUSTOM BLOCKPM MESSAGE'})
if _blockPmMsg:
blocked_message = _blockPmMsg.get('data')


@userge.on_cmd("allow", about={
Expand Down Expand Up @@ -110,16 +113,16 @@ async def get_id(message: Message):
"the previously allowed chats will be there !"}, allow_channels=False)
async def pmguard(message: Message):
""" enable or disable auto pm handler """
global allowAllPms, pmCounter
if allowAllPms:
allowAllPms = False
global pmCounter
if Config.ALLOW_ALL_PMS:
Config.ALLOW_ALL_PMS = False
await message.edit("`PM_guard activated`", del_in=3, log=__name__)
else:
allowAllPms = True
Config.ALLOW_ALL_PMS = True
await message.edit("`PM_guard deactivated`", del_in=3, log=__name__)
pmCounter.clear()
await SAVED_SETTINGS.update_one(
{'_id': 'PM GUARD STATUS'}, {"$set": {'data': allowAllPms}}, upsert=True)
{'_id': 'PM GUARD STATUS'}, {"$set": {'data': Config.ALLOW_ALL_PMS}}, upsert=True)


@userge.on_cmd("setpmmsg", about={
Expand All @@ -146,11 +149,43 @@ async def set_custom_nopm_message(message: Message):
{'_id': 'CUSTOM NOPM MESSAGE'}, {"$set": {'data': string}}, upsert=True)


@userge.on_cmd("setbpmmsg", about={
'header': "Sets the block message",
'description': "You can change the default blockPm message "
"which userge gives on un-invited PMs",
'options': {
'{fname}': "add first name",
'{lname}': "add last name",
'{flname}': "add full name",
'{uname}': "username",
'{chat}': "chat name",
'{mention}': "mention user"}}, allow_channels=False)
async def set_custom_blockpm_message(message: Message):
""" setup custom blockpm message """
global blocked_message
await message.edit('`Custom BLOCKpm message saved`', del_in=3, log=__name__)
if message.reply_to_message:
string = message.reply_to_message.text
else:
string = message.input_str
if string:
blocked_message = string
await SAVED_SETTINGS.update_one(
{'_id': 'CUSTOM BLOCKPM MESSAGE'}, {"$set": {'data': string}}, upsert=True)


@userge.on_cmd("vpmmsg", about={
'header': "Displays the reply message for uninvited PMs"}, allow_channels=False)
async def view_current_noPM_msg(message: Message):
""" view current pm message """
await message.edit('```' + noPmMessage + '```')
await message.edit(f"--current PM message--\n\n{noPmMessage}")


@userge.on_cmd("vbpmmsg", about={
'header': "Displays the reply message for blocked PMs"}, allow_channels=False)
async def view_current_blockPM_msg(message: Message):
""" view current block pm message """
await message.edit(f"--current blockPM message--\n\n{blocked_message}")


@userge.on_filters(~allowAllFilter & Filters.incoming & Filters.private & ~Filters.bot
Expand All @@ -164,7 +199,7 @@ async def uninvitedPmHandler(message: Message):
if message.from_user.id in pmCounter:
if pmCounter[message.from_user.id] > 3:
del pmCounter[message.from_user.id]
await message.reply("**You were automatically blocked**")
await message.reply(blocked_message)
await message.from_user.block()
await asyncio.sleep(1)
await CHANNEL.log(
Expand Down

0 comments on commit 8b4e34d

Please sign in to comment.