Skip to content

Commit

Permalink
some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Nov 25, 2020
1 parent 5c773dd commit 74bc329
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ resources/base_profile_pic.jpg
resources/mdfy_profile_pic.jpg
pictest.py
resources/MutantAcademyBB.ttf
userge/plugins/dev/
22 changes: 18 additions & 4 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
FROM gitpod/workspace-full
FROM gitpod/workspace-full-vnc

# Simply install Heroku CLI
# Just in case we use Gitpod for deployment.
RUN curl https://cli-assets.heroku.com/install.sh | -
RUN sudo apt-get update
RUN sudo apt-get install -y \
libasound2-dev \
libgtk-3-dev \
libnss3-dev \
curl \
git \
gnupg2 \
unzip \
wget \
neofetch \
ffmpeg \
jq

RUN curl -sO https://cli-assets.heroku.com/install.sh && bash install.sh && rm install.sh

RUN sudo rm -rf /var/lib/apt/lists/*
2 changes: 1 addition & 1 deletion init/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ upgradePip() {
}

installReq() {
pip3 install -r $1/requirements.txt &> /dev/null
pip3 install --use-feature=2020-resolver -r $1/requirements.txt &> /dev/null
}

printLine() {
Expand Down
7 changes: 4 additions & 3 deletions tests/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
from userge import userge


async def worker() -> None: # pylint: disable=missing-function-docstring
async def _worker() -> None:
chat_id = int(os.environ.get("CHAT_ID") or 0)
await userge.send_message(chat_id, '`build completed !`')
type_ = 'unofficial' if os.path.exists("../userge/plugins/unofficial") else 'main'
await userge.send_message(chat_id, f'`{type_} build completed !`')

if __name__ == "__main__":
userge.begin(worker())
userge.begin(_worker())
print('userge test has been finished!')
89 changes: 51 additions & 38 deletions userge/core/methods/decorators/raw_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
_U_AD_CHT: Dict[int, ChatMember] = {}
_U_NM_CHT: Dict[int, ChatMember] = {}

_CH_LKS: Dict[str, asyncio.Lock] = {}
_CH_LKS_LK = asyncio.Lock()
_INIT_LK = asyncio.Lock()


async def _update_u_cht(r_m: RawMessage) -> ChatMember:
if r_m.chat.id not in {**_U_AD_CHT, **_U_NM_CHT}:
Expand Down Expand Up @@ -88,20 +92,21 @@ async def _init(r_c: Union['_client.Userge', '_client._UsergeBot'],
global _U_ID, _B_ID # pylint: disable=global-statement
if r_m.from_user and (r_m.from_user.is_self
or (r_m.from_user.id in Config.SUDO_USERS)
or (r_m.from_user.id == Config.OWNER_ID)):
or (r_m.from_user.id in Config.OWNER_ID)):
RawClient.LAST_OUTGOING_TIME = time.time()
if _U_ID and _B_ID:
return
if isinstance(r_c, _client.Userge):
if not _U_ID:
_U_ID = (await r_c.get_me()).id
if RawClient.DUAL_MODE and not _B_ID:
_B_ID = (await r_c.bot.get_me()).id
else:
if not _B_ID:
_B_ID = (await r_c.get_me()).id
if RawClient.DUAL_MODE and not _U_ID:
_U_ID = (await r_c.ubot.get_me()).id
async with _INIT_LK:
if _U_ID and _B_ID:
return
if isinstance(r_c, _client.Userge):
if not _U_ID:
_U_ID = (await r_c.get_me()).id
if RawClient.DUAL_MODE and not _B_ID:
_B_ID = (await r_c.bot.get_me()).id
else:
if not _B_ID:
_B_ID = (await r_c.get_me()).id
if RawClient.DUAL_MODE and not _U_ID:
_U_ID = (await r_c.ubot.get_me()).id


async def _raise_func(r_c: Union['_client.Userge', '_client._UsergeBot'],
Expand Down Expand Up @@ -130,6 +135,26 @@ async def _is_admin(r_c: Union['_client.Userge', '_client._UsergeBot'],
return r_m.chat.id in _B_AD_CHT


def _get_chat_member(r_c: Union['_client.Userge', '_client._UsergeBot'],
r_m: RawMessage) -> Optional[ChatMember]:
if r_m.chat.type in ("private", "bot"):
return None
if isinstance(r_c, _client.Userge):
if r_m.chat.id in _U_AD_CHT:
return _U_AD_CHT[r_m.chat.id]
return _U_NM_CHT[r_m.chat.id]
if r_m.chat.id in _B_AD_CHT:
return _B_AD_CHT[r_m.chat.id]
return _B_NM_CHT[r_m.chat.id]


async def _get_lock(key: str) -> asyncio.Lock:
async with _CH_LKS_LK:
if key not in _CH_LKS:
_CH_LKS[key] = asyncio.Lock()
return _CH_LKS[key]


async def _bot_is_present(r_c: Union['_client.Userge', '_client._UsergeBot'],
r_m: RawMessage) -> bool:
global _TASK_2_START_TO # pylint: disable=global-statement
Expand All @@ -149,19 +174,6 @@ async def _bot_is_present(r_c: Union['_client.Userge', '_client._UsergeBot'],
return r_m.chat.id in _B_CMN_CHT


def _get_chat_member(r_c: Union['_client.Userge', '_client._UsergeBot'],
r_m: RawMessage) -> Optional[ChatMember]:
if r_m.chat.type in ("private", "bot"):
return None
if isinstance(r_c, _client.Userge):
if r_m.chat.id in _U_AD_CHT:
return _U_AD_CHT[r_m.chat.id]
return _U_NM_CHT[r_m.chat.id]
if r_m.chat.id in _B_AD_CHT:
return _B_AD_CHT[r_m.chat.id]
return _B_NM_CHT[r_m.chat.id]


async def _both_are_admins(r_c: Union['_client.Userge', '_client._UsergeBot'],
r_m: RawMessage) -> bool:
if not await _bot_is_present(r_c, r_m):
Expand Down Expand Up @@ -278,18 +290,19 @@ async def template(r_c: Union['_client.Userge', '_client._UsergeBot'],
if (flt.check_client
or (r_m.from_user and r_m.from_user.id in Config.SUDO_USERS)):
cond = True
if flt.only_admins:
cond = cond and await _both_are_admins(r_c, r_m)
if flt.check_perm:
cond = cond and await _both_have_perm(flt, r_c, r_m)
if cond:
if Config.USE_USER_FOR_CLIENT_CHECKS:
# pylint: disable=protected-access
if isinstance(r_c, _client._UsergeBot):
return
elif await _bot_is_present(r_c, r_m):
if isinstance(r_c, _client.Userge):
return
async with await _get_lock(str(flt)):
if flt.only_admins:
cond = cond and await _both_are_admins(r_c, r_m)
if flt.check_perm:
cond = cond and await _both_have_perm(flt, r_c, r_m)
if cond:
if Config.USE_USER_FOR_CLIENT_CHECKS:
# pylint: disable=protected-access
if isinstance(r_c, _client._UsergeBot):
return
elif await _bot_is_present(r_c, r_m):
if isinstance(r_c, _client.Userge):
return
if flt.check_downpath and not os.path.isdir(Config.DOWN_PATH):
os.makedirs(Config.DOWN_PATH)
try:
Expand Down
3 changes: 2 additions & 1 deletion userge/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

def get_all_plugins() -> List[str]:
""" list all plugins """
plugins = get_import_path(ROOT, "/" if len(sys.argv) == 2 and sys.argv[1] == 'dev' else "/**/")
plugins = get_import_path(
ROOT, "/dev/" if len(sys.argv) == 2 and sys.argv[1] == 'dev' else "/**/")
_LOG.debug("All Available Plugins: %s", plugins)
return list(plugins)
3 changes: 2 additions & 1 deletion userge/plugins/misc/gdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,8 @@ async def del_perms(self) -> None:


