Skip to content

Commit

Permalink
fixes gban, gdrive, pymongo escape error, welcome(remove media limit)…
Browse files Browse the repository at this point in the history
…, add media support for filters and notes
  • Loading branch information
rking32 committed Jun 29, 2020
1 parent 73b36fb commit 8107485
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 345 deletions.
8 changes: 5 additions & 3 deletions userge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Config:
WORKERS = int(os.environ.get("WORKERS", 4))
ANTISPAM_SENTRY = bool(os.environ.get("ANTISPAM_SENTRY", False))
HU_STRING_SESSION = os.environ.get("HU_STRING_SESSION", None)
DB_URI = os.environ.get("DATABASE_URL", None)
DB_URI = os.environ.get("DATABASE_URL", '')
LANG = os.environ.get("PREFERRED_LANGUAGE", "en")
DOWN_PATH = os.environ.get("DOWN_PATH", "downloads").rstrip('/') + '/'
SCREENSHOT_API = os.environ.get("SCREENSHOT_API", None)
Expand All @@ -66,7 +66,7 @@ class Config:
GOOGLE_CHROME_DRIVER = os.environ.get("GOOGLE_CHROME_DRIVER", None)
GOOGLE_CHROME_BIN = os.environ.get("GOOGLE_CHROME_BIN", None)
LOG_CHANNEL_ID = int(os.environ.get("LOG_CHANNEL_ID", 0))
UPSTREAM_REPO = os.environ.get("UPSTREAM_REPO", "https://github.com/UsergeTeam/Userge")
UPSTREAM_REPO = os.environ.get("UPSTREAM_REPO", "https://github.com/UsergeTeam/Userge.git")
HEROKU_API_KEY = os.environ.get("HEROKU_API_KEY", None)
HEROKU_APP_NAME = os.environ.get("HEROKU_APP_NAME", None)
LOAD_UNOFFICIAL_PLUGINS = bool(os.environ.get("LOAD_UNOFFICIAL_PLUGINS", False))
Expand Down Expand Up @@ -160,5 +160,7 @@ def get_version() -> str:
ver = f"{versions.__major__}.{versions.__minor__}.{versions.__micro__}"
diff = list(_REPO.iter_commits(f'{Config.UPSTREAM_REMOTE}/master..HEAD'))
if diff:
return f"{ver}-beta.{len(diff)}"
if "/usergeteam/userge" in Config.UPSTREAM_REPO.lower():
return f"{ver}-beta.{len(diff)}"
return f"{ver}-custom.{len(diff)}"
return ver
4 changes: 4 additions & 0 deletions userge/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

__all__ = ['get_collection']

import re
import asyncio
from typing import List

Expand All @@ -21,6 +22,9 @@

_LOG.info(_LOG_STR, "Connecting to Database...")

_UN_AND_PWD = Config.DB_URI.split('//')[1].split('@')[0]
Config.DB_URI.replace(_UN_AND_PWD, re.escape(_UN_AND_PWD))

_MGCLIENT: AgnosticClient = AsyncIOMotorClient(Config.DB_URI)
_RUN = asyncio.get_event_loop().run_until_complete

Expand Down
20 changes: 5 additions & 15 deletions userge/core/methods/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,33 +150,23 @@ def on_filters(self,
group: int = 0) -> Callable[[_PYROFUNC], _PYROFUNC]:
""" Decorator for handling filters """
flt = Filtr(self, group)
filters = filters & Filters.create(lambda _, __: flt.is_enabled)
filters = Filters.create(lambda _, __: flt.is_enabled) & filters
return self._build_decorator(log=f"On Filters {filters}",
filters=filters, flt=flt)

def on_new_member(self,
welcome_chats: Filters.chat,
group: int = -2) -> Callable[[_PYROFUNC], _PYROFUNC]:
""" Decorator for handling new members """
flt = Filtr(self, group)
welcome_chats = welcome_chats & Filters.create(lambda _, __: flt.is_enabled)
return self._build_decorator(log=f"On New Member in {welcome_chats}",
filters=(
Filters.group & Filters.new_chat_members
& welcome_chats),
flt=flt)
return self.on_filters(
filters=Filters.group & Filters.new_chat_members & welcome_chats, group=group)

