From 68bd6ca2828771ed6b61c9845e7995feff9a2218 Mon Sep 17 00:00:00 2001 From: suti7yk5032 Date: Tue, 4 Jun 2024 19:43:52 +0900 Subject: [PATCH 1/5] =?UTF-8?q?db=E3=82=82=E8=BF=BD=E5=8A=A0(sqlite?= =?UTF-8?q?=E7=94=A8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8165e1f..76cbdaf 100644 --- a/.gitignore +++ b/.gitignore @@ -160,4 +160,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ -config +config/ +db/ From 05f17d6d68ed7fc262d82a172f295602da82a467 Mon Sep 17 00:00:00 2001 From: suti7yk5032 Date: Tue, 4 Jun 2024 19:53:24 +0900 Subject: [PATCH 2/5] =?UTF-8?q?DM=E3=81=8B=E3=82=92=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E3=81=A7=E3=81=8D=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 | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/dislocker.py b/dislocker.py index 01595be..a394189 100644 --- a/dislocker.py +++ b/dislocker.py @@ -11,6 +11,9 @@ class Bot(discord.Client): if message.author.bot: return + if isinstance(message.channel, discord.DMChannel): + await message.channel.send("DMを確認") + if message.content == "/hello": await message.channel.send("こんにちは!!!!") @@ -39,25 +42,39 @@ class Cert(): elif result[0][1] == username and result[0][2] == password: return 0 -class Web(flask.Flask): - async def temp(self): - print("") +#cert = Cert() +config_dir_path = "./confg/" +db_config_path = config_dir_path + "db.json" +if not os.path.isfile(db_config_path): + if not os.path.isdir(config_dir_path): + os.mkdir(config_dir_path) -cert = Cert() -print(cert.get_username("suti7", "0126")) + db_config = { + "host": "localhost", + "db": "dislocker", + "username": "user", + "password": "example_pass" + } + with open(db_config_path, "w") as w: + json.dump(db_config, w, indent=4) +elif os.path.isfile(db_config_path): + with open(db_config_path, "r") as r: + db_config = json.load(r) - -bot_config_path = "./config/bot.json" +bot_config_path = config_dir_path + "bot.json" if not os.path.isfile(bot_config_path): + if not os.path.isdir(config_dir_path): + os.mkdir(config_dir_path) + bot_config = { - "token": "" + "token": "TYPE HERE BOTS TOKEN KEY" } with open(bot_config_path, "w") as w: json.dump(bot_config, w, indent=4) elif os.path.isfile(bot_config_path): - with open("./config/bot.json", "r") as r: + with open(bot_config_path, "r") as r: bot_config = json.load(r) intents = discord.Intents.default() From 61219d52ef189dfcc9f8914927ae284286bc4ec2 Mon Sep 17 00:00:00 2001 From: suti7yk5032 Date: Tue, 4 Jun 2024 19:53:35 +0900 Subject: [PATCH 3/5] 1st --- requirements.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bb96364 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +discord.py +flask +flask-login +psycopg2 \ No newline at end of file From e0e4ac73bbf4efb1052a407ed35276624e15ffc1 Mon Sep 17 00:00:00 2001 From: suti7yk5032 Date: Tue, 4 Jun 2024 22:53:31 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=E4=B8=80=E5=BF=9C=E5=8B=95=E3=81=8F?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dislocker.py | 94 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/dislocker.py b/dislocker.py index a394189..2f1f4c4 100644 --- a/dislocker.py +++ b/dislocker.py @@ -2,26 +2,9 @@ import json import discord import os import psycopg2 +import datetime -class Bot(discord.Client): - async def on_ready(self): - print("ログイン成功") - - async def on_message(self, message): - if message.author.bot: - return - - if isinstance(message.channel, discord.DMChannel): - await message.channel.send("DMを確認") - - if message.content == "/hello": - await message.channel.send("こんにちは!!!!") - - if message.content == "/password": - await message.channel.send("gjhioragiopwjhgiopweap") - - -class Cert(): +class Database(): def __init__(self): self.db = psycopg2.connect("host=localhost dbname=dislocker user=suti7 password=testing") @@ -42,9 +25,76 @@ class Cert(): elif result[0][1] == username and result[0][2] == password: return 0 +class Bot(discord.Client): + def db_connect(self, host, db, user, password): + self.db = psycopg2.connect(f"host={host} dbname={db} user={user} password={password}") + + async def on_ready(self): + print("ログイン成功") + + async def on_message(self, message): + if message.author.bot: + return + + if isinstance(message.channel, discord.DMChannel): + user_id = message.author.id + user_name = message.author.name + msg_split = message.content.split() + if msg_split[0] == "/password": + if len(msg_split) <= 2: + await message.channel.send("PC番号、もしくはデバイス番号が入力されていません。") + + elif len(msg_split) == 3: + if msg_split[1].isdigit() and msg_split[2].isdigit(): + if int(msg_split[1]) <= 10 and int(msg_split[1]) >= 1: + 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") + else: + await message.channel.send("パソコンの台数が\n# だめです") + else: + await message.channel.send("構文が不正です。") + + elif len(msg_split) >= 4: + if msg_split[1].isdigit() and msg_split[2].isdigit(): + if int(msg_split[1]) <= 10 and int(msg_split[1]) >= 1: + 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") + else: + await message.channel.send("パソコンの台数が\n# だめです") + else: + await message.channel.send("構文が不正です。") + + + elif msg_split[0] == "/stop": + cursor = self.db.cursor() + cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),)) + 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"使用が終了されました。") + + else: + + if message.content == "/hello": + await message.channel.send("こんにちは!!!!") + + else: + await message.channel.send("どうしましたか?") + #cert = Cert() -config_dir_path = "./confg/" +config_dir_path = "./config/" db_config_path = config_dir_path + "db.json" if not os.path.isfile(db_config_path): if not os.path.isdir(config_dir_path): @@ -54,7 +104,8 @@ if not os.path.isfile(db_config_path): "host": "localhost", "db": "dislocker", "username": "user", - "password": "example_pass" + "password": "example_pass", + "port": "5432" } with open(db_config_path, "w") as w: json.dump(db_config, w, indent=4) @@ -81,4 +132,5 @@ intents = discord.Intents.default() intents.message_content = True bot = Bot(intents=intents) +bot.db_connect(db_config["host"], db_config["db"], db_config["username"], db_config["password"]) bot.run(bot_config["token"]) \ No newline at end of file From dfcc4d7c371d08af53811a2978ea3f359a387569 Mon Sep 17 00:00:00 2001 From: suti7yk5032 Date: Tue, 4 Jun 2024 22:53:53 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=E3=83=86=E3=83=BC=E3=83=96=E3=83=AB?= =?UTF-8?q?=E3=81=AE=E5=90=8D=E5=89=8D=E3=80=81=E3=82=AB=E3=83=A9=E3=83=A0?= =?UTF-8?q?=E3=81=AA=E3=81=A9=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/データベースセットアップ.txt | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/docs/データベースセットアップ.txt b/docs/データベースセットアップ.txt index d719e70..14d8667 100644 --- a/docs/データベースセットアップ.txt +++ b/docs/データベースセットアップ.txt @@ -1,13 +1,8 @@ # ログイン psql -U suti7 -d dislocker -CREATE TABLE user -(user_id INTEGER PRIMARY KEY NOT NULL, -discord_id INTEGER NOT NULL, -discord_username VARCHAR(32) NOT NULL, -PRIMARY KEY (user_id)); +CREATE TABLE club_member (id SERIAL NOT NULL, name VARCHAR(30) NOT NULL, discord_username VARCHAR(32) NOT NULL, discord_userid VARCHAR(18) NOT NULL, PRIMARY KEY (id)); +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 (memb +er_id) REFERENCES club_member(id)); -CREATE TABLE user_list (user_id INTEGER NOT NULL, discord_username VARCHAR(32) NOT NULL, discord_userid INTEGER NOT NULL, PRIMARY KEY (user_id)); - -# テーブルを作る -CREATE TABLE web_auth (id SERIAL, username VARCHAR(10) NOT NULL, password VARCHAR(32) NOT NULL, PRIMARY KEY(id)); \ No newline at end of file +INSERTのとき文字列はシングルクォーテーションで囲む!!! \ No newline at end of file