modular-bot-framework-for-t.../modules/pingtools/index.py

130 lines
4.7 KiB
Python
Raw Normal View History

2023-10-09 23:48:08 +03:00
msg = self.MESSAGE.text.lower()
if msg == "!ping":
2023-09-26 12:58:40 +03:00
current_time = time.time()
delay = current_time - 7200 - float(self.MESSAGE.date.strftime('+%s'))
self.RESPONSE = f"Pong in {delay} seconds"
2023-09-21 19:32:56 +03:00
2023-09-26 12:58:40 +03:00
if not os.path.exists(self.path + "scoreboard/"):
os.mkdir(self.path + "scoreboard/")
def append_score(path, name, data):
open(path + f"scoreboard/{name}", "a").write(data)
user_id = self.MESSAGE.from_user.id
chat_id = self.MESSAGE.chat.id
first_name = self.MESSAGE.from_user.first_name
last_name = self.MESSAGE.from_user.last_name
username = self.MESSAGE.from_user.username
append_score(self.path, self.MESSAGE.from_user.id, f"{current_time} {user_id} {chat_id} {first_name} {last_name} {username} {delay}\n")
2023-10-09 23:48:08 +03:00
elif msg == "!pong":
2023-09-26 12:58:40 +03:00
current_time = time.time()
delay = current_time - 7200 - float(self.MESSAGE.date.strftime('+%s'))
self.RESPONSE = f"Ping in {delay} seconds"
if not os.path.exists(self.path + "pong-scoreboard/"):
os.mkdir(self.path + "pong-scoreboard/")
def append_score(path, name, data):
open(path + f"pong-scoreboard/{name}", "a").write(data)
user_id = self.MESSAGE.from_user.id
chat_id = self.MESSAGE.chat.id
first_name = self.MESSAGE.from_user.first_name
last_name = self.MESSAGE.from_user.last_name
username = self.MESSAGE.from_user.username
append_score(self.path, self.MESSAGE.from_user.id, f"{current_time} {user_id} {chat_id} {first_name} {last_name} {username} {delay}\n")
2023-10-09 23:48:08 +03:00
elif msg == "!pingtop":
2023-09-26 12:58:40 +03:00
def read_score(path, name):
if os.path.exists(path + f"scoreboard/{name}"):
return open(path + f"scoreboard/{name}").read()
else:
return None
if not os.path.exists(self.path + "scoreboard/"):
os.mkdir(self.path + "scoreboard/")
results = []
for name in os.listdir(self.path + "scoreboard/"):
data = [i for i in read_score(self.path, name).split("\n") if i != ""]
data.sort(key = lambda x: float(x.split()[6]))
results.append(data[0].split())
print(results)
2023-10-09 23:48:08 +03:00
results.sort(key = lambda x: float(x[6]))
2023-09-26 12:58:40 +03:00
self.RESPONSE = "<u>Ping top</u>:\n" + "\n".join( [f"{i+1}. {v[3]} {v[4]} ({v[5]}) - {v[6]}" for i, v in enumerate(results[:10])] )
self.FORMAT = "HTML"
2023-10-09 23:48:08 +03:00
elif msg == "!pongtop":
2023-09-26 12:58:40 +03:00
def read_score(path, name):
if os.path.exists(path + f"pong-scoreboard/{name}"):
return open(path + f"pong-scoreboard/{name}").read()
else:
return None
if not os.path.exists(self.path + "pong-scoreboard/"):
os.mkdir(self.path + "pong-scoreboard/")
results = []
for name in os.listdir(self.path + "pong-scoreboard/"):
data = [i for i in read_score(self.path, name).split("\n") if i != ""]
data.sort(key = lambda x: float(x.split()[6]))
results.append(data[0].split())
print(results)
2023-10-09 23:48:08 +03:00
results.sort(key = lambda x: float(x[6]))
2023-09-26 12:58:40 +03:00
self.RESPONSE = "<u>Pong top</u>:\n" + "\n".join( [f"{i+1}. {v[3]} {v[4]} ({v[5]}) - {v[6]}" for i, v in enumerate(results[:10])] )
self.FORMAT = "HTML"
2023-10-09 23:48:08 +03:00
elif msg == "!pingantitop":
2023-09-26 12:58:40 +03:00
def read_score(path, name):
if os.path.exists(path + f"scoreboard/{name}"):
return open(path + f"scoreboard/{name}").read()
else:
return None
if not os.path.exists(self.path + "scoreboard/"):
os.mkdir(self.path + "scoreboard/")
results = []
for name in os.listdir(self.path + "scoreboard/"):
data = [i for i in read_score(self.path, name).split("\n") if i != ""]
data.sort(key = lambda x: float(x.split()[6]))
results.append(data[-1].split())
print(results)
2023-10-09 23:48:08 +03:00
results.sort(key = lambda x: float(x[6]))
2023-09-26 12:58:40 +03:00
results = results[::-1]
self.RESPONSE = "<u>Ping antitop</u>:\n" + "\n".join( [f"{i+1}. {v[3]} {v[4]} ({v[5]}) - {v[6]}" for i, v in enumerate(results[:10])] )
self.FORMAT = "HTML"
2023-10-09 23:48:08 +03:00
elif msg == "!pongantitop":
2023-09-26 12:58:40 +03:00
def read_score(path, name):
if os.path.exists(path + f"pong-scoreboard/{name}"):
return open(path + f"pong-scoreboard/{name}").read()
else:
return None
if not os.path.exists(self.path + "pong-scoreboard/"):
os.mkdir(self.path + "pong-scoreboard/")
results = []
for name in os.listdir(self.path + "pong-scoreboard/"):
data = [i for i in read_score(self.path, name).split("\n") if i != ""]
data.sort(key = lambda x: float(x.split()[6]))
results.append(data[-1].split())
print(results)
2023-10-09 23:48:08 +03:00
results.sort(key = lambda x: float(x[6]))
2023-09-26 12:58:40 +03:00
results = results[::-1]
self.RESPONSE = "<u>Pong antitop</u>:\n" + "\n".join( [f"{i+1}. {v[3]} {v[4]} ({v[5]}) - {v[6]}" for i, v in enumerate(results[:10])] )
self.FORMAT = "HTML"