Spaces:
Runtime error
Runtime error
File size: 7,250 Bytes
78b07ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import contextlib
import os
import heroku3
from git import Repo
from git.exc import GitCommandError, InvalidGitRepositoryError, NoSuchPathError
from pyrogram.types import Message
from Hellbot import HEROKU_APP
from Hellbot.core import LOGS
from Hellbot.functions.tools import restart, gen_changelogs, initialize_git
from . import Config, HelpMenu, hellbot, on_message
@on_message("restart", allow_stan=True)
async def restart_bot(_, message: Message):
await hellbot.edit(message, "π« Restarted Bot Successfully!")
try:
if HEROKU_APP:
try:
heroku = heroku3.from_key(Config.HEROKU_APIKEY)
app = heroku.apps()[Config.HEROKU_APPNAME]
app.restart()
except:
await restart()
else:
await restart()
except Exception as e:
LOGS.error(e)
@on_message("shutdown", allow_stan=True)
async def shutdown_bot(_, message: Message):
await hellbot.edit(
message,
"**[ β οΈ ]** __HellBot is now offline! Manually start again to get it back online.__",
)
try:
if HEROKU_APP:
try:
heroku = heroku3.from_key(Config.HEROKU_APIKEY)
app = heroku.apps()[Config.HEROKU_APPNAME]
app.process_formation()["worker"].scale(0)
except:
await restart(shutdown=True)
else:
await restart(shutdown=True)
except Exception as e:
LOGS.error(e)
@on_message("cleanup", allow_stan=True)
async def clenup_bot(_, message: Message):
await hellbot.edit(message, "**β»οΈ HellBot Cleanup Completed!**")
await restart(clean_up=True)
@on_message("update", allow_stan=True)
async def update_bot(_, message: Message):
hell = await hellbot.edit(message, "**π π¨π π―πππππΎππ...**")
if len(message.command) < 2:
status, repo, force = await initialize_git(Config.PLUGINS_REPO)
if not status:
return await hellbot.error(hell, repo)
active_branch = repo.active_branch.name
upstream = repo.remote("upstream")
upstream.fetch(active_branch)
changelogs = await gen_changelogs(repo, f"HEAD..upstream/{active_branch}")
if not changelogs and not force:
repo.__del__()
return await hellbot.delete(
hell, "__There are no updates available right now.__"
)
if force:
return await hell.edit(
f"Force-Sync in progress... Please wait for a moment and try again.\n\n{changelogs}",
disable_web_page_preview=True,
)
return await hell.edit(
f"**π π΄ππ½πΊππΎ π ππΊππ
πΊπ»π
πΎ πΏππ π―π
πππππ:**\n\n{changelogs}",
disable_web_page_preview=True,
)
cmd = message.command[1].lower()
if cmd == "plugins":
if HEROKU_APP:
try:
heroku = heroku3.from_key(Config.HEROKU_APIKEY)
app = heroku.apps()[Config.HEROKU_APPNAME]
await hell.edit(
"**π π΄ππ½πΊππΎπ½ π―π
πππππ ππΎππ!** \n__π‘ππ πππ
π
πππΊππ πππππππ πΊπΏππΎπ 1 ππππππΎ.__"
)
app.restart()
except Exception as e:
return await hellbot.error(message, f"`{e}`")
else:
await hell.edit(
"**π π΄ππ½πΊππΎπ½ π―π
πππππ ππΎππ!** \n__π‘ππ πππ
π
πππΊππ πππππππ πΊπΏππΎπ 1 ππππππΎ.__"
)
return await restart(update=True)
elif cmd == "deploy":
if HEROKU_APP:
os.chdir("/app")
status, repo, _ = await initialize_git(Config.DEPLOY_REPO)
if not status:
return await hellbot.error(hell, repo)
active_branch = repo.active_branch.name
upstream = repo.remote("upstream")
upstream.fetch(active_branch)
heroku = heroku3.from_key(Config.HEROKU_APIKEY)
app = heroku.apps()[Config.HEROKU_APPNAME]
await hell.edit(
"**π π£πΎππ
πππππ π¨π π―πππππΎππ...**\nThis might take upto 5 minutes to complete!"
)
repo.git.reset("--hard", "FETCH_HEAD")
heroku_git = app.git_url.replace(
"https://", f"https://api:{Config.HEROKU_APIKEY}@"
)
if "heroku" in repo.remotes:
remote = repo.remote("heroku")
remote.set_url(heroku_git)
else:
remote = repo.create_remote("heroku", heroku_git)
try:
remote.push(f"HEAD:refs/heads/master", force=True)
except BaseException as e:
repo.__del__()
return await hellbot.error(hell, f"__Invalid Heroku Creds:__ `{e}`")
build = app.builds(order_by="created_at", sort="desc")[0]
if build.status == "failed":
return await hellbot.error(
hell,
"__There were some problems with the update! Make sure your heroku api and app name are correct.__",
)
try:
remote.push("master:main", force=True)
except BaseException as e:
repo.__del__()
return await hellbot.error(hell, f"__Invalid Heroku Creds:__ `{e}`")
else:
await hell.edit(
"**π π£πΎππ
πππππ π¨π π―πππππΎππ...**\n\n__Please wait for a minute or two.__"
)
return await restart(update=True)
else:
return await hellbot.delete(
hell, f"**[ β οΈ ]** __Invalid update argument:__ `{cmd}`"
)
HelpMenu("power").add(
"restart", None, "Restart the bot. It might take 2 minutes to execute.", "restart"
).add(
"shutdown",
None,
"Shutdown the bot. Will permanently turn off the bot until started manually.",
"shutdown",
).add(
"cleanup",
None,
"Cleanup the bot. Delete downloaded files and temp files.",
"cleanup",
).add(
"update",
None,
"Check if there's any update available for the bot. If there is, it'll give new last 5 changelogs.",
"update",
).add(
"update plugins",
None,
"Updates the plugins to the latest code if there is any update available, else it'll just restart the bot.",
"update plugins",
).add(
"update deploy",
None,
"Updates the main code base to the latest commit.",
"update deploy",
"This process might take upto 5 minutes to complete.",
).info(
"Commands to manage the bot."
).done()
|