def on_left_member(self,
leaving_chats: Filters.chat,
group: int = -2) -> Callable[[_PYROFUNC], _PYROFUNC]:
""" Decorator for handling left members """
flt = Filtr(self, group)
leaving_chats = leaving_chats & Filters.create(lambda _, __: flt.is_enabled)
return self._build_decorator(log=f"On Left Member in {leaving_chats}",
filters=(
Filters.group & Filters.left_chat_member
& leaving_chats),
flt=flt)
return self.on_filters(
filters=Filters.group & Filters.left_chat_member & leaving_chats, group=group)

def _build_decorator(self,
log: str,
Expand Down
135 changes: 129 additions & 6 deletions userge/core/methods/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
__all__ = ['CLogger']

import asyncio
from typing import Optional, Tuple

from pyrogram import Message as RawMessage
from userge import logging, Config
from userge.utils import SafeDict
from . import message
from .. import client as _client

Expand All @@ -29,6 +31,40 @@ def __init__(self, client: '_client.Userge', name: str) -> None:
def _gen_string(name: str) -> str:
return "**logger** : #" + name.split('.')[-1].upper() + "\n\n{}"

@staticmethod
def _get_file_id_and_ref(message_: 'message.Message') -> Tuple[str, str]:
if message_.audio:
file_ = message_.audio
elif message_.animation:
file_ = message_.animation
elif message_.photo:
file_ = message_.photo
elif message_.sticker:
file_ = message_.sticker
elif message_.voice:
file_ = message_.voice
elif message_.video_note:
file_ = message_.video_note
elif message_.video:
file_ = message_.video
else:
file_ = message_.document
return file_.file_id, file_.file_ref

@staticmethod
def get_link(message_id: int) -> str:
"""\nreturns link for a specific message.
Parameters:
message_id (`int`):
Message id of stored message.
Returns:
str
"""
return "<b><a href='https://t.me/c/{}/{}'>Preview</a></b>".format(
str(Config.LOG_CHANNEL_ID)[4:], message_id)

def update(self, name: str) -> None:
"""\nupdate current logger name.
Expand All @@ -41,20 +77,21 @@ def update(self, name: str) -> None:
"""
self._string = self._gen_string(name)

async def log(self, text: str) -> None:
async def log(self, text: str) -> Optional[int]:
"""\nsend text message to log channel.
Parameters:
text (``str``):
Text of the message to be sent.
Returns:
None
message_id on success or None
"""
_LOG.debug(_LOG_STR, f"logging text : {text} to channel : {Config.LOG_CHANNEL_ID}")
if Config.LOG_CHANNEL_ID:
await self._client.send_message(chat_id=Config.LOG_CHANNEL_ID,
text=self._string.format(text))
msg = await self._client.send_message(chat_id=Config.LOG_CHANNEL_ID,
text=self._string.format(text.strip()))
return msg.message_id

async def fwd_msg(self,
message_: 'message.Message',
Expand All @@ -63,7 +100,7 @@ async def fwd_msg(self,
"""\nforward message to log channel.
Parameters:
message (`pyrogram.Message`):
message_ (`pyrogram.Message`):
pass pyrogram.Message object which want to forward.
as_copy (`bool`, *optional*):
Expand Down Expand Up @@ -92,4 +129,90 @@ async def fwd_msg(self,
as_copy=as_copy,
remove_caption=remove_caption)
else:
await self.log(message_.text)
await self.log(message_.text.html)

async def store(self,
message_: Optional['message.Message'],
caption: Optional[str] = '') -> Optional[int]:
"""\nstore message to log channel.
Parameters:
message_ (`pyrogram.Message` | `None`):
pass pyrogram.Message object which want to forward.
caption (`str`, *optional*):
Text or Cpation of the message to be sent.
Returns:
message_id on success or None
"""
caption = caption or ''
if message_ and message_.caption:
caption = caption + message_.caption.html
if message_ and message_.media:
if caption:
caption = self._string.format(caption.strip())
file_id, file_ref = self._get_file_id_and_ref(message_)
msg = await self._client.send_cached_media(chat_id=Config.LOG_CHANNEL_ID,
file_id=file_id,
file_ref=file_ref,
caption=caption)
message_id = msg.message_id
else:
message_id = await self.log(caption)
return message_id

async def forward_stored(self,
message_id: int,
chat_id: int,
user_id: int,
reply_to_message_id: int,
del_in: int = 0) -> None:
"""\nforward stored message from log channel.
Parameters:
message_id (`int`):
Message id of stored message.
chat_id (`int`):
ID of chat (dest) you want to farward.
user_id (`int`):
ID of user you want to reply.
reply_to_message_id (`int`):
If the message is a reply, ID of the original message.
del_in (`int`):
Time in Seconds for delete that message.
Returns:
message_id on success or None
"""
message_ = await self._client.get_messages(chat_id=Config.LOG_CHANNEL_ID,
message_ids=message_id)
caption = ''
if message_.caption:
caption = message_.caption.html.split('\n\n', maxsplit=1)[-1]
elif message_.text:
caption = message_.text.html.split('\n\n', maxsplit=1)[-1]
if caption:
u_dict = await self._client.get_user_dict(user_id)
chat = await self._client.get_chat(chat_id)
u_dict.update(
{'chat': chat.title if chat.title else "this group", 'count': chat.members_count})
caption = caption.format_map(SafeDict(**u_dict))
if message_.media:
file_id, file_ref = self._get_file_id_and_ref(message_)
msg = await self._client.send_cached_media(chat_id=chat_id,
file_id=file_id,
file_ref=file_ref,
caption=caption,
reply_to_message_id=reply_to_message_id)
else:
msg = await self._client.send_message(chat_id=chat_id,
text=caption,
reply_to_message_id=reply_to_message_id)
if del_in and msg:
await asyncio.sleep(del_in)
await msg.delete()
6 changes: 3 additions & 3 deletions userge/core/methods/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import List, Dict, Union, Optional, Sequence

import aiofiles
from pyrogram import Client as RawClient, Message as RawMessage, InlineKeyboardMarkup
from pyrogram import Message as RawMessage, InlineKeyboardMarkup
from pyrogram.errors.exceptions import MessageAuthorRequired, MessageTooLong
from pyrogram.errors.exceptions.bad_request_400 import MessageNotModified, MessageIdInvalid

Expand Down Expand Up @@ -50,7 +50,7 @@ def __init__(self,
@property
def input_str(self) -> str:
""" Returns the input string without command """
input_ = self.text
input_ = self.text.html
if ' ' in input_:
return str(input_.split(maxsplit=1)[1].strip())
return ''
Expand All @@ -60,7 +60,7 @@ def input_or_reply_str(self) -> str:
""" Returns the input string or replied msg text without command """
input_ = self.input_str
if not input_ and self.reply_to_message:
input_ = (self.reply_to_message.text or '').strip()
input_ = (self.reply_to_message.text.html if self.reply_to_message.text else '').strip()
return input_

@property
Expand Down
4 changes: 4 additions & 0 deletions userge/plugins/admin/gban.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ async def gban_user(message: Message):
"**#Already_GBanned**\n\nUser Already Exists in My Gban List.\n"
f"**Reason For GBan:** `{found['reason']}`", del_in=5)
return
async for chat in userge.get_common_chats(user_id):
await chat.kick_member(user_id)
sent = await message.edit(
r"\\**#GBanned_User**//"
f"\n\n**First Name:** [{firstname}](tg://user?id={user_id})\n"
Expand Down Expand Up @@ -139,6 +141,8 @@ async def ungban_user(message: Message):
if not found:
await message.err("User Not Found in My Gban List")
return
async for chat in userge.get_common_chats(user_id):
await chat.unban_member(user_id)
await asyncio.gather(
GBAN_USER_BASE.delete_one({'firstname': firstname, 'user_id': user_id}),
message.edit(
Expand Down
2 changes: 1 addition & 1 deletion userge/plugins/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def helpme(message: Message) -> None:
out_str = f"""⚒ <b><u>(<code>{len(plugins)}</code>) Plugins Available</u></b>\n\n"""
cat_plugins = userge.manager.get_plugins()
for cat in sorted(cat_plugins):
out_str += (f" {_CATEGORY[cat]} <b>{cat}</b> "
out_str += (f" {_CATEGORY.get(cat, '📁')} <b>{cat}</b> "
f"(<code>{len(cat_plugins[cat])}</code>) : <code>"
+ "</code> <code>".join(sorted(cat_plugins[cat])) + "</code>\n\n")
out_str += f"""📕 <b>Usage:</b> <code>{Config.CMD_TRIGGER}help [plugin_name]</code>"""
Expand Down
Loading

0 comments on commit 8107485

Please sign in to comment.