Skip to content

Commit

Permalink
Custom Alive (UsergeTeam#152)
Browse files Browse the repository at this point in the history
* Customizable Alive

* Update config.py

* hmmm

* Update alive.py

* Update alive.py

* Update alive.py

* sed lyf

* 🤔🤔added loggers

* ooof

* remove loggers

* Update config.env.sample

* fix pep

* missing reply_markup

Co-authored-by: rking32 <[email protected]>
  • Loading branch information
Phyco-Ninja and rking32 authored Sep 15, 2020
1 parent 0a3ebab commit c7339cc
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 21 deletions.
7 changes: 7 additions & 0 deletions config.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ UNFINISHED_PROGRESS_STR='░'
# custom name for your sticker pack
CUSTOM_PACK_NAME=""

# set your own custom media for .alive or
# disable it by putting value 'nothing'
# accepted formats:
# a link to message: https://t.me/theuserge/8
# chat and message id separated by |: -1005545442|84565
ALIVE_MEDIA=""


# ----------- Only If Using Heroku ----------- #

Expand Down
1 change: 1 addition & 0 deletions userge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Config:
SUDO_TRIGGER = os.environ.get("SUDO_TRIGGER")
FINISHED_PROGRESS_STR = os.environ.get("FINISHED_PROGRESS_STR")
UNFINISHED_PROGRESS_STR = os.environ.get("UNFINISHED_PROGRESS_STR")
ALIVE_MEDIA = os.environ.get("ALIVE_MEDIA", None)
CUSTOM_PACK_NAME = os.environ.get("CUSTOM_PACK_NAME")
UPSTREAM_REPO = os.environ.get("UPSTREAM_REPO")
UPSTREAM_REMOTE = os.environ.get("UPSTREAM_REMOTE")
Expand Down
106 changes: 85 additions & 21 deletions userge/plugins/tools/alive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,38 @@
#
# All rights reserved.

import re
from typing import Optional

from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from pyrogram.errors import ChatSendMediaForbidden
from pyrogram.errors.exceptions import FileIdInvalid, FileReferenceEmpty
from pyrogram.errors.exceptions.bad_request_400 import BadRequest, ChannelInvalid, MediaEmpty

from userge.core.ext import RawClient
from userge import userge, Message, Config, versions, get_version
from userge import userge, Message, Config, versions, get_version, logging
from userge.utils import get_file_id_and_ref

LOGO_ID, LOGO_REF = None, None
_LOG = logging.getLogger(__name__)
_IS_STICKER = False
_DEFAULT = "https://t.me/theUserge/31"
_CHAT, _MSG_ID = None, None
_LOGO_ID, _LOGO_REF = None, None


@userge.on_cmd("alive", about={
'header': "This command is just for fun"}, allow_channels=False)
async def alive(message: Message):
await message.delete()

if not (_CHAT and _MSG_ID):
try:
_set_data()
except Exception as set_err:
_LOG.exception("There was some problem while setting Media Data. "
f"trying again... ERROR:: {set_err} ::")
_set_data(True)
markup = None
copy_ = "https://github.com/UsergeTeam/Userge/blob/master/LICENSE"
output = f"""
**⏱ uptime** : `{userge.uptime}`
**💡 version** : `{get_version()}`
Expand All @@ -34,15 +52,27 @@ async def alive(message: Message):
• **unofficial**: `{_parse_arg(Config.LOAD_UNOFFICIAL_PLUGINS)}`
**__Python__**: `{versions.__python_version__}`
**__Pyrogram__**: `{versions.__pyro_version__}`
**{versions.__license__}** | **{versions.__copyright__}** | **[Repo]({Config.UPSTREAM_REPO})**
**__Pyrogram__**: `{versions.__pyro_version__}`"""
if not message.client.is_bot:
output += f"""\n
🎖 **{versions.__license__}** | 👥 **{versions.__copyright__}** | 🧪 **[Repo]({Config.UPSTREAM_REPO})**
"""
else:
markup = InlineKeyboardMarkup([
[
InlineKeyboardButton(text="👥 UsergeTeam", url="https://github.com/UsergeTeam"),
InlineKeyboardButton(text="🧪 Repo", url=Config.UPSTREAM_REPO)
],
[InlineKeyboardButton(text="🎖 GNU GPL v3.0", url=copy_)]
])
if _MSG_ID == "text_format":
return await message.edit(output, reply_markup=markup, disable_web_page_preview=True)
await message.delete()
try:
await _send_alive(message, output)
await _send_alive(message, output, markup)
except (FileIdInvalid, FileReferenceEmpty, BadRequest):
await _refresh_id()
await _send_alive(message, output)
await _send_alive(message, output, markup)


def _get_mode() -> str:
Expand All @@ -57,27 +87,61 @@ def _parse_arg(arg: bool) -> str:
return "enabled" if arg else "disabled"


async def _send_alive(message: Message, text: str) -> None:
if not (LOGO_ID and LOGO_REF):
async def _send_alive(message: Message,
text: str,
reply_markup: Optional[InlineKeyboardMarkup]) -> None:
if not (_LOGO_ID and _LOGO_REF):
await _refresh_id()
try:
await message.client.send_animation(chat_id=message.chat.id,
animation=LOGO_ID,
file_ref=LOGO_REF,
caption=text)
await message.client.send_cached_media(chat_id=message.chat.id,
file_id=_LOGO_ID,
file_ref=_LOGO_REF,
caption=text,
reply_markup=reply_markup)
if _IS_STICKER:
raise MediaEmpty
except (MediaEmpty, ChatSendMediaForbidden):
await message.client.send_message(chat_id=message.chat.id,
text=text,
reply_markup=reply_markup,
disable_web_page_preview=True)


async def _refresh_id():
global LOGO_ID, LOGO_REF # pylint: disable=global-statement
async def _refresh_id() -> None:
global _LOGO_ID, _LOGO_REF, _IS_STICKER # pylint: disable=global-statement
try:
gif = (await userge.get_messages('theUserge', 31)).animation
media = await userge.get_messages(_CHAT, _MSG_ID)
except ChannelInvalid:
LOGO_ID = None
LOGO_REF = None
_set_data(True)
return await _refresh_id()
else:
if media.sticker:
_IS_STICKER = True
_LOGO_ID, _LOGO_REF = get_file_id_and_ref(media)


def _set_data(errored: bool = False) -> None:
global _CHAT, _MSG_ID, _DEFAULT # pylint: disable=global-statement

pattern = r"^(http(?:s?):\/\/)?(www\.)?(t.me)(\/c\/(\d+)|:?\/(\w+))?\/(\d+)$"
if Config.ALIVE_MEDIA and not errored:
if Config.ALIVE_MEDIA.lower().strip() == "nothing":
_CHAT = "text_format"
_MSG_ID = "text_format"
return
media_link = Config.ALIVE_MEDIA
match = re.search(pattern, media_link)
if match:
_MSG_ID = int(match.group(7))
if match.group(5):
_CHAT = int("-100" + match.group(5))
elif match.group(6):
_CHAT = match.group(6)
elif "|" in Config.ALIVE_MEDIA:
_CHAT, _MSG_ID = Config.ALIVE_MEDIA.split("|", maxsplit=1)
_CHAT = _CHAT.strip()
_MSG_ID = int(_MSG_ID.strip())
else:
LOGO_ID = gif.file_id
LOGO_REF = gif.file_ref
match = re.search(pattern, _DEFAULT)
_CHAT = match.group(6)
_MSG_ID = int(match.group(7))

0 comments on commit c7339cc

Please sign in to comment.