Skip to content

Commit

Permalink
some enhancements in core and plugins + closes UsergeTeam#7
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Aug 6, 2020
1 parent a3833a3 commit 0b2cd3f
Show file tree
Hide file tree
Showing 23 changed files with 865 additions and 1,070 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Userge 🔥

[![Build Status](https://travis-ci.com/UsergeTeam/Userge.svg?branch=dev)](https://travis-ci.com/UsergeTeam/Userge) ![Python Version](https://img.shields.io/badge/python-3.7/3.8-lightgrey) ![Release](https://img.shields.io/github/v/release/UsergeTeam/Userge) ![Stars](https://img.shields.io/github/stars/UsergeTeam/Userge) ![Forks](https://img.shields.io/github/forks/UsergeTeam/Userge) ![Issues Open](https://img.shields.io/github/issues/UsergeTeam/Userge) ![Issues Closed](https://img.shields.io/github/issues-closed/UsergeTeam/Userge) ![PR Open](https://img.shields.io/github/issues-pr/UsergeTeam/Userge) ![PR Closed](https://img.shields.io/github/issues-pr-closed/UsergeTeam/Userge) ![Contributors](https://img.shields.io/github/contributors/UsergeTeam/Userge) ![Repo Size](https://img.shields.io/github/repo-size/UsergeTeam/Userge) ![License](https://img.shields.io/github/license/UsergeTeam/Userge) ![Commit Activity](https://img.shields.io/github/commit-activity/m/UsergeTeam/Userge) [![Plugins Repo!](https://img.shields.io/badge/Plugins%20Repo-!-orange)](https://github.com/UsergeTeam/Userge-Plugins) [![Join Channel!](https://img.shields.io/badge/Join%20Channel-!-red)](https://t.me/theUserge) [![DeepSource](https://static.deepsource.io/deepsource-badge-light-mini.svg)](https://deepsource.io/gh/UsergeTeam/Userge/?ref=repository-badge)
[![Build Status](https://travis-ci.com/UsergeTeam/Userge.svg?branch=dev)](https://travis-ci.com/UsergeTeam/Userge) ![Python Version](https://img.shields.io/badge/python-3.8-lightgrey) ![Release](https://img.shields.io/github/v/release/UsergeTeam/Userge) ![Stars](https://img.shields.io/github/stars/UsergeTeam/Userge) ![Forks](https://img.shields.io/github/forks/UsergeTeam/Userge) ![Issues Open](https://img.shields.io/github/issues/UsergeTeam/Userge) ![Issues Closed](https://img.shields.io/github/issues-closed/UsergeTeam/Userge) ![PR Open](https://img.shields.io/github/issues-pr/UsergeTeam/Userge) ![PR Closed](https://img.shields.io/github/issues-pr-closed/UsergeTeam/Userge) ![Contributors](https://img.shields.io/github/contributors/UsergeTeam/Userge) ![Repo Size](https://img.shields.io/github/repo-size/UsergeTeam/Userge) ![License](https://img.shields.io/github/license/UsergeTeam/Userge) ![Commit Activity](https://img.shields.io/github/commit-activity/m/UsergeTeam/Userge) [![Plugins Repo!](https://img.shields.io/badge/Plugins%20Repo-!-orange)](https://github.com/UsergeTeam/Userge-Plugins) [![Join Channel!](https://img.shields.io/badge/Join%20Channel-!-red)](https://t.me/theUserge) [![DeepSource](https://static.deepsource.io/deepsource-badge-light-mini.svg)](https://deepsource.io/gh/UsergeTeam/Userge/?ref=repository-badge)

> **Userge** is a Powerful , _Pluggable_ Telegram UserBot written in _Python_ using [Pyrogram](https://github.com/pyrogram/pyrogram).
Expand Down
2 changes: 1 addition & 1 deletion init/checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _checkBashReq() {

_checkPythonVersion() {
log "Checking Python Version ..."
test $(sed 's/\.//g' <<< $pVer) -lt 380 \
( test -z $pVer || test $(sed 's/\.//g' <<< $pVer) -lt 380 ) \
&& quit "You MUST have a python version of at least 3.8.0 !"
log "\tFound PYTHON - v$pVer ..."
}
Expand Down
8 changes: 4 additions & 4 deletions userge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from git import Repo
from pyrogram import Filters

from userge.logbot import LogBot
from userge import logbot
from userge import logging
from . import versions

_REPO = Repo()
_LOG = logging.getLogger(__name__)
LogBot.reply_last_msg("Setting Configs ...")
logbot.reply_last_msg("Setting Configs ...")


class Config:
Expand Down Expand Up @@ -78,14 +78,14 @@ class Config:


if Config.HEROKU_API_KEY:
LogBot.reply_last_msg("Checking Heroku App...", _LOG.info)
logbot.reply_last_msg("Checking Heroku App...", _LOG.info)
for heroku_app in heroku3.from_key(Config.HEROKU_API_KEY).apps():
if (heroku_app and Config.HEROKU_APP_NAME
and heroku_app.name == Config.HEROKU_APP_NAME):
_LOG.info("Heroku App : %s Found...", heroku_app.name)
Config.HEROKU_APP = heroku_app
break
LogBot.del_last_msg()
logbot.del_last_msg()


def get_version() -> str:
Expand Down
8 changes: 4 additions & 4 deletions userge/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import psutil

from userge import logging, Config
from userge.logbot import LogBot
from userge import logbot
from userge.utils import time_formatter
from userge.utils.exceptions import UsergeBotNotFound
from userge.plugins import get_all_plugins
Expand Down Expand Up @@ -81,7 +81,7 @@ async def load_plugin(self, name: str, reload_plugin: bool = False) -> None:
async def _load_plugins(self) -> None:
_IMPORTED.clear()
_INIT_TASKS.clear()
LogBot.edit_last_msg("Importing All Plugins", _LOG.info, _LOG_STR)
logbot.edit_last_msg("Importing All Plugins", _LOG.info, _LOG_STR)
for name in get_all_plugins():
try:
await self.load_plugin(name)
Expand Down Expand Up @@ -191,8 +191,8 @@ def begin(self, coro: Optional[Awaitable[Any]] = None) -> None:
run(coro)
else:
_LOG.info(_LOG_STR, "Idling Userge")
LogBot.edit_last_msg("Userge has Started Successfully !")
LogBot.end()
logbot.edit_last_msg("Userge has Started Successfully !")
logbot.end()
run(Userge.idle())
_LOG.info(_LOG_STR, "Exiting Userge")
for task in running_tasks:
Expand Down
6 changes: 3 additions & 3 deletions userge/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
from motor.core import AgnosticClient, AgnosticDatabase, AgnosticCollection

from userge import logging, Config
from userge.logbot import LogBot
from userge import logbot

_LOG = logging.getLogger(__name__)
_LOG_STR = "$$$>>> %s <<<$$$"

LogBot.edit_last_msg("Connecting to Database ...", _LOG.info, _LOG_STR)
logbot.edit_last_msg("Connecting to Database ...", _LOG.info, _LOG_STR)

_MGCLIENT: AgnosticClient = AsyncIOMotorClient(Config.DB_URI)
_RUN = asyncio.get_event_loop().run_until_complete
Expand All @@ -45,4 +45,4 @@ def get_collection(name: str) -> AgnosticCollection:
return _DATABASE[name]


LogBot.del_last_msg()
logbot.del_last_msg()
127 changes: 68 additions & 59 deletions userge/core/methods/decorators/on_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@

__all__ = ['OnCmd']

import re
from typing import Dict, List, Union

from pyrogram import Filters

from userge import Config
from ... import types
from . import RawDecorator
Expand All @@ -24,18 +21,26 @@ class OnCmd(RawDecorator): # pylint: disable=missing-class-docstring
def on_cmd(self,
command: str,
about: Union[str, Dict[str, Union[str, List[str], Dict[str, str]]]],
*,
group: int = 0,
name: str = '',
trigger: str = Config.CMD_TRIGGER,
filter_me: bool = True,
only_admins: bool = False,
allow_private: bool = True,
allow_bots: bool = True,
allow_groups: bool = True,
allow_channels: bool = True,
only_admins: bool = False,
allow_via_bot: bool = True,
check_client: bool = False,
check_downpath: bool = False,
check_change_info_perm: bool = False,
check_edit_perm: bool = False,
check_delete_perm: bool = False,
check_restrict_perm: bool = False,
check_promote_perm: bool = False,
check_invite_perm: bool = False,
check_pin_perm: bool = False,
**kwargs: Union[str, bool]
) -> RawDecorator._PYRORETTYPE:
"""\nDecorator for handling messages.
Expand Down Expand Up @@ -71,31 +76,59 @@ def on_cmd(self,
trigger to start command.
filter_me (``bool``, *optional*):
If ``False``, anyone can access, defaults to True.
only_admins (``bool``, *optional*):
If ``True``, client should be an admin, defaults to False.
If ``False``, anyone can access, defaults to True.
allow_private (``bool``, *optional*):
If ``False``, prohibit private chats, defaults to True.
If ``False``, prohibit private chats, defaults to True.
allow_bots (``bool``, *optional*):
If ``False``, prohibit bot chats, defaults to True.
If ``False``, prohibit bot chats, defaults to True.
allow_groups (``bool``, *optional*):
If ``False``, prohibit group chats, defaults to True.
If ``False``, prohibit group chats, defaults to True.
allow_channels (``bool``, *optional*):
If ``False``, prohibit channel chats, defaults to True.
If ``False``, prohibit channel chats, defaults to True.
only_admins (``bool``, *optional*):
If ``True``, client should be an admin, defaults to False.
allow_via_bot (``bool``, *optional*):
If ``True``, allow this via your bot, defaults to True.
If ``True``, allow this via your bot, defaults to True.
check_client (``bool``, *optional*):
If ``True``, check client is bot or not before execute, defaults to False.
If ``True``, check client is bot or not before execute, defaults to False.
check_downpath (``bool``, *optional*):
If ``True``, check downpath and make if not exist, defaults to False.
If ``True``, check downpath and make if not exist, defaults to False.
check_change_info_perm (``bool``, *optional*):
If ``True``, check user has change_info permission before execute,
defaults to False.
check_edit_perm (``bool``, *optional*):
If ``True``, check user has edit permission before execute,
defaults to False.
check_delete_perm (``bool``, *optional*):
If ``True``, check user has delete permission before execute,
defaults to False.
check_restrict_perm (``bool``, *optional*):
If ``True``, check user has restrict permission before execute,
defaults to False.
check_promote_perm (``bool``, *optional*):
If ``True``, check user has promote permission before execute,
defaults to False.
check_invite_perm (``bool``, *optional*):
If ``True``, check user has invite permission before execute,
defaults to False.
check_pin_perm (``bool``, *optional*):
If ``True``, check user has pin permission before execute,
defaults to False.
kwargs:
prefix (``str``, *optional*):
Expand All @@ -105,47 +138,23 @@ def on_cmd(self,
If ``True``, flags returns without prefix,
defaults to False.
"""
pattern = f"^(?:\\{trigger}|\\{Config.SUDO_TRIGGER}){command.lstrip('^')}" if trigger \
else f"^{command.lstrip('^')}"
if [i for i in '^()[]+*.\\|?:$' if i in command]:
match = re.match("(\\w[\\w_]*)", command)
cname = match.groups()[0] if match else ''
cname = name or cname
cname = trigger + cname if cname else ''
else:
cname = trigger + command
cname = name or cname
pattern += r"(?:\s([\S\s]+))?$"
cmd = types.raw.Command(self, cname, about, group, allow_via_bot)
scope: List[str] = []
if only_admins:
scope.append('admin')
if allow_private:
scope.append('private')
if allow_bots:
scope.append('bot')
if allow_groups:
scope += ['group', 'supergroup']
if allow_channels:
scope.append('channel')
filters_ = Filters.create(lambda _, __: cmd.is_enabled) & Filters.regex(pattern=pattern)
if filter_me:
outgoing_flt = Filters.create(
lambda _, m:
not (m.from_user and m.from_user.is_bot)
and (m.outgoing or (m.from_user and m.from_user.is_self))
and not (m.chat and m.chat.type == "channel" and m.edit_date)
and (m.text.startswith(trigger) if trigger else True))
incoming_flt = Filters.create(
lambda _, m:
not m.outgoing
and (
(Config.OWNER_ID
and (m.from_user and m.from_user.id == Config.OWNER_ID))
or ((cname.lstrip(trigger) in Config.ALLOWED_COMMANDS)
and (m.from_user and m.from_user.id in Config.SUDO_USERS)))
and (m.text.startswith(Config.SUDO_TRIGGER) if trigger else True))
filters_ = filters_ & (outgoing_flt | incoming_flt)
return self._build_decorator(log=f"On {pattern}", filters=filters_, flt=cmd,
check_client=allow_via_bot and check_client,
check_downpath=check_downpath, scope=scope, **kwargs)
return self._build_decorator(
types.raw.Command.parse(command, about,
trigger, name, filter_me,
client=self,
group=group,
allow_private=allow_private,
allow_bots=allow_bots,
allow_groups=allow_groups,
allow_channels=allow_channels,
only_admins=only_admins,
allow_via_bot=allow_via_bot,
check_client=check_client,
check_downpath=check_downpath,
check_change_info_perm=check_change_info_perm,
check_edit_perm=check_edit_perm,
check_delete_perm=check_delete_perm,
check_restrict_perm=check_restrict_perm,
check_promote_perm=check_promote_perm,
check_invite_perm=check_invite_perm,
check_pin_perm=check_pin_perm), **kwargs)
93 changes: 81 additions & 12 deletions userge/core/methods/decorators/on_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@

__all__ = ['OnFilters']

from pyrogram import Filters
from pyrogram.client.filters.filter import Filter as RawFilter

from ... import types
from . import RawDecorator


class OnFilters(RawDecorator): # pylint: disable=missing-class-docstring
def on_filters(self,
filters: Filters,
def on_filters(self, # pylint: disable=arguments-differ
filters: RawFilter,
group: int = 0,
allow_private: bool = True,
allow_bots: bool = True,
allow_groups: bool = True,
allow_channels: bool = True,
only_admins: bool = False,
allow_via_bot: bool = True,
check_client: bool = True,
check_downpath: bool = False) -> RawDecorator._PYRORETTYPE:
check_downpath: bool = False,
check_change_info_perm: bool = False,
check_edit_perm: bool = False,
check_delete_perm: bool = False,
check_restrict_perm: bool = False,
check_promote_perm: bool = False,
check_invite_perm: bool = False,
check_pin_perm: bool = False,) -> RawDecorator._PYRORETTYPE:
"""\nDecorator for handling filters
Parameters:
Expand All @@ -33,17 +45,74 @@ def on_filters(self,
group (``int``, *optional*):
The group identifier, defaults to 0.
allow_private (``bool``, *optional*):
If ``False``, prohibit private chats, defaults to True.
allow_bots (``bool``, *optional*):
If ``False``, prohibit bot chats, defaults to True.
allow_groups (``bool``, *optional*):
If ``False``, prohibit group chats, defaults to True.
allow_channels (``bool``, *optional*):
If ``False``, prohibit channel chats, defaults to True.
only_admins (``bool``, *optional*):
If ``True``, client should be an admin, defaults to False.
allow_via_bot (``bool``, *optional*):
If ``True``, allow this via your bot, defaults to True.
If ``True``, allow this via your bot, defaults to True.
check_client (``bool``, *optional*):
If ``True``, check client is bot or not before execute, defaults to True.
If ``True``, check client is bot or not before execute, defaults to True.
check_downpath (``bool``, *optional*):
If ``True``, check downpath and make if not exist, defaults to False.
If ``True``, check downpath and make if not exist, defaults to False.
check_change_info_perm (``bool``, *optional*):
If ``True``, check user has change_info permission before execute,
defaults to False.
check_edit_perm (``bool``, *optional*):
If ``True``, check user has edit permission before execute,
defaults to False.
check_delete_perm (``bool``, *optional*):
If ``True``, check user has delete permission before execute,
defaults to False.
check_restrict_perm (``bool``, *optional*):
If ``True``, check user has restrict permission before execute,
defaults to False.
check_promote_perm (``bool``, *optional*):
If ``True``, check user has promote permission before execute,
defaults to False.
check_invite_perm (``bool``, *optional*):
If ``True``, check user has invite permission before execute,
defaults to False.
check_pin_perm (``bool``, *optional*):
If ``True``, check user has pin permission before execute,
defaults to False.
"""
flt = types.raw.Filter(self, group, allow_via_bot)
filters = Filters.create(lambda _, __: flt.is_enabled) & filters
return self._build_decorator(log=f"On Filters {filters}", filters=filters,
flt=flt, check_client=allow_via_bot and check_client,
check_downpath=check_downpath)
return self._build_decorator(
types.raw.Filter.parse(client=self,
filters=filters,
group=group,
allow_private=allow_private,
allow_bots=allow_bots,
allow_groups=allow_groups,
allow_channels=allow_channels,
only_admins=only_admins,
allow_via_bot=allow_via_bot,
check_client=check_client,
check_downpath=check_downpath,
check_change_info_perm=check_change_info_perm,
check_edit_perm=check_edit_perm,
check_delete_perm=check_delete_perm,
check_restrict_perm=check_restrict_perm,
check_promote_perm=check_promote_perm,
check_invite_perm=check_invite_perm,
check_pin_perm=check_pin_perm))
Loading

0 comments on commit 0b2cd3f

Please sign in to comment.