使用登録の強制停止を実装

This commit is contained in:
suti7yk5032 2024-07-07 16:35:57 +09:00
parent 18970c9079
commit 57f428e910

View file

@ -190,7 +190,7 @@ class Bot(discord.Client):
csv_file_path = self.export_dir_path + "pc_usage_history.csv"
cursor = dislocker.db.cursor()
# メインテーブルの列情報を取得user_idを除く
cur.execute(sql.SQL("SELECT * FROM {} LIMIT 0").format(sql.Identifier(main_table)))
cursor.execute(sql.SQL("SELECT * FROM {} LIMIT 0").format(sql.Identifier(main_table)))
main_columns = [desc[0] for desc in cur.description if desc[0] != 'member_id']
# クエリを作成(列名を明確に指定)
@ -204,12 +204,12 @@ class Bot(discord.Client):
related_table=sql.Identifier(related_table)
)
cur.execute(query)
cursor.execute(query)
# 列名を再構成nameを2番目に配置
column_names = [main_columns[0], 'name'] + main_columns[1:]
rows = cur.fetchall()
rows = cursor.fetchall()
with open(csv_file_path, 'w', newline='', encoding='utf-8-sig') as csvfile:
csvwriter = csv.writer(csvfile)
@ -231,6 +231,33 @@ class Bot(discord.Client):
cursor.close()
return result
def force_stop(self, **kwargs):
try:
pc_number = kwargs["pc_number"]
cursor = dislocker.db.cursor()
cursor.execute("SELECT * FROM pc_list WHERE pc_number = %s", (pc_number,))
pc_list_record = cursor.fetchall()
if not pc_list_record[0][1] == None:
cursor.execute("UPDATE pc_list SET using_user_id = 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))
pc_usage_history_record = cursor.fetchall()
cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (pc_usage_history_record[0][0],))
dislocker.db.commit()
result = {"result": "ok"}
else:
result = {"result": "not_used"}
except:
result = {"result": "error"}
finally:
cursor.close()
async def on_ready(self):
print("DiscordのBotが起動しました。")
@ -260,7 +287,7 @@ class Bot(discord.Client):
await message.channel.send(f"使用が開始されました。\nパスワード | {register["password"]}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}")
elif len(msg_split) == 4:
await message.channel.send(f"使用が開始されました。\nパスワード | {register["password"]}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n使用目的 | {msg_split[3]}")
await self.get_channel(dislocker.server.config["bot"]["log_channel_id"]).send(f'{register["name"]} さんがPC {msg_split[1]} を使用しています')
await self.get_channel(dislocker.server_config["bot"]["log_channel_id"]).send(f'{register["name"]} さんがPC {msg_split[1]} を使用しています')
elif register["result"] == "user_data_not_found":
await message.channel.send("ユーザーとして登録されていないようです。管理者に問い合わせてください。")
elif register["result"] == "pc_already_in_use_by_you":
@ -278,9 +305,9 @@ class Bot(discord.Client):
await message.channel.send("使用されていないようです...")
elif stop["result"] == "ok":
await message.channel.send(f"PC番号 {stop["pc_number"]} の使用が終了されました。")
await self.get_channel(dislocker.server.config["bot"]["log_channel_id"]).send(f'{stop["name"]} さんがPC {stop["pc_number"]} の使用を終了しました')
await self.get_channel(dislocker.server_config["bot"]["log_channel_id"]).send(f'{stop["name"]} さんがPC {stop["pc_number"]} の使用を終了しました')
elif message.channel.id == dislocker.server.config["bot"]["config_channel_id"]:
elif message.channel.id == dislocker.server_config["bot"]["config_channel_id"]:
msg_split = message.content.split()
if msg_split[0] == "/register":
if len(msg_split) <= 3:
@ -305,6 +332,21 @@ class Bot(discord.Client):
elif export["result"] == "export_error":
await message.channel.send("エクスポートに失敗しました。")
elif msg_split[0] == "/fstop":
if len(msg_split) == 1:
await message.channel.send("PC番号を指定してください。")
elif len(msg_split) == 2:
fstop = self.force_stop(msg_split[1])
if fstop["result"] == "ok":
await message.channel.send(f"PC番号 {msg_split[1]} の使用登録を解除しました。")
elif fstop["result"] == "not_used":
await message.channel.send("PCは使用されていないようです...")
else:
await message.channel.send("エラーが発生しました。")
else:
await message.channel.send("引数が多すぎます。")
dislocker = DL()
if dislocker.init_result == "ok":
intents = discord.Intents.default()