From 7d06ad18c46c2349dbd309b8f938e3a2fe6c8a5b Mon Sep 17 00:00:00 2001 From: suti7yk5032 Date: Sun, 7 Jul 2024 13:49:53 +0900 Subject: [PATCH] =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E3=81=8C=E3=81=B2=E3=81=A8?= =?UTF-8?q?=E3=81=A4=E3=81=AEjson=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=81=A7=E5=AE=8C=E7=B5=90=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dislocker.py | 83 ++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/dislocker.py b/dislocker.py index 465e3ba..d666344 100644 --- a/dislocker.py +++ b/dislocker.py @@ -6,6 +6,42 @@ import hashlib import string import random +class Dislocker(): + def __init__(self): + self.config_dir_path = "./config/" + self.server_config_path = self.config_dir_path + "server.json" + + if not os.path.isdir(self.config_dir_path): + print("config ディレクトリが見つかりません... 作成します。") + os.mkdir(self.config_dir_path) + + if not os.path.isfile(self.server_config_path): + print("config ファイルが見つかりません... 作成します。") + self.server_config = { + "db": { + "host": "localhost", + "port": "5432", + "db_name": "dislocker", + "username": "user", + "password": "password" + }, + "bot": { + "token": "TYPE HERE BOTS TOKEN KEY", + "log_channel_id" : "TYPE HERE CHANNEL ID", + "config_channel_id": "TYPE HERE CHANNEL ID" + } + } + + with open(self.server_config_path, "w") as w: + json.dump(self.server_config, w, indent=4) + + elif os.path.isfile(self.server_config_path): + with open(self.server_config_path, "r") as r: + self.server_config = json.load(r) + + + + 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}") @@ -146,7 +182,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(bot_config["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": @@ -164,9 +200,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(bot_config["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 == bot_config["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: @@ -184,45 +220,10 @@ class Bot(discord.Client): await message.channel.send("なんでかわからんけど不正です。") -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", - "log_channel_id" : "TYPE HERE CHANNEL ID", - "config_channel_id": "TYPE HERE CHANNEL ID" - } - 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 +dislocker = Dislocker() bot = Bot(intents=intents) -bot.db_connect(db_config["host"], db_config["db"], db_config["port"], db_config["username"], db_config["password"]) -bot.run(bot_config["token"]) \ No newline at end of file +bot.db_connect(dislocker.server_config["db"]["host"], dislocker.server_config["db"]["db_name"], dislocker.server_config["db"]["port"], dislocker.server_config["db"]["username"], dislocker.server_config["db"]["password"]) +bot.run(dislocker.server_config["bot"]["token"]) \ No newline at end of file