@userge.on_cmd("gsetup", about={
'header': "Setup GDrive Creds"})
'header': "Setup GDrive Creds"},
allow_groups=False, allow_private=False, allow_bots=False)
async def gsetup_(message: Message):
""" setup creds """
await Worker(message).setup()
Expand Down
2 changes: 1 addition & 1 deletion userge/plugins/misc/instadl.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_caption(post: Post) -> str:
header = f'♥️`{post.likes}` 💬`{post.comments}`'
if post.is_video:
header += f' 👀`{post.video_view_count}`'
caption = header + '\n\n' + caption
caption = header + '\n\n' + (caption or '')
return caption


Expand Down
26 changes: 20 additions & 6 deletions userge/plugins/tools/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@
#
# All rights reserved.

import asyncio
from datetime import datetime

from userge import userge, Message


@userge.on_cmd(
"ping", about={'header': "check how long it takes to ping your userbot"})
@userge.on_cmd("ping", about={
'header': "check how long it takes to ping your userbot",
'flags': {'-a': "average ping"}}, group=-1)
async def pingme(message: Message):
start = datetime.now()
await message.edit('`Pong!`')
end = datetime.now()
m_s = (end - start).microseconds / 1000
await message.edit(f"**Pong!**\n`{m_s} ms`")
if '-a' in message.flags:
await message.edit('`!....`')
await asyncio.sleep(0.3)
await message.edit('`..!..`')
await asyncio.sleep(0.3)
await message.edit('`....!`')
end = datetime.now()
t_m_s = (end - start).microseconds / 1000
m_s = round((t_m_s - 0.6) / 3, 3)
await message.edit(f"**AvePong!**\n`{m_s} ms`")
else:
await message.edit('`Pong!`')
end = datetime.now()
m_s = (end - start).microseconds / 1000
await message.edit(f"**Pong!**\n`{m_s} ms`")

0 comments on commit 74bc329

Please sign in to comment.