fstopに理由を要求するように

使用停止忘れのための停止処理を追加
This commit is contained in:
suti7yk5032 2024-08-11 15:25:01 +09:00
parent 3aad819485
commit b5fc778715

View file

@ -68,6 +68,8 @@ class DL():
self.db = psycopg2.connect(f"host={self.server_config['db']['host']} dbname={self.server_config['db']['db_name']} port={self.server_config['db']['port']} user={self.server_config['db']['username']} password={self.server_config['db']['password']}") self.db = psycopg2.connect(f"host={self.server_config['db']['host']} dbname={self.server_config['db']['db_name']} port={self.server_config['db']['port']} user={self.server_config['db']['username']} password={self.server_config['db']['password']}")
cursor = self.db.cursor() cursor = self.db.cursor()
self.pc_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
cursor.execute("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'club_member')") cursor.execute("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'club_member')")
find_club_member_table = cursor.fetchall() find_club_member_table = cursor.fetchall()
print(find_club_member_table) print(find_club_member_table)
@ -89,7 +91,7 @@ class DL():
find_pc_usage_history_table = cursor.fetchall() find_pc_usage_history_table = cursor.fetchall()
print(find_pc_usage_history_table) print(find_pc_usage_history_table)
if find_pc_usage_history_table[0][0] == False: if find_pc_usage_history_table[0][0] == False:
cursor.execute("CREATE TABLE pc_usage_history (id SERIAL NOT NULL, member_id INTEGER NOT NULL, pc_number INTEGER NOT NULL, device_number INTEGER NOT NULL, start_use_time TIMESTAMP NOT NULL, end_use_time TIMESTAMP, use_detail VARCHAR(30), PRIMARY KEY (id), FOREIGN KEY (member_id) REFERENCES club_member(id), FOREIGN KEY (pc_number) REFERENCES pc_list(pc_number))") cursor.execute("CREATE TABLE pc_usage_history (id SERIAL NOT NULL, member_id INTEGER NOT NULL, pc_number INTEGER NOT NULL, device_number INTEGER NOT NULL, start_use_time TIMESTAMP NOT NULL, end_use_time TIMESTAMP, use_detail VARCHAR(30), bot_about VARCHAR(30), PRIMARY KEY (id), FOREIGN KEY (member_id) REFERENCES club_member(id), FOREIGN KEY (pc_number) REFERENCES pc_list(pc_number))")
self.db.commit() self.db.commit()
cursor.close() cursor.close()
@ -209,6 +211,10 @@ class Bot(discord.Client):
def stop(self, **kwargs): def stop(self, **kwargs):
try: try:
discord_user_id = str(kwargs["user_id"]) discord_user_id = str(kwargs["user_id"])
if "bot_about" in kwargs:
bot_about = kwargs["bot_about"]
else:
bot_about = None
cursor = dislocker.db.cursor() cursor = dislocker.db.cursor()
cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (discord_user_id,)) cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (discord_user_id,))
user_record = cursor.fetchall() user_record = cursor.fetchall()
@ -219,9 +225,12 @@ class Bot(discord.Client):
if not pc_usage_history_record[0][5] == None: if not pc_usage_history_record[0][5] == None:
result = {"result": "unused"} result = {"result": "unused"}
else: else:
cursor.execute("UPDATE pc_usage_history SET end_use_time = clock_timestamp() WHERE id = %s", (pc_usage_history_record[0][0],)) if not bot_about == None:
cursor.execute("UPDATE pc_list SET using_user_id = NULL WHERE pc_number = %s", (pc_usage_history_record[0][2],)) cursor.execute("UPDATE pc_usage_history SET end_use_time = clock_timestamp(), bot_about = %s WHERE id = %s", (bot_about, pc_usage_history_record[0][0]))
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (pc_usage_history_record[0][2],)) else:
cursor.execute("UPDATE pc_usage_history SET end_use_time = clock_timestamp() WHERE id = %s", (pc_usage_history_record[0][0],))
cursor.execute("UPDATE pc_list SET using_user_id = NULL, password_hash = NULL WHERE pc_number = %s", (pc_usage_history_record[0][2],))
dislocker.db.commit() dislocker.db.commit()
result = {"result": "ok", "pc_number": str(pc_usage_history_record[0][2]), "name": str(user_record[0][1])} result = {"result": "ok", "pc_number": str(pc_usage_history_record[0][2]), "name": str(user_record[0][1])}
else: else:
@ -365,23 +374,29 @@ class Bot(discord.Client):
def force_stop(self, **kwargs): def force_stop(self, **kwargs):
try: try:
pc_number = kwargs["pc_number"] pc_number = kwargs["pc_number"]
cursor = dislocker.db.cursor() if "bot_about" in kwargs:
cursor.execute("SELECT * FROM pc_list WHERE pc_number = %s", (pc_number,)) bot_about = kwargs["bot_about"]
pc_list_record = cursor.fetchall() cursor = dislocker.db.cursor()
if not pc_list_record[0][1] == None: cursor.execute("SELECT * FROM pc_list WHERE pc_number = %s", (pc_number,))
cursor.execute("UPDATE pc_list SET using_user_id = NULL WHERE pc_number = %s", (pc_number,)) pc_list_record = cursor.fetchall()
if not pc_list_record[0][1] == None:
if not pc_list_record[0][2] == None: cursor.execute("UPDATE pc_list SET using_user_id = NULL WHERE pc_number = %s", (pc_number,))
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (pc_number,))
if not pc_list_record[0][2] == None:
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (pc_number,))
cursor.execute("SELECT * FROM pc_usage_history WHERE member_id = %s AND pc_number = %s ORDER BY id DESC LIMIT 1", (pc_list_record[0][1], pc_number)) cursor.execute("SELECT * FROM pc_usage_history WHERE member_id = %s AND pc_number = %s ORDER BY id DESC LIMIT 1", (pc_list_record[0][1], pc_number))
pc_usage_history_record = cursor.fetchall() pc_usage_history_record = cursor.fetchall()
cursor.execute("UPDATE pc_usage_history SET end_use_time = clock_timestamp() WHERE id = %s", (pc_usage_history_record[0][0],)) cursor.execute("UPDATE pc_usage_history SET end_use_time = clock_timestamp(), bot_about = %s WHERE id = %s", (bot_about, pc_usage_history_record[0][0]))
dislocker.db.commit() dislocker.db.commit()
result = {"result": "ok"} result = {"result": "ok"}
else:
result = {"result": "not_used"}
else: else:
result = {"result": "not_used"} bot_about = None
result = {"result": "bot_about_not_found"}
except: except:
result = {"result": "error"} result = {"result": "error"}
@ -514,7 +529,7 @@ class Bot(discord.Client):
await message.channel.send("# :warning: 登録を解除できませんでした。\n使用を停止したいPC番号を指定してください。\n-# /fstop PC番号") await message.channel.send("# :warning: 登録を解除できませんでした。\n使用を停止したいPC番号を指定してください。\n-# /fstop PC番号")
elif len(msg_split) == 2: elif len(msg_split) == 2:
if msg_split[1].isdigit(): if msg_split[1].isdigit():
fstop = self.force_stop(pc_number=msg_split[1]) fstop = self.force_stop(pc_number=msg_split[1], bot_about="管理者による強制停止。")
if fstop["result"] == "ok": if fstop["result"] == "ok":
await message.channel.send(f"# :white_check_mark: PC番号 {msg_split[1]} の使用登録を解除しました。") await message.channel.send(f"# :white_check_mark: PC番号 {msg_split[1]} の使用登録を解除しました。")
elif fstop["result"] == "not_used": elif fstop["result"] == "not_used":
@ -656,61 +671,64 @@ class Monitor():
cursor = dislocker.db.cursor() cursor = dislocker.db.cursor()
cursor.execute("SELECT * FROM pc_list WHERE password_hash IS NOT NULL") cursor.execute("SELECT * FROM pc_list WHERE password_hash IS NOT NULL")
pc_list = cursor.fetchall() pc_list = cursor.fetchall()
current_datetime = datetime.now()
fstop_time = "21:00:00"
print(pc_list) print(pc_list)
print(len(pc_list)) print(len(pc_list))
if pc_list: if current_datetime.time().strftime("%H:%M:%S") == fstop_time:
if len(pc_list) == 1: for i in dislocker.pc_list:
user_id = pc_list[0][1] stop = bot.force_stop(pc_number=i, bot_about="使用停止忘れによるBotによる強制停止。")
cursor.execute("SELECT * FROM pc_usage_history WHERE member_id= %s AND end_use_time IS NULL ORDER BY id DESC LIMIT 1", (user_id,)) result = {"result": "FSTOP"}
pc_usage = cursor.fetchall() else:
print(pc_usage) if pc_list:
start_time = pc_usage[0][4] if len(pc_list) == 1:
print(start_time) user_id = pc_list[0][1]
print(type(start_time))
current_time = datetime.now()
time_difference = current_time - start_time
print(current_time, start_time)
print(time_difference.seconds, timedelta(seconds=self.allowable_time).seconds)
if time_difference.seconds >= timedelta(seconds=self.allowable_time).seconds:
cursor.execute("SELECT * FROM club_member WHERE id = %s", (user_id,))
user_info = cursor.fetchall()
stop = bot.stop(user_id=user_info[0][3])
bot.timeout_notify(pc_number=pc_list[0][0], discord_display_name=user_info[0][1])
result = {"result": "STOP"}
else:
result = {"result": "BUT SAFE"}
elif len(pc_list) >= 2:
for i in pc_list:
print(i)
user_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", (user_id,)) cursor.execute("SELECT * FROM pc_usage_history WHERE member_id= %s AND end_use_time IS NULL ORDER BY id DESC LIMIT 1", (user_id,))
pc_usage = cursor.fetchall() pc_usage = cursor.fetchall()
print(pc_usage) print(pc_usage)
start_time = pc_usage[0][4] start_time = pc_usage[0][4]
print(start_time) print(start_time)
print(type(start_time)) print(type(start_time))
current_time = datetime.now() time_difference = current_datetime - start_time
time_difference = current_time - start_time print(current_datetime, start_time)
print(time_difference.seconds, timedelta(seconds=self.allowable_time).seconds) print(time_difference.seconds, timedelta(seconds=self.allowable_time).seconds)
if time_difference.seconds >= timedelta(seconds=self.allowable_time).seconds: if time_difference.seconds >= timedelta(seconds=self.allowable_time).seconds:
cursor.execute("SELECT * FROM club_member WHERE id = %s", (user_id,)) cursor.execute("SELECT * FROM club_member WHERE id = %s", (user_id,))
user_info = cursor.fetchall() user_info = cursor.fetchall()
stop = bot.stop(user_id=user_info[0][3]) stop = bot.stop(user_id=user_info[0][3], bot_about="パスワードのタイムアウトでBotによる強制停止。")
bot.timeout_notify(pc_number=i[0], discord_display_name=user_info[0][1]) bot.timeout_notify(pc_number=pc_list[0][0], discord_display_name=user_info[0][1])
result = {"result": "STOP"} result = {"result": "STOP"}
else: else:
result = {"result": "BUT SAFE"} result = {"result": "BUT SAFE"}
elif len(pc_list) >= 2:
for i in pc_list:
print(i)
user_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", (user_id,))
pc_usage = cursor.fetchall()
print(pc_usage)
start_time = pc_usage[0][4]
print(start_time)
print(type(start_time))
time_difference = current_datetime - start_time
print(time_difference.seconds, timedelta(seconds=self.allowable_time).seconds)
if time_difference.seconds >= timedelta(seconds=self.allowable_time).seconds:
cursor.execute("SELECT * FROM club_member WHERE id = %s", (user_id,))
user_info = cursor.fetchall()
stop = bot.stop(user_id=user_info[0][3], bot_about="タイムアウトでBotによる強制停止。")
bot.timeout_notify(pc_number=i[0], discord_display_name=user_info[0][1])
result = {"result": "STOP"}
else:
result = {"result": "BUT SAFE"}
else:
result = {"result": "NONE"}
else: else:
result = {"result": "NONE"} result = {"result": "NONE"}
else:
result = {"result": "NONE"}
cursor.close()
print(result["result"]) print(result["result"])
time.sleep(self.serach_time) time.sleep(self.serach_time)