-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeep_alive.py
96 lines (77 loc) · 2.77 KB
/
keep_alive.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
from flask import Flask
from threading import Thread
import time
from subprocess import call
import discord
import asyncio
from datetime import datetime
from os import system
app = Flask('')
#d_client = discord.Client()
@app.route('/')
def home():
return "Hello, I am alive!"
def run():
app.run(host='0.0.0.0', port=8080)
def keep_alive():
t = Thread(target=run)
t.start()
#If you are not on Replit, you probably don't need to use this function
#This function checks the connection to discord and restarts to dodge the 429 TooManyRequests limit
async def keep_discord_connection(d_client):
await asyncio.sleep(10)
dc_conn = 0
dc_conn_last = 1
server = d_client.get_guild(973611254696542338)
channel = server.get_channel(988684103257833472)
msg_obj = await channel.fetch_message(988687689584885820)
#https://discord.com/channels/973611254696542338/988684103257833472/988687689584885820
await channel.send("Starting Mother AI")
while True:
try:
user = await d_client.fetch_user(d_client.user.id)
dc_conn = 0
except:
print("__________________________________")
now = datetime.now()
current_time = now.strftime("%D, %H:%M:%S")
print("Current Time =", current_time)
print("I CAN'T FETCH MYSELF ;(")
system("python restarter_2.py")
with open('restart.sh', 'rb') as file:
script = file.read()
#rc = call(script, shell=True)
rc = call("kill 1", shell=True)
dc_conn = 1
finally:
if dc_conn != dc_conn_last:
print("__________________________________")
now = datetime.now()
current_time = now.strftime("%D, %H:%M:%S")
print("Current Time =", current_time)
if dc_conn == 1:
print("I CAN'T FETCH MYSELF ;(")
elif dc_conn == 0:
print("I can fetch myself :)")
dc_conn_last = dc_conn
await asyncio.sleep(30)
async def restart_repl(pid):
print("__________________________________")
now = datetime.now()
current_time = now.strftime("%D, %H:%M:%S")
print("Current Time =", current_time)
print("I CAN'T FETCH MYSELF ;(")
print("System will be restarted soon")
print("pid: " + str(pid))
await asyncio.sleep(60)
system("python restarter_2.py")
print("python restarter_2.py")
system("kill {0}".format(pid))
print("kill {0}".format(pid))
system("kill 1")
print("kill 1")
with open('restart.sh', 'rb') as file:
script = file.read()
#rc = call(script, shell=True)
system("kill {0}".format(pid))
rc = call("kill 1", shell=True)