Spaces:
Runtime error
Runtime error
File size: 9,453 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
import math
import os
import heroku3
import psutil
import requests
import urllib3
from pyrogram.types import Message
from Hellbot import HEROKU_APP
from Hellbot.core.config import all_env, os_configs
from Hellbot.functions.paste import spaceBin
from Hellbot.functions.templates import usage_templates
from Hellbot.functions.tools import restart, update_dotenv
from . import Config, HelpMenu, Symbols, db, hellbot, on_message
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
Heroku = heroku3.from_key(Config.HEROKU_APIKEY)
@on_message("getvar", allow_stan=True)
async def getvar(_, message: Message):
if len(message.command) < 2:
return await hellbot.delete(message, "Give a varname to fetch value.")
varname = message.command[1]
if varname.upper() in os_configs:
value = os.environ.get(varname.upper(), None)
else:
value = await db.get_env(varname.upper())
if isinstance(value, str):
await hellbot.edit(
message,
f"{Symbols.anchor} **๐ต๐บ๐๐๐บ๐ป๐
๐พ ๐ญ๐บ๐๐พ:** `{varname.upper()}`\n{Symbols.anchor} **๐ต๐บ๐
๐๐พ:** `{value}`",
)
elif value is None:
await hellbot.delete(message, f"**๐ต๐บ๐๐๐บ๐ป๐
๐พ {varname} ๐ฝ๐๐พ๐ ๐๐๐ ๐พ๐๐๐๐๐!**")
@on_message(["getallvar", "getallvars"], allow_stan=True)
async def getallvar(_, message: Message):
text = "**๐ ๐ซ๐๐๐ ๐๐ฟ ๐บ๐
๐
๐๐บ๐๐๐บ๐ป๐
๐พ ๐บ๐๐พ:**\n\n"
for env in all_env:
text += f" {Symbols.anchor} `{env}`\n"
for config in os_configs:
text += f" {Symbols.anchor} `{config}`\n"
await hellbot.edit(message, text)
@on_message("setvar", allow_stan=True)
async def setvar(_, message: Message):
if len(message.command) < 3:
return await hellbot.delete(
message, "**๐ฆ๐๐๐พ ๐๐บ๐๐๐บ๐๐พ ๐บ๐๐ฝ ๐๐บ๐-๐๐บ๐
๐๐พ ๐บ๐
๐๐๐ ๐๐๐๐ ๐๐๐พ ๐ผ๐๐๐๐บ๐๐ฝ!**"
)
is_heroku = False
input_str = (await hellbot.input(message)).split(" ", 1)
varname = input_str[0]
varvalue = input_str[1]
if varname.upper() in os_configs:
if HEROKU_APP:
try:
app = Heroku.app(Config.HEROKU_APPNAME)
config = app.config()
oldValue = (
config[varname.upper()] if varname.upper() in config else "None"
)
is_heroku = True
except BaseException:
return await hellbot.error(
message, "`HEROKU_APIKEY` or `HEROKU_APPNAME` is Invalid!"
)
else:
oldValue = os.environ.get(varname.upper(), "None")
await update_dotenv(varname.upper(), varvalue)
await hellbot.edit(
message,
f"**{Symbols.anchor} ๐ต๐บ๐๐๐บ๐ป๐
๐พ:** `{varname.upper()}` \n\n"
f"**{Symbols.anchor} ๐ฎ๐
๐ฝ ๐ต๐บ๐
๐๐พ:** `{oldValue}` \n\n"
f"**{Symbols.anchor} ๐ญ๐พ๐ ๐ต๐บ๐
๐๐พ:** `{varvalue}`\n\n"
"__Restarting to apply changes!__",
)
if is_heroku:
config[varname.upper()] = varvalue
return await restart()
oldValue = await db.get_env(varname.upper())
await db.set_env(varname.upper(), varvalue)
await hellbot.delete(
message,
f"**{Symbols.anchor} ๐ต๐บ๐๐๐บ๐ป๐
๐พ:** `{varname.upper()}` \n\n"
f"**{Symbols.anchor} ๐ฎ๐
๐ฝ ๐ต๐บ๐
๐๐พ:** `{oldValue}` \n\n"
f"**{Symbols.anchor} ๐ญ๐พ๐ ๐ต๐บ๐
๐๐พ:** `{varvalue}`",
)
@on_message("delvar", allow_stan=True)
async def delvar(_, message: Message):
if len(message.command) < 2:
return await hellbot.delete(message, "**๐ฆ๐๐๐พ ๐๐บ๐๐๐บ๐๐พ ๐บ๐
๐๐๐ ๐๐๐๐ ๐๐๐พ ๐ผ๐๐๐๐บ๐๐ฝ!**")
varname = message.command[1]
if varname.upper() in os_configs:
return await hellbot.error(
message, "You can't delete this var for security reasons."
)
if await db.is_env(varname.upper()):
await db.rm_env(varname.upper())
await hellbot.delete(
message, f"**๐ต๐บ๐๐๐บ๐ป๐
๐พ** `{varname.upper()}` **๐ฝ๐พ๐
๐พ๐๐พ๐ฝ ๐๐๐ผ๐ผ๐พ๐๐๐ฟ๐๐
๐
๐!**"
)
return
await hellbot.delete(message, "**๐ญ๐ ๐๐๐ผ๐ ๐๐บ๐๐๐บ๐ป๐
๐พ ๐ฟ๐๐๐๐ฝ ๐๐ ๐ฝ๐บ๐๐บ๐ป๐บ๐๐พ ๐๐ ๐ฝ๐พ๐
๐พ๐๐พ!**")
@on_message("usage", allow_stan=True)
async def dynoUsage(_, message: Message):
hell = await hellbot.edit(message, "Fetching usage info...")
percentage = 0
hours = 0
minutes = 0
appHours = 0
appMinutes = 0
appPercentage = 0
if HEROKU_APP:
HEADERS = {
"User-Agent": "Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Mobile Safari/537.36",
"Authorization": f"Bearer {Config.HEROKU_APIKEY}",
"Accept": "application/vnd.heroku+json; version=3.account-quotas",
}
herokuId = Heroku.account().id
apiUrl = f"https://api.heroku.com/accounts/{herokuId}/actions/get-quota"
response = requests.get(apiUrl, headers=HEADERS)
if response.status_code != 200:
return await hellbot.error(message, "Something went wrong!")
result: dict = response.json()
herokuApp = result.get("apps", {})
quota = result.get("account_quota", 0)
quotaUsed = result.get("quota_used", 0)
quotaRemaining = quota - quotaUsed
minutesRemaining = quotaRemaining / 60
percentage = math.floor(quotaRemaining / quota * 100)
hours = math.floor(minutesRemaining / 60)
minutes = math.floor(minutesRemaining % 60)
try:
herokuApp[0]["quota_used"]
except IndexError:
appQuotaUsed = 0
appPercentage = 0
else:
appQuotaUsed = herokuApp[0]["quota_used"] / 60
appPercentage = math.floor(herokuApp[0]["quota_used"] / quota * 100)
appHours = math.floor(appQuotaUsed / 60)
appMinutes = math.floor(appQuotaUsed % 60)
try:
disk = psutil.disk_usage("/")
diskTotal = int(disk.total / (1024.0**3))
diskUsed = int(disk.used / (1024.0**3))
diskPercent = disk.percent
except:
diskTotal = 0
diskUsed = 0
diskPercent = 0
try:
memory = psutil.virtual_memory()
memoryTotal = int(memory.total / (1024.0**3))
memoryUsed = int(memory.used / (1024.0**3))
memoryPercent = memory.percent
except:
memoryTotal = 0
memoryUsed = 0
memoryPercent = 0
await hell.edit(
await usage_templates(
appName=Config.HEROKU_APPNAME,
appHours=appHours,
appMinutes=appMinutes,
appPercentage=appPercentage,
hours=hours,
minutes=minutes,
percentage=percentage,
diskUsed=diskUsed,
diskTotal=diskTotal,
diskPercent=diskPercent,
memoryUsed=memoryUsed,
memoryTotal=memoryTotal,
memoryPercent=memoryPercent,
)
)
@on_message("logs", allow_stan=True)
async def getLogs(_, message: Message):
limit = int(message.command[1]) if len(message.command) > 1 else 100
try:
if os.path.exists("HellBot.log"):
with open("HellBot.log", "r") as file:
logData = file.readlines()
logData = "".join(logData[-limit:])
file.close()
with open("log.txt", "w") as file:
file.write(logData)
file.close()
link = spaceBin(logData)
await message.reply_document(
"log.txt",
caption=f"**๐ซ๐๐๐ ๐๐ ๐
๐๐๐:** [click here]({link})",
file_name="log.txt",
)
os.remove("log.txt")
except Exception as e:
await hellbot.error(message, str(e))
HelpMenu("manager").add(
"getvar",
"<varname>",
"Get value of a varname from env or database.",
"getvar handler",
).add(
"getallvar",
None,
"Get the name of every env and database variable.",
).add(
"setvar",
"<varname> <value>",
"Set value of a varname to env or database.",
"setvar handler !",
).add(
"delvar", "<varname>", "Delete a varname from env or database.", "delvar handler"
).add(
"usage",
None,
"Get dyno, disk and RAM usage of your bot server.",
"usage",
"Dyno usage is only available for heroku users.",
).add(
"logs",
"<limit>",
"Get last 'n' lines of log file. By default limit is 100 lines if not mentioned with the command",
"logs 69",
).info(
"Manage your bot with ease!"
).done()
|