dump-mirrorbot/bot/helper/__init__.py

71 lines
2.4 KiB
Python

import heroku3
from functools import wraps
from pyrogram.types import Message
from bot import HEROKU_API_KEY, HEROKU_APP_NAME
# Implement by https://github.com/jusidama18
# Setting Message
def get_text(message: Message) -> [None, str]:
"""Extract Text From Commands"""
text_to_return = message.text
if message.text is None:
return None
if " " in text_to_return:
try:
return message.text.split(None, 1)[1]
except IndexError:
return None
else:
return None
# Preparing For Setting Config
# Implement by https://github.com/jusidama18 and Based on this https://github.com/DevsExpo/FridayUserbot/blob/master/plugins/heroku_helpers.py
heroku_client = None
if HEROKU_API_KEY:
heroku_client = heroku3.from_key(HEROKU_API_KEY)
def check_heroku(func):
@wraps(func)
async def heroku_cli(client, message):
heroku_app = None
if not heroku_client:
await message.reply_text("`Please Add HEROKU_API_KEY Key For This To Function To Work!`", parse_mode="markdown")
elif not HEROKU_APP_NAME:
await message.reply_text("`Please Add HEROKU_APP_NAME For This To Function To Work!`", parse_mode="markdown")
if HEROKU_APP_NAME and heroku_client:
try:
heroku_app = heroku_client.app(HEROKU_APP_NAME)
except:
await message.reply_text(message, "`Heroku Api Key And App Name Doesn't Match!`", parse_mode="markdown")
if heroku_app:
await func(client, message, heroku_app)
return heroku_cli
# Preparing For Update Bot
# Implement by https://github.com/jusidama18 and Based on this https://github.com/DevsExpo/FridayUserbot/blob/master/plugins/updater.py
def fetch_heroku_git_url(api_key, app_name):
if not api_key:
return None
if not app_name:
return None
heroku = heroku3.from_key(api_key)
try:
heroku_applications = heroku.apps()
except:
return None
heroku_app = None
for app in heroku_applications:
if app.name == app_name:
heroku_app = app
break
if not heroku_app:
return None
return heroku_app.git_url.replace("https://", "https://api:" + api_key + "@")
HEROKU_URL = fetch_heroku_git_url(HEROKU_API_KEY, HEROKU_APP_NAME)