Compare commits
2 Commits
b5a37acecb
...
f591c8d54a
Author | SHA1 | Date |
---|---|---|
dymik739 | f591c8d54a | |
dymik739 | 48e672a7b0 |
71
main.py
71
main.py
|
@ -11,7 +11,9 @@ import traceback
|
|||
|
||||
# global variables
|
||||
STOP_REQUESTED = False
|
||||
|
||||
DELAY_AFTER_RESPONSE = 3
|
||||
DELAY_AFTER_MESSAGE = 0.1
|
||||
DELAY_AFTER_IDLE = 1.0
|
||||
|
||||
# some functions that increase readability of the code
|
||||
def readfile(filename):
|
||||
|
@ -59,7 +61,7 @@ class ModuleV1:
|
|||
return self.RESPONSE, self.FORMAT
|
||||
except Exception as e:
|
||||
print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"")
|
||||
print(f"[ERROR] module v1: traceback:\ntraceback.format_exc()")
|
||||
print(f"[ERROR] module v1: traceback:\n{traceback.format_exc()}")
|
||||
return "", None
|
||||
|
||||
|
||||
|
@ -166,12 +168,13 @@ def queue_processor():
|
|||
try:
|
||||
if len(message_queue) > 0:
|
||||
msg = message_queue[0]
|
||||
del message_queue[0]
|
||||
print("[DEBUG] queue_processor: {}".format(msg)) # debug
|
||||
|
||||
# check for control commands
|
||||
if msg["chat"]["id"] == 575246355:
|
||||
if msg.chat.id == 575246355:
|
||||
if msg["text"][0] == "$":
|
||||
command = msg["text"][1:].split(" ")
|
||||
command = msg["text"][1:].split()
|
||||
|
||||
if len(command) >= 2 and command[0] == "module":
|
||||
if command[1] == "reload":
|
||||
|
@ -185,7 +188,49 @@ def queue_processor():
|
|||
del mcu.modules[:]
|
||||
mcu.reload_modules()
|
||||
|
||||
del message_queue[0]
|
||||
elif (2 <= len(command) <= 3) and command[0] == "delay":
|
||||
l = len(command)
|
||||
|
||||
if command[1] == "response":
|
||||
if l == 3:
|
||||
try:
|
||||
new_value = float(command[2])
|
||||
global DELAY_AFTER_RESPONSE
|
||||
DELAY_AFTER_RESPONSE = new_value
|
||||
updater.bot.send_message(msg.chat.id, f"Set DELAY_AFTER_RESPONSE to {command[2]}")
|
||||
except:
|
||||
print(f"[WARN]: Cannot set DELAY_AFTER_RESPONSE to non-float value of {command[2]}")
|
||||
updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_RESPONSE to non-float value of {command[2]}")
|
||||
elif l == 2:
|
||||
print(f"[INFO]: DELAY_AFTER_RESPONSE = {DELAY_AFTER_RESPONSE}")
|
||||
updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_RESPONSE = {DELAY_AFTER_RESPONSE}")
|
||||
|
||||
elif command[1] == "message":
|
||||
if l == 3:
|
||||
try:
|
||||
new_value = float(command[2])
|
||||
global DELAY_AFTER_MESSAGE
|
||||
DELAY_AFTER_MESSAGE = new_value
|
||||
updater.bot.send_message(msg.chat.id, f"Set DELAY_AFTER_MESSAGE to {command[2]}")
|
||||
except:
|
||||
print("[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}")
|
||||
updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}")
|
||||
elif l == 2:
|
||||
print(f"[INFO]: DELAY_AFTER_MESSAGE = {DELAY_AFTER_MESSAGE}")
|
||||
updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_MESSAGE = {DELAY_AFTER_MESSAGE}")
|
||||
|
||||
elif command[1] == "idle":
|
||||
if l == 3:
|
||||
try:
|
||||
new_value = float(command[2])
|
||||
global DELAY_AFTER_IDLE
|
||||
DELAY_AFTER_IDLE = new_value
|
||||
except:
|
||||
print("[WARN]: Cannot set DELAY_AFTER_IDLE to non-float value of {command[2]}")
|
||||
updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}")
|
||||
elif l == 2:
|
||||
print(f"[INFO]: DELAY_AFTER_IDLE = {DELAY_AFTER_IDLE}")
|
||||
updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_IDLE = {DELAY_AFTER_IDLE}")
|
||||
continue
|
||||
|
||||
# modules are used in here
|
||||
|
@ -199,6 +244,7 @@ def queue_processor():
|
|||
updater.bot.send_message(chat_id=msg.chat.id, text=response,
|
||||
disable_web_page_preview=True)
|
||||
print(f"Responded using module {mod.path} ({mod.alias}) with text: {response}")
|
||||
time.sleep(DELAY_AFTER_RESPONSE)
|
||||
break
|
||||
|
||||
elif formatting in ["Markdown", "MarkdownV2", "HTML"]:
|
||||
|
@ -206,25 +252,20 @@ def queue_processor():
|
|||
disable_web_page_preview=True,
|
||||
parse_mode=formatting)
|
||||
print(f"Responded using module {mod.path} ({mod.alias}) with text (using {formatting}): {response}")
|
||||
time.sleep(DELAY_AFTER_RESPONSE)
|
||||
break
|
||||
|
||||
del message_queue[0]
|
||||
|
||||
time.sleep(0.1)
|
||||
time.sleep(DELAY_AFTER_MESSAGE)
|
||||
else:
|
||||
if STOP_REQUESTED:
|
||||
break
|
||||
else:
|
||||
time.sleep(1)
|
||||
time.sleep(DELAY_AFTER_IDLE)
|
||||
except Exception as e:
|
||||
print(f"[ERROR] queue_processor: current message queue: {message_queue}")
|
||||
print(f"[ERROR] Traceback:\n{traceback.format_exc()}")
|
||||
print(f"[ERROR] queue_processor: error while processing message ({e}), trying to delete it...")
|
||||
try:
|
||||
del message_queue[0]
|
||||
print("[INFO] queue_processor: deleted broken message from the queue")
|
||||
except:
|
||||
print("[WARN] queue_processor: message seems absent, whatever")
|
||||
print(f"[ERROR] queue_processor: error while processing message ({e})...")
|
||||
print("[INFO] queue_processor: skipped broken message")
|
||||
|
||||
print("[INFO] queue_processor thread stops successfully")
|
||||
|
||||
|
|
|
@ -1,4 +1,127 @@
|
|||
text = self.MESSAGE.text
|
||||
if self.MESSAGE.text == "!ping":
|
||||
current_time = time.time()
|
||||
delay = current_time - 7200 - float(self.MESSAGE.date.strftime('+%s'))
|
||||
self.RESPONSE = f"Pong in {delay} seconds"
|
||||
|
||||
if text == "!ping":
|
||||
self.RESPONSE = f"Pong in {time.time() - 7200 - float(self.MESSAGE.date.strftime('+%s'))} seconds"
|
||||
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")
|
||||
|
||||
elif self.MESSAGE.text == "!pong":
|
||||
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")
|
||||
|
||||
elif self.MESSAGE.text == "!pingtop":
|
||||
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)
|
||||
|
||||
results.sort(key = lambda x: x[6])
|
||||
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"
|
||||
|
||||
elif self.MESSAGE.text == "!pongtop":
|
||||
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)
|
||||
|
||||
results.sort(key = lambda x: x[6])
|
||||
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"
|
||||
|
||||
elif self.MESSAGE.text == "!pingantitop":
|
||||
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)
|
||||
|
||||
results.sort(key = lambda x: x[6])
|
||||
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"
|
||||
|
||||
elif self.MESSAGE.text == "!pongantitop":
|
||||
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)
|
||||
|
||||
results.sort(key = lambda x: x[6])
|
||||
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"
|
||||
|
|
Loading…
Reference in New Issue