-
Notifications
You must be signed in to change notification settings - Fork 1
/
heroku.py
291 lines (264 loc) · 11.2 KB
/
heroku.py
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import asyncio
import math
import os
import heroku3
import requests
import urllib3
import sys
from os import execl
from time import sleep
from . import *
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
Heroku = heroku3.from_key(Config.HEROKU_API_KEY)
heroku_api = "https://api.heroku.com"
HEROKU_APP_NAME = Config.HEROKU_APP_NAME
HEROKU_API_KEY = Config.HEROKU_API_KEY
lg_id = Config.LOGGER_ID
async def restart(event):
if HEROKU_APP_NAME and HEROKU_API_KEY:
try:
Heroku
except BaseException:
return await eor(
event, "`HEROKU_API_KEY` is wrong. Re-Check in config vars."
)
await eor(event, f"**✓ʀᴇsᴛᴀʀᴛᴇᴅ ᴅʏɴᴏs** \n **𝚃𝚢𝚙𝚎** `{hl}ping` **𝚊𝚏𝚝𝚎𝚛 1 𝚖𝚒𝚗𝚞𝚝𝚎 𝚝𝚘 𝚌𝚑𝚎𝚌𝚔 𝚒𝚏 𝙸 𝚊𝚖 𝚠𝚘𝚛𝚔𝚒𝚗𝚐 !**")
app = Heroku.apps()[HEROKU_APP_NAME]
app.restart()
else:
execl(executable, executable, "bash", "D3vilBot")
@bot.on(d3vil_cmd(pattern="restart$"))
@bot.on(sudo_cmd(pattern="restart$", allow_sudo=True))
async def re(d3vil):
if d3vil.fwd_from:
return
event = await eor(d3vil, "Restarting Dynos ...")
if HEROKU_API_KEY:
await restart(event)
else:
await event.edit("Please Set Your `HEROKU_API_KEY` to restart ∂3víℓвσт")
@bot.on(d3vil_cmd(pattern="shutdown$"))
@bot.on(sudo_cmd(pattern="shutdown$", allow_sudo=True))
async def down(d3vil):
if d3vil.fwd_from:
return
await eor(d3vil, "**[ ! ]** Turning off 𝖣3𝗏𝗂𝗅𝖡𝗈𝗍 Dynos... Manually turn me on later ಠ_ಠ")
if HEROKU_APP is not None:
HEROKU_APP.process_formation()["worker"].scale(0)
else:
sys.exit(0)
@bot.on(d3vil_cmd(pattern="(set|get|del) var(?: |$)(.*)(?: |$)([\s\S]*)", outgoing=True))
@bot.on(sudo_cmd(pattern="(set|get|del) var(?: |$)(.*)(?: |$)([\s\S]*)", allow_sudo=True))
async def variable(d3vil):
if d3vil.fwd_from:
return
if Config.HEROKU_APP_NAME is not None:
app = Heroku.app(Config.HEROKU_APP_NAME)
else:
return await eor(d3vil, "`[HEROKU]:" "\nPlease setup your` **HEROKU_APP_NAME**")
exe = d3vil.pattern_match.group(1)
heroku_var = app.config()
if exe == "get":
event = await eor(d3vil, "Getting Variable Info...")
await asyncio.sleep(1.5)
cap = "Logger me chala jaa bsdk."
capn = "Saved in LOGGER_ID !!"
try:
variable = d3vil.pattern_match.group(2).split()[0]
if variable in ("D3VILBOT_SESSION", "BOT_TOKEN", "HEROKU_API_KEY"):
if Config.ABUSE == "ON":
await bot.send_file(d3vil.chat_id, cjb, caption=cap)
await event.delete()
await bot.send_message(lg_id, f"#HEROKU_VAR \n\n`{heroku_var[variable]}`")
return
else:
await event.edit(f"**{capn}**")
await bot.send_message(lg_id, f"#HEROKU_VAR \n\n`{heroku_var[variable]}`")
return
if variable in heroku_var:
return await event.edit(
"**Heroku Var** :" f"\n\n`{variable}` = `{heroku_var[variable]}`\n"
)
else:
return await event.edit(
"**Heroku Var** :" f"\n\n__Error:__\n-> I doubt `{variable}` exists!"
)
except IndexError:
configs = prettyjson(heroku_var.to_dict(), indent=2)
with open("configs.json", "w") as fp:
fp.write(configs)
with open("configs.json", "r") as fp:
result = fp.read()
if len(result) >= 4096:
await hell.client.send_file(
hell.chat_id,
"configs.json",
reply_to=hell.id,
caption="`Output too large, sending it as a file`",
)
else:
await event.edit(
"**Heroku Var :**\n\n"
"================================"
f"\n```{result}```\n"
"================================"
)
os.remove("configs.json")
return
elif exe == "set":
event = await eor(d3vil, "Setting Heroku Variable...")
variable = d3vil.pattern_match.group(2)
if not variable:
return await event.edit(f"`{hl}set var <Var Name> <Value>`")
value = d3vil.pattern_match.group(3)
if not value:
variable = variable.split()[0]
try:
value = d3vil.pattern_match.group(2).split()[1]
except IndexError:
return await event.edit(f"`{hl}set var <Var Name> <Value>`")
await asyncio.sleep(1.5)
if variable in heroku_var:
await event.edit(
f"`{variable}` **successfully changed to** -> `{value}`"
)
else:
await event.edit(
f"`{variable}` **successfully added with value** -> `{value}`"
)
heroku_var[variable] = value
elif exe == "del":
event = await eor(d3vil, "Getting info to delete Variable")
try:
variable = d3vil.pattern_match.group(2).split()[0]
except IndexError:
return await event.edit("`Please specify ConfigVars you want to delete`")
await asyncio.sleep(1.5)
if variable in heroku_var:
await event.edit(f"**Successfully Deleted** \n`{variable}`")
del heroku_var[variable]
else:
return await event.edit(f"`{variable}` **does not exists**")
@bot.on(d3vil_cmd(pattern="usage(?: |$)", outgoing=True))
@bot.on(sudo_cmd(pattern="usage(?: |$)", allow_sudo=True))
async def dyno_usage(d3vil):
if d3vil.fwd_from:
return
event = await edit_or_reply(d3vil, "`Processing...`")
useragent = (
"Mozilla/5.0 (Linux; Android 10; SM-G975F) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/80.0.3987.149 Mobile Safari/537.36"
)
user_id = Heroku.account().id
headers = {
"User-Agent": useragent,
"Authorization": f"Bearer {Config.HEROKU_API_KEY}",
"Accept": "application/vnd.heroku+json; version=3.account-quotas",
}
path = "/accounts/" + user_id + "/actions/get-quota"
r = requests.get(heroku_api + path, headers=headers)
if r.status_code != 200:
return await event.edit(
"`Error: something bad happened`\n\n" f">.`{r.reason}`\n"
)
result = r.json()
quota = result["account_quota"]
quota_used = result["quota_used"]
""" - Used - """
remaining_quota = quota - quota_used
percentage = math.floor(remaining_quota / quota * 100)
minutes_remaining = remaining_quota / 60
hours = math.floor(minutes_remaining / 60)
minutes = math.floor(minutes_remaining % 60)
""" - Current - """
App = result["apps"]
try:
App[0]["quota_used"]
except IndexError:
AppQuotaUsed = 0
AppPercentage = 0
else:
AppQuotaUsed = App[0]["quota_used"] / 60
AppPercentage = math.floor(App[0]["quota_used"] * 100 / quota)
AppHours = math.floor(AppQuotaUsed / 60)
AppMinutes = math.floor(AppQuotaUsed % 60)
await asyncio.sleep(1.5)
return await event.edit(
"✯ **𝙳𝚈𝙽𝙾 𝚄𝚂𝙰𝙶𝙴** ✯:\n\n"
f" ➪ __𝙳𝚈𝙽𝙾 𝚄𝚂𝙰𝙶𝙴 𝙵𝙾𝚁__ • **{Config.HEROKU_APP_NAME}** • :\n"
f" ✩ `{AppHours}`**h** `{AppMinutes}`**m** "
f"**|** `{AppPercentage}`**%**"
"\n\n"
" ➪ __𝙳𝚈𝙽𝙾 𝙷𝙾𝚄𝚁𝚂 𝚁𝙴𝙼𝙰𝙸𝙽𝙸𝙽𝙶 𝚃𝙷𝙸𝚂 𝙼𝙾𝙽𝚃𝙷__ :\n"
f" ☆ `{hours}`**𝙷** `{minutes}`**𝙼** "
f"**|** `{percentage}`**%**"
f"\n\n**𝙼𝙰𝚂𝚃𝙴𝚁 ➪ :** {d3vil_mention}"
)
@bot.on(d3vil_cmd(pattern="logs$"))
@bot.on(sudo_cmd(pattern="logs$", allow_sudo=True))
async def _(dyno):
if (HEROKU_APP_NAME is None) or (HEROKU_API_KEY is None):
return await eor(dyno, f"Make Sure Your HEROKU_APP_NAME & HEROKU_API_KEY are filled correct. Visit {d3vil_grp} for help.", link_preview=False)
try:
Heroku = heroku3.from_key(HEROKU_API_KEY)
app = Heroku.app(HEROKU_APP_NAME)
except BaseException:
return await dyno.reply(f"Make Sure Your Heroku AppName & API Key are filled correct. Visit {d3vil_grp} for help.", link_preview=False)
event = await eor(dyno, "Downloading Logs...")
with open("d3vilbot-logs.txt", "w") as log:
log.write(app.get_log())
await bot.send_file(
dyno.chat_id,
"d3vilbot-logs.txt",
reply_to=dyno.id,
caption=f"**✔︎ 𝙷𝙴𝚁𝙾𝙺𝚄 𝙻𝙾𝙶𝚂 𝙾𝙵 💯 𝙻𝙸𝙽𝙴𝚂. 🗒️**\n\n **𝙼𝙰𝚂𝚃𝙴𝚁 ➪** {d3vil_mention}"
)
await event.edit("Heroku Logs..")
await asyncio.sleep(5)
await event.delete()
return os.remove("d3vilbot-logs.txt")
# d3vil_data = app.get_log()
# await eor(
# dyno, d3vil_data, deflink=True, linktext=f"**✔︎ 𝙷𝙴𝚁𝙾𝙺𝚄 𝙻𝙾𝙶𝚂 𝙾𝙵 💯 𝙻𝙸𝙽𝙴𝚂. 🗒️**\n\n **𝙼𝙰𝚂𝚃𝙴𝚁 ➪** {d3vil_mention}\n\n🚀** 𝙿𝙰𝚂𝚃𝙴𝙳** "
# )
"""
key = (
requests.post("https://nekobin.com/api/documents", json={"content": d3vil_data})
.json()
.get("result")
.get("key")
)
d3vil_url = f"https://nekobin.com/{key}"
url_raw = f"https://nekobin.com/raw/{key}"
foutput = f"**✔︎ 𝙷𝙴𝚁𝙾𝙺𝚄 𝙻𝙾𝙶𝚂 𝙾𝙵 💯 𝙻𝙸𝙽𝙴𝚂. 🗒️** \n\n [Nekobin]({d3vil_url}) & [Raw]({url_raw}) \n\n **𝙼𝙰𝚂𝚃𝙴𝚁 ➪** {d3vil_mention}"
"""
def prettyjson(obj, indent=2, maxlinelength=80):
"""Renders JSON content with indentation and line splits/concatenations to fit maxlinelength.
Only dicts, lists and basic types are supported"""
items, _ = getsubitems(
obj,
itemkey="",
islast=True,
maxlinelength=maxlinelength - indent,
indent=indent,
)
return indentitems(items, indent, level=0)
CmdHelp("power").add_command(
"restart", None, "Restarts your userbot. Redtarting Bot may result in better functioning of bot when its laggy"
).add_command(
"shutdown", None, "Turns off Dynos of Userbot. Userbot will stop working unless you manually turn it on from heroku"
).add_info(
"Power Switch For Bot"
).add()
CmdHelp("heroku").add_command(
"usage", None, "Check your heroku dyno hours status."
).add_command(
"set var", "<Var Name> <value>", "Add new variable or update existing value/variable\nAfter setting a variable bot will restart so stay calm for 1 minute."
).add_command(
"get var", "<Var Name", "Gets the variable and its value (if any) from heroku."
).add_command(
"del var", "<Var Name", "Deletes the variable from heroku. Bot will restart after deleting the variable. So be calm for a minute 😃"
).add_command(
"logs", None, "Gets the app log of 100 lines of your bot directly from heroku."
).add()