fstopに理由を要求するように
使用停止忘れのための停止処理を追加
This commit is contained in:
parent
3aad819485
commit
b5fc778715
1 changed files with 73 additions and 55 deletions
46
dislocker.py
46
dislocker.py
|
@ -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()
|
||||||
|
@ -218,10 +224,13 @@ class Bot(discord.Client):
|
||||||
if pc_usage_history_record:
|
if pc_usage_history_record:
|
||||||
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:
|
||||||
|
if not bot_about == None:
|
||||||
|
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]))
|
||||||
else:
|
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_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 WHERE pc_number = %s", (pc_usage_history_record[0][2],))
|
|
||||||
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (pc_usage_history_record[0][2],))
|
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,6 +374,8 @@ 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"]
|
||||||
|
if "bot_about" in kwargs:
|
||||||
|
bot_about = kwargs["bot_about"]
|
||||||
cursor = dislocker.db.cursor()
|
cursor = dislocker.db.cursor()
|
||||||
cursor.execute("SELECT * FROM pc_list WHERE pc_number = %s", (pc_number,))
|
cursor.execute("SELECT * FROM pc_list WHERE pc_number = %s", (pc_number,))
|
||||||
pc_list_record = cursor.fetchall()
|
pc_list_record = cursor.fetchall()
|
||||||
|
@ -376,12 +387,16 @@ class Bot(discord.Client):
|
||||||
|
|
||||||
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:
|
else:
|
||||||
result = {"result": "not_used"}
|
result = {"result": "not_used"}
|
||||||
|
else:
|
||||||
|
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,8 +671,15 @@ 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 current_datetime.time().strftime("%H:%M:%S") == fstop_time:
|
||||||
|
for i in dislocker.pc_list:
|
||||||
|
stop = bot.force_stop(pc_number=i, bot_about="使用停止忘れによるBotによる強制停止。")
|
||||||
|
result = {"result": "FSTOP"}
|
||||||
|
else:
|
||||||
if pc_list:
|
if pc_list:
|
||||||
if len(pc_list) == 1:
|
if len(pc_list) == 1:
|
||||||
user_id = pc_list[0][1]
|
user_id = pc_list[0][1]
|
||||||
|
@ -667,14 +689,13 @@ class Monitor():
|
||||||
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(current_time, 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=pc_list[0][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"}
|
||||||
|
@ -692,13 +713,12 @@ class Monitor():
|
||||||
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(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=i[0], discord_display_name=user_info[0][1])
|
||||||
result = {"result": "STOP"}
|
result = {"result": "STOP"}
|
||||||
|
@ -710,8 +730,6 @@ class Monitor():
|
||||||
else:
|
else:
|
||||||
result = {"result": "NONE"}
|
result = {"result": "NONE"}
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
print(result["result"])
|
print(result["result"])
|
||||||
time.sleep(self.serach_time)
|
time.sleep(self.serach_time)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue