From b01ce7b3738aab207dd3242777a820eef3e52d41 Mon Sep 17 00:00:00 2001 From: suti7yk5032 Date: Wed, 5 Jun 2024 16:13:57 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=84=E3=82=89=E3=81=AA=E3=81=84=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4=E3=80=81config?= =?UTF-8?q?=E3=81=AB=E3=83=AD=E3=82=B0=E9=80=81=E4=BF=A1=E5=85=88=E3=83=81?= =?UTF-8?q?=E3=83=A3=E3=83=B3=E3=83=8D=E3=83=AB=E3=81=AEID=E3=82=92?= =?UTF-8?q?=E8=A8=98=E8=BC=89=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dislocker.py | 75 +++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 48 deletions(-) diff --git a/dislocker.py b/dislocker.py index 739518b..c0494ed 100644 --- a/dislocker.py +++ b/dislocker.py @@ -4,43 +4,6 @@ import os import psycopg2 import datetime -class Database(): - def __init__(self): - if not os.path.isfile("./db/dislocker.db"): - initial = 1 - else: - initial = 0 - self.db = sqlite3.connect("./db/dislocker.db") - if initial == 1: - self.initial() - - def initial(self): - cursor = self.db.cursor() - initial_sql_web_auth = "CREATE TABLE web_auth (id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(10) NOT NULL, password VARCHAR(32) NOT NULL);" - cursor.execute(initial_sql_web_auth) - initial_sql_discord_user = "CREATE TABLE discord_user (id INTEGER PRIMARY KEY AUTOINCREMENT, discord_username VARCHAR(32) NOT NULL, discord_userid VARCHAR(18) NOT NULL);" - cursor.execute(initial_sql_discord_user) - initial_sql_insert_web_admin = "INSERT INTO discord_user (username, password) VALUES ('admin', 'admin');" - cursor.execute(initial_sql_insert_web_admin) - self.db.commit() - - def register(self, username, userid): - cursor = self.db.cursor() - insert_sql = "INSERT INTO user_list (discord_username, discord_userid) VALUES (?, ?);" - cursor.execute(insert_sql, (username, userid)) - self.db.commit() - - def get_username(self, username, password): - cursor = self.db.cursor() - get_sql = "SELECT * FROM web_auth WHERE username = ? AND password = ? " - cursor.execute(get_sql, (username, password)) - result = cursor.fetchall() - print(result) - if result == []: - return 1 - elif result[0][1] == username and result[0][2] == password: - return 0 - class Bot(discord.Client): def db_connect(self, host, db, port, user, password): self.db = psycopg2.connect(f"host={host} dbname={db} port={port} user={user} password={password}") @@ -66,9 +29,15 @@ class Bot(discord.Client): cursor = self.db.cursor() cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),)) member_info = cursor.fetchall() - cursor.execute("INSERT INTO pc_usage_history ( member_id, pc_number, device_number, start_use_time ) VALUES ( %s, %s, %s, current_timestamp)", (member_info[0][0], int(msg_split[1]), int(msg_split[2]))) - self.db.commit() - await message.channel.send(f"使用が開始されました。\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") + cursor.execute("SELECT * FROM pc_usage_history WHERE member_id=%s ORDER BY id DESC LIMIT 1", (member_info[0][0],)) + history_info = cursor.fetchall() + if not history_info[0][5]: + await message.channel.send(f"もう使用されているようです。解除するには /stop で使用終了をお知らせください。\n使用中のPC番号 | {str(history_info[0][2])}\n使用中のデバイス番号 | {str(history_info[0][3])}\n使用開始時刻 | {str(history_info[0][4])}\n使用目的 | {str(history_info[0][6])}") + else: + cursor.execute("INSERT INTO pc_usage_history ( member_id, pc_number, device_number, start_use_time ) VALUES ( %s, %s, %s, current_timestamp)", (member_info[0][0], int(msg_split[1]), int(msg_split[2]))) + self.db.commit() + await message.channel.send(f"使用が開始されました。\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") + await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{msg_split[1]}を使用しています') else: await message.channel.send("パソコンの台数が\n# だめです") else: @@ -80,9 +49,15 @@ class Bot(discord.Client): cursor = self.db.cursor() cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),)) member_info = cursor.fetchall() - cursor.execute("INSERT INTO pc_usage_history ( member_id, pc_number, device_number, start_use_time, use_detail ) VALUES ( %s, %s, %s, current_timestamp, %s)", (member_info[0][0], int(msg_split[1]), int(msg_split[2]), str(msg_split[3]))) - self.db.commit() - await message.channel.send(f"使用が開始されました。\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") + cursor.execute("SELECT * FROM pc_usage_history WHERE member_id=%s ORDER BY id DESC LIMIT 1", (member_info[0][0],)) + history_info = cursor.fetchall() + if not history_info[0][5]: + await message.channel.send(f"もう使用されているようです。解除するには /stop で使用終了をお知らせください。\n使用中のPC番号 | {str(history_info[0][2])}\n使用中のデバイス番号 | {str(history_info[0][3])}\n使用開始時刻 | {str(history_info[0][4])}\n使用目的 | {str(history_info[0][6])}") + else: + cursor.execute("INSERT INTO pc_usage_history ( member_id, pc_number, device_number, start_use_time, use_detail ) VALUES ( %s, %s, %s, current_timestamp, %s)", (member_info[0][0], int(msg_split[1]), int(msg_split[2]), str(msg_split[3]))) + self.db.commit() + await message.channel.send(f"使用が開始されました。\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") + await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{msg_split[1]}を使用しています') else: await message.channel.send("パソコンの台数が\n# だめです") else: @@ -95,16 +70,19 @@ class Bot(discord.Client): member_info = cursor.fetchall() cursor.execute("SELECT * FROM pc_usage_history WHERE member_id=%s ORDER BY id DESC LIMIT 1", (member_info[0][0],)) history_info = cursor.fetchall() - cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (history_info[0][0],)) - self.db.commit() - - await message.channel.send(f"使用が終了されました。") + if history_info[0][5]: + await message.channel.send("使用されていないようです...") + else: + cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (history_info[0][0],)) + self.db.commit() + await message.channel.send(f"使用が終了されました。") + await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{history_info[0][2]}の使用を終了しました') else: if message.content == "/hello": await message.channel.send("こんにちは!!!!") - + else: await message.channel.send("どうしましたか?") @@ -136,6 +114,7 @@ if not os.path.isfile(bot_config_path): bot_config = { "token": "TYPE HERE BOTS TOKEN KEY" + "log_channel_id" "TYPE HERE CHANNEL ID" } with open(bot_config_path, "w") as w: json.dump(bot_config, w, indent=4)