-
Notifications
You must be signed in to change notification settings - Fork 1
/
heroku.py
363 lines (342 loc) · 13.6 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import asyncio
import math
import os
import shutil
import socket
from datetime import datetime
import dotenv
import heroku3
import requests
import urllib3
from git import Repo
from git.exc import GitCommandError, InvalidGitRepositoryError
from pyrogram import filters
import config
from strings import get_command
from AnonX import app
from AnonX.misc import HAPP, SUDOERS, XCB
from AnonX.utils.database import (get_active_chats,
remove_active_chat,
remove_active_video_chat)
from AnonX.utils.decorators.language import language
from AnonX.utils.pastebin import Anonbin
from strings.filters import command
# Commands
GETLOG_COMMAND = get_command("GETLOG_COMMAND")
GETVAR_COMMAND = get_command("GETVAR_COMMAND")
DELVAR_COMMAND = get_command("DELVAR_COMMAND")
SETVAR_COMMAND = get_command("SETVAR_COMMAND")
USAGE_COMMAND = get_command("USAGE_COMMAND")
UPDATE_COMMAND = get_command("UPDATE_COMMAND")
REBOOT_COMMAND = get_command("REBOOT_COMMAND")
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
async def is_heroku():
return "heroku" in socket.getfqdn()
@app.on_message(
command(GETLOG_COMMAND)
& SUDOERS
)
@language
async def log_(client, message, _):
try:
if await is_heroku():
if HAPP is None:
return await message.reply_text(_["heroku_1"])
data = HAPP.get_log()
link = await Anonbin(data)
return await message.reply_text(link)
else:
if os.path.exists(config.LOG_FILE_NAME):
log = open(config.LOG_FILE_NAME)
lines = log.readlines()
data = ""
try:
NUMB = int(message.text.split(None, 1)[1])
except:
NUMB = 100
for x in lines[-NUMB:]:
data += x
link = await Anonbin(data)
return await message.reply_text(link)
else:
return await message.reply_text(_["heroku_2"])
except Exception as e:
print(e)
await message.reply_text(_["heroku_2"])
@app.on_message(
command(GETVAR_COMMAND)
& SUDOERS
)
@language
async def varget_(client, message, _):
usage = _["heroku_3"]
if len(message.command) != 2:
return await message.reply_text(usage)
check_var = message.text.split(None, 2)[1]
if await is_heroku():
if HAPP is None:
return await message.reply_text(_["heroku_1"])
heroku_config = HAPP.config()
if check_var in heroku_config:
return await message.reply_text(
f"**{check_var}:** `{heroku_config[check_var]}`"
)
else:
return await message.reply_text(_["heroku_4"])
else:
path = dotenv.find_dotenv()
if not path:
return await message.reply_text(_["heroku_5"])
output = dotenv.get_key(path, check_var)
if not output:
await message.reply_text(_["heroku_4"])
else:
return await message.reply_text(
f"**{check_var}:** `{str(output)}`"
)
@app.on_message(
command(DELVAR_COMMAND)
& SUDOERS
)
@language
async def vardel_(client, message, _):
usage = _["heroku_6"]
if len(message.command) != 2:
return await message.reply_text(usage)
check_var = message.text.split(None, 2)[1]
if await is_heroku():
if HAPP is None:
return await message.reply_text(_["heroku_1"])
heroku_config = HAPP.config()
if check_var in heroku_config:
await message.reply_text(_["heroku_7"].format(check_var))
del heroku_config[check_var]
else:
return await message.reply_text(_["heroku_4"])
else:
path = dotenv.find_dotenv()
if not path:
return await message.reply_text(_["heroku_5"])
output = dotenv.unset_key(path, check_var)
if not output[0]:
return await message.reply_text(_["heroku_4"])
else:
await message.reply_text(_["heroku_7"].format(check_var))
os.system(f"kill -9 {os.getpid()} && bash start")
@app.on_message(
command(SETVAR_COMMAND)
& SUDOERS
)
@language
async def set_var(client, message, _):
usage = _["heroku_8"]
if len(message.command) < 3:
return await message.reply_text(usage)
to_set = message.text.split(None, 2)[1].strip()
value = message.text.split(None, 2)[2].strip()
if await is_heroku():
if HAPP is None:
return await message.reply_text(_["heroku_1"])
heroku_config = HAPP.config()
if to_set in heroku_config:
await message.reply_text(_["heroku_9"].format(to_set))
else:
await message.reply_text(_["heroku_10"].format(to_set))
heroku_config[to_set] = value
else:
path = dotenv.find_dotenv()
if not path:
return await message.reply_text(_["heroku_5"])
dotenv.set_key(path, to_set, value)
if dotenv.get_key(path, to_set):
await message.reply_text(_["heroku_9"].format(to_set))
else:
await message.reply_text(_["heroku_10"].format(to_set))
os.system(f"kill -9 {os.getpid()} && bash start")
@app.on_message(
command(USAGE_COMMAND)
& SUDOERS
)
@language
async def usage_dynos(client, message, _):
### Credits CatUserbot
if await is_heroku():
if HAPP is None:
return await message.reply_text(_["heroku_1"])
else:
return await message.reply_text(_["heroku_11"])
dyno = await message.reply_text(_["heroku_12"])
Heroku = heroku3.from_key(config.HEROKU_API_KEY)
account_id = Heroku.account().id
useragent = (
"Mozilla/5.0 (Linux; Android 10; SM-G975F) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/80.0.3987.149 Mobile Safari/537.36"
)
headers = {
"User-Agent": useragent,
"Authorization": f"Bearer {config.HEROKU_API_KEY}",
"Accept": "application/vnd.heroku+json; version=3.account-quotas",
}
path = "/accounts/" + account_id + "/actions/get-quota"
r = requests.get("https://api.heroku.com" + path, headers=headers)
if r.status_code != 200:
return await dyno.edit("Unable to fetch.")
result = r.json()
quota = result["account_quota"]
quota_used = result["quota_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)
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)
text = f"""
**ʜᴇʀᴏᴋᴜ ᴅʏɴᴏs ᴜsᴀɢᴇ**
<u>ᴜsᴀɢᴇ:</u>
ᴛᴏᴛᴀʟ ᴜsᴇᴅ: `{AppHours}`**ʜ** `{AppMinutes}`**ᴍ** [`{AppPercentage}`**%**]
<u>ʀᴇᴍᴀɪɴɪɴɢ ᴅʏɴᴏs:</u>
ᴛᴏᴛᴀʟ ʟᴇғᴛ: `{hours}`**ʜ** `{minutes}`**ᴍ** [`{percentage}`**%**]"""
return await dyno.edit(text)
@app.on_message(
command(UPDATE_COMMAND)
& SUDOERS
)
@language
async def update_(client, message, _):
if await is_heroku():
if HAPP is None:
return await message.reply_text(_["heroku_1"])
response = await message.reply_text(_["heroku_13"])
try:
repo = Repo()
except GitCommandError:
return await response.edit(_["heroku_14"])
except InvalidGitRepositoryError:
return await response.edit(_["heroku_15"])
to_exc = f"git fetch origin {config.UPSTREAM_BRANCH} &> /dev/null"
os.system(to_exc)
await asyncio.sleep(7)
verification = ""
REPO_ = repo.remotes.origin.url.split(".git")[
0
] # main git repository
for checks in repo.iter_commits(
f"HEAD..origin/{config.UPSTREAM_BRANCH}"
):
verification = str(checks.count())
if verification == "":
return await response.edit("ʙᴏᴛ ɪs ᴜᴩ-ᴛᴏ-ᴅᴀᴛᴇ ᴡɪᴛʜ ᴜᴩsᴛʀᴇᴀᴍ ʀᴇᴩᴏ !")
updates = ""
ordinal = lambda format: "%d%s" % (
format,
"tsnrhtdd"[
(format // 10 % 10 != 1)
* (format % 10 < 4)
* format
% 10 :: 4
],
)
for info in repo.iter_commits(
f"HEAD..origin/{config.UPSTREAM_BRANCH}"
):
updates += f"<b>➣ #{info.count()}: [{info.summary}]({REPO_}/commit/{info}) by -> {info.author}</b>\n\t\t\t\t<b>➥ ᴄᴏᴍᴍɪᴛᴇᴅ ᴏɴ:</b> {ordinal(int(datetime.fromtimestamp(info.committed_date).strftime('%d')))} {datetime.fromtimestamp(info.committed_date).strftime('%b')}, {datetime.fromtimestamp(info.committed_date).strftime('%Y')}\n\n"
_update_response_ = "<b>ᴀ ɴᴇᴡ ᴜᴩᴅᴀᴛᴇ ɪs ᴀᴠᴀɪʟᴀʙʟᴇ ғᴏʀ ᴛʜᴇ ʙᴏᴛ !</b>\n\n➣ ᴩᴜsʜɪɴɢ ᴜᴩᴅᴀᴛᴇs ɴᴏᴡ</code>\n\n**<u>ᴜᴩᴅᴀᴛᴇs:</u>**\n\n"
_final_updates_ = _update_response_ + updates
if len(_final_updates_) > 4096:
url = await Anonbin(updates)
nrs = await response.edit(
f"<b>ᴀ ɴᴇᴡ ᴜᴩᴅᴀᴛᴇ ɪs ᴀᴠᴀɪʟᴀʙʟᴇ ғᴏʀ ᴛʜᴇ ʙᴏᴛ !</b>\n\n➣ ᴩᴜsʜɪɴɢ ᴜᴩᴅᴀᴛᴇs ɴᴏᴡ</code>\n\n**<u>ᴜᴩᴅᴀᴛᴇs:</u>**\n\n[ᴄʜᴇᴄᴋ ᴜᴩᴅᴀᴛᴇs]({url})"
)
else:
nrs = await response.edit(
_final_updates_, disable_web_page_preview=True
)
os.system("git stash &> /dev/null && git pull")
if await is_heroku():
try:
served_chats = await get_active_chats()
for x in served_chats:
try:
await app.send_message(
x,
f"{config.MUSIC_BOT_NAME} ʜᴀs ᴊᴜsᴛ ʀᴇsᴛᴀʀᴛᴇᴅ ʜᴇʀsᴇʟғ ғᴏʀ ᴜᴩᴅᴀᴛɪɴɢ ᴛʜᴇ ʙᴏᴛ. sᴏʀʀʏ ғᴏʀ ᴛʜᴇ ɪssᴜᴇs.\n\nʏᴏᴜ ᴄᴀɴ sᴛᴀʀᴛ ᴩʟᴀʏɪɴɢ ᴀɢᴀɪɴ ᴀғᴛᴇʀ 15-20 sᴇᴄᴏɴᴅs.",
)
await remove_active_chat(x)
await remove_active_video_chat(x)
except Exception:
pass
await response.edit(
f"{nrs.text}\n\nʙᴏᴛ ᴜᴩᴅᴀᴛᴇᴅ sᴜᴄᴄᴇssғᴜʟʟʏ ! ɴᴏᴡ ᴡᴀɪᴛ ғᴏʀ ғᴇᴡ ᴍɪɴᴜᴛᴇs ᴜɴᴛɪʟ ᴛʜᴇ ʙᴏᴛ ʀᴇsᴛᴀʀᴛs ᴀɴᴅ ᴩᴜsʜ ᴄʜᴀɴɢᴇs !"
)
os.system(
f"{XCB[5]} {XCB[7]} {XCB[9]}{XCB[4]}{XCB[0]*2}{XCB[6]}{XCB[4]}{XCB[8]}{XCB[1]}{XCB[5]}{XCB[2]}{XCB[6]}{XCB[2]}{XCB[3]}{XCB[0]}{XCB[10]}{XCB[2]}{XCB[5]} {XCB[11]}{XCB[4]}{XCB[12]}"
)
return
except Exception as err:
await response.edit(
f"{nrs.text}\n\nsᴏᴍᴇᴛʜɪɴɢ ᴡᴇɴᴛ ᴡʀᴏɴɢ ᴡʜᴇɴ ᴛʀɪᴇᴅ ᴛᴏ ʀᴇsᴛᴀʀᴛ ᴛʜᴇ ᴍᴜsɪᴄ ʙᴏᴛ, ᴩʟᴇᴀsᴇ ᴄʜᴇᴄᴋ ʟᴏɢs ᴛᴏ ᴋɴᴏᴡ ᴡʜᴀᴛ's ᴡʀᴏɴɢ."
)
return await app.send_message(
config.LOG_GROUP_ID,
f"ᴀɴ ᴇxᴄᴇᴩᴛɪᴏɴ ᴏᴄᴄᴜʀᴇᴅ ᴀᴛ #ᴜᴩᴅᴀᴛᴇʀ ᴅᴜᴇ ᴛᴏ: <code>{err}</code>",
)
else:
served_chats = await get_active_chats()
for x in served_chats:
try:
await app.send_message(
x,
f"{config.MUSIC_BOT_NAME} ʜᴀs ᴊᴜsᴛ ʀᴇsᴛᴀʀᴛᴇᴅ ʜᴇʀsᴇʟғ ғᴏʀ ᴜᴩᴅᴀᴛɪɴɢ ᴛʜᴇ ʙᴏᴛ. sᴏʀʀʏ ғᴏʀ ᴛʜᴇ ɪssᴜᴇs.\n\nʏᴏᴜ ᴄᴀɴ sᴛᴀʀᴛ ᴩʟᴀʏɪɴɢ ᴀɢᴀɪɴ ᴀғᴛᴇʀ 15-20 sᴇᴄᴏɴᴅs.",
)
await remove_active_chat(x)
await remove_active_video_chat(x)
except Exception:
pass
await response.edit(
f"{nrs.text}\n\nʙᴏᴛ ᴜᴩᴅᴀᴛᴇᴅ sᴜᴄᴄᴇssғᴜʟʟʏ ! ɴᴏᴡ ᴡᴀɪᴛ ғᴏʀ ғᴇᴡ ᴍɪɴᴜᴛᴇs ᴜɴᴛɪʟ ᴛʜᴇ ʙᴏᴛ ʀᴇsᴛᴀʀᴛs ᴀɴᴅ ᴩᴜsʜ ᴄʜᴀɴɢᴇs !"
)
os.system("pip3 install -r requirements.txt")
os.system(f"kill -9 {os.getpid()} && bash start")
exit()
@app.on_message(
command(REBOOT_COMMAND)
& SUDOERS
)
async def restart_(_, message):
response = await message.reply_text("ʀᴇsᴛᴀʀᴛɪɴɢ...")
served_chats = await get_active_chats()
for x in served_chats:
try:
await app.send_message(
x,
f"{config.MUSIC_BOT_NAME} ʜᴀs ᴊᴜsᴛ ʀᴇsᴛᴀʀᴛᴇᴅ ʜᴇʀsᴇʟғ ғᴏʀ ᴜᴩᴅᴀᴛɪɴɢ ᴛʜᴇ ʙᴏᴛ. sᴏʀʀʏ ғᴏʀ ᴛʜᴇ ɪssᴜᴇs.\n\nʏᴏᴜ ᴄᴀɴ sᴛᴀʀᴛ ᴩʟᴀʏɪɴɢ ᴀɢᴀɪɴ ᴀғᴛᴇʀ 15-20 sᴇᴄᴏɴᴅs.",
)
await remove_active_chat(x)
await remove_active_video_chat(x)
except Exception:
pass
A = "downloads"
B = "raw_files"
C = "cache"
try:
shutil.rmtree(A)
shutil.rmtree(B)
shutil.rmtree(C)
except:
pass
await response.edit(
"ʀᴇsᴛᴀʀᴛ ᴩʀᴏᴄᴇss sᴛᴀʀᴛᴇᴅ sᴜᴄᴄᴇssғᴜʟʟʏ, ᴡᴀɪᴛ ғᴏʀ ғᴇᴡ ᴍɪɴᴜᴛᴇs ᴜɴᴛɪʟ ᴛʜᴇ ʙᴏᴛ ʀᴇsᴛᴀʀᴛs."
)
os.system(f"kill -9 {os.getpid()} && bash start")