diff --git a/dislocker.py b/dislocker.py index fba9635..b691495 100644 --- a/dislocker.py +++ b/dislocker.py @@ -212,17 +212,22 @@ class Bot(discord.Client): cursor = dislocker.db.cursor() cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (discord_user_id,)) user_record = cursor.fetchall() - cursor.execute("SELECT * FROM pc_usage_history WHERE member_id= %s ORDER BY id DESC LIMIT 1", (user_record[0][0],)) - pc_usage_history_record = cursor.fetchall() - if pc_usage_history_record: - if not pc_usage_history_record[0][5] == None: - result = {"result": "unused"} + if user_record: + cursor.execute("SELECT * FROM pc_usage_history WHERE member_id= %s ORDER BY id DESC LIMIT 1", (user_record[0][0],)) + pc_usage_history_record = cursor.fetchall() + if pc_usage_history_record: + if not pc_usage_history_record[0][5] == None: + result = {"result": "unused"} + else: + cursor.execute("UPDATE pc_usage_history SET end_use_time = current_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],)) + dislocker.db.commit() + result = {"result": "ok", "pc_number": str(pc_usage_history_record[0][2]), "name": str(user_record[0][1])} else: - cursor.execute("UPDATE pc_usage_history SET end_use_time = current_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],)) - dislocker.db.commit() - result = {"result": "ok", "pc_number": str(pc_usage_history_record[0][2]), "name": str(user_record[0][1])} + result = {"result": "unused"} + else: + result = {"result": "user_data_not_found"} except: print("停止処理にエラーが発生しました。") result = {"result": "error"} @@ -615,6 +620,8 @@ class Bot(discord.Client): stop_view = View(timeout=15) if pc_stop["result"] == "unused": await interaction.response.send_message("# :shaking_face: 使用されていないようです...", ephemeral=True) + elif pc_stop["result"] == "user_data_not_found": + await interaction.response.send_message("# :dizzy_face: ユーザーとして登録されていないようです。\n最初にサーバーで登録を行ってください。", ephemeral=True) elif pc_stop["result"] == "ok": await interaction.response.send_message(f":white_check_mark: PC番号 {pc_stop["pc_number"]} の使用が終了されました。", ephemeral=True) await self.get_channel(dislocker.server_config["bot"]["log_channel_id"]).send(f':negative_squared_cross_mark: {pc_stop["name"]} さんがPC {pc_stop["pc_number"]} の使用を終了しました。') @@ -632,17 +639,15 @@ class Bot(discord.Client): await interaction.response.send_message("# :no_entry: 登録できませんでした。\n内部エラーが発生しています。", ephemeral=True) class Monitor(): - def __init__(self) -> None: - pass - - def start(self, **kwargs): + def __init__(self, **kwargs) -> None: self.serach_time = kwargs["search_time"] self.allowable_time = kwargs["allowable_time"] + + def start(self, **kwargs): serach_thread = threading.Thread(target=self.search) serach_thread.start() - - - + + def search(self): try: while True: @@ -708,6 +713,7 @@ class Monitor(): print(result["result"]) time.sleep(self.serach_time) + except Exception as error: print("自動停止処理中にエラーが発生しました。\nエラー内容") print(str(error.__class__.__name__)) @@ -760,8 +766,8 @@ if dislocker.init_result == "ok": intents = discord.Intents.default() intents.message_content = True bot = Bot(intents=intents) - monitor = Monitor() - monitor.start(search_time=10, allowable_time=300) + monitor = Monitor(search_time=1, allowable_time=300) + monitor.start() bot.run(dislocker.server_config['bot']['token']) else: pass \ No newline at end of file