import json import discord 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, 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 = "./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): os.mkdir(config_dir_path) db_config = { "host": "localhost", "db": "dislocker", "username": "user", "password": "example_pass", "port": "5432" } 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_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": "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(bot_config_path, "r") as r: bot_config = json.load(r) 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"])