asyncio導入
This commit is contained in:
parent
f13d119eb2
commit
6ea299f661
1 changed files with 98 additions and 2 deletions
|
@ -3,7 +3,7 @@ import os
|
|||
import json
|
||||
import psycopg2
|
||||
from psycopg2 import sql
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
import asyncio
|
||||
import string
|
||||
import random
|
||||
|
@ -761,6 +761,92 @@ class ReasonModal(discord.ui.Modal):
|
|||
else:
|
||||
await interaction.response.send_message("# :skull_crossbones: 登録できませんでした。\n内部エラーが発生しています。", ephemeral=True)
|
||||
|
||||
|
||||
class Monitor():
|
||||
def __init__(self, **kwargs) -> None:
|
||||
self.search_frequency = kwargs["search_frequency"]
|
||||
self.allowable_time = kwargs["allowable_time"]
|
||||
self.fstop_time = kwargs["fstop_time"]
|
||||
self.init_wait_time = 10
|
||||
|
||||
async def search(self):
|
||||
try:
|
||||
asyncio.sleep(self.init_wait_time)
|
||||
while True:
|
||||
cursor = dislocker.db.cursor()
|
||||
cursor.execute("SELECT * FROM pc_list WHERE password_hash IS NOT NULL")
|
||||
pc_list = cursor.fetchall()
|
||||
current_datetime = datetime.now()
|
||||
fstop_time = self.fstop_time
|
||||
if current_datetime.time().strftime("%H:%M:%S") == fstop_time:
|
||||
dislocker.log(title=f"[INFO] 定期のPCの使用停止処理を開始します。", flag=0)
|
||||
for i in dislocker.pc_list:
|
||||
stop = dislocker.force_stop(pc_number=i, bot_about="使用停止忘れによるBotによる強制停止。")
|
||||
result = {"result": "FSTOP"}
|
||||
dislocker.log(title=f"[SUCCESS] 定期のPCの使用停止処理は完了しました。", flag=0)
|
||||
else:
|
||||
if pc_list:
|
||||
if len(pc_list) == 1:
|
||||
member_id = pc_list[0][1]
|
||||
cursor.execute("SELECT * FROM pc_usage_history WHERE member_id= %s AND end_use_time IS NULL ORDER BY id DESC LIMIT 1", (member_id,))
|
||||
pc_usage = cursor.fetchall()
|
||||
start_time = pc_usage[0][5]
|
||||
time_difference = current_datetime - start_time
|
||||
dislocker.log(title=f"[INFO] 現在確認されているパスワード未使用のユーザー", message=f"レコード | {str(pc_usage)}, 経過時間(Sec) | {time_difference.seconds}/{timedelta(seconds=self.allowable_time).seconds}", flag=0)
|
||||
if time_difference.seconds >= timedelta(seconds=self.allowable_time).seconds:
|
||||
cursor.execute("SELECT * FROM club_member WHERE member_id = %s", (member_id,))
|
||||
user_info = cursor.fetchall()
|
||||
stop = dislocker.stop(discord_user_id=user_info[0][3], bot_about="パスワードのタイムアウトでBotによる強制停止。")
|
||||
|
||||
#bot.timeout_notify(pc_number=pc_list[0][0], discord_display_name=user_info[0][1])
|
||||
dislocker.log(title=f"[INFO] パスワードのタイムアウト時間に達したため、強制停止されました。", flag=0)
|
||||
result = {"result": "STOP", "details": str(pc_usage)}
|
||||
else:
|
||||
result = {"result": "BUT SAFE", "details": str(pc_usage)}
|
||||
|
||||
|
||||
elif len(pc_list) >= 2:
|
||||
for i in pc_list:
|
||||
member_id = i[1]
|
||||
cursor.execute("SELECT * FROM pc_usage_history WHERE member_id= %s AND end_use_time IS NULL ORDER BY id DESC LIMIT 1", (member_id,))
|
||||
pc_usage = cursor.fetchall()
|
||||
start_time = pc_usage[0][5]
|
||||
time_difference = current_datetime - start_time
|
||||
dislocker.log(title=f"[INFO] 現在確認されているパスワード未使用のユーザー", message=f"レコード | {str(pc_usage)}, 経過時間(Sec) | {time_difference.seconds}/{timedelta(seconds=self.allowable_time).seconds}", flag=0)
|
||||
if time_difference.seconds >= timedelta(seconds=self.allowable_time).seconds:
|
||||
cursor.execute("SELECT * FROM club_member WHERE member_id = %s", (member_id,))
|
||||
user_info = cursor.fetchall()
|
||||
stop = dislocker.stop(discord_user_id=user_info[0][3], bot_about="タイムアウトでBotによる強制停止。")
|
||||
|
||||
#bot.timeout_notify(pc_number=i[0], discord_display_name=user_info[0][1])
|
||||
dislocker.log(title=f"[INFO] パスワードのタイムアウト時間に達したため、強制停止されました。", flag=0)
|
||||
result = {"result": "STOP", "details": str(pc_usage)}
|
||||
else:
|
||||
result = {"result": "BUT SAFE", "details": str(pc_usage)}
|
||||
|
||||
else:
|
||||
result = {"result": "NONE"}
|
||||
else:
|
||||
result = {"result": "NONE"}
|
||||
|
||||
if result["result"] == "NONE":
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
asyncio.sleep(self.search_frequency)
|
||||
|
||||
|
||||
except Exception as error:
|
||||
dislocker.log(title=f"[ERROR] 自動停止処理中にエラーが発生しました。 {str(error.__class__.__name__)}", message=str(error.args), flag=1)
|
||||
result = {"result": "error"}
|
||||
dislocker.db.rollback()
|
||||
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
return result
|
||||
|
||||
|
||||
dislocker = DL()
|
||||
|
||||
intents = discord.Intents.default()
|
||||
|
@ -1064,5 +1150,15 @@ async def button_init(interaction: discord.Interaction, text_channel: discord.Te
|
|||
await interaction.response.send_message(f":white_check_mark: ボタンを送信しました!", ephemeral=True)
|
||||
|
||||
|
||||
async def run():
|
||||
print("Botを起動します...")
|
||||
monitor = Monitor(search_frequency=dislocker.server_config["bot"]["monitor"]["search_frequency"], allowable_time=dislocker.server_config["bot"]["monitor"]["allowable_time"], fstop_time=dislocker.server_config["bot"]["monitor"]["fstop_time"])
|
||||
monitor_task = asyncio.create_task(monitor.start())
|
||||
client_task = asyncio.create_task(client.run(dislocker.server_config["bot"]["token"]))
|
||||
await asyncio.gather(monitor_task, client_task)
|
||||
|
||||
client.run(dislocker.server_config["bot"]["token"])
|
||||
|
||||
if dislocker.init_result == "ok":
|
||||
asyncio.run(run())
|
||||
else:
|
||||
pass
|
Loading…
Reference in a new issue