設定がひとつのjsonファイルで完結するように
This commit is contained in:
parent
0a7bb89a98
commit
7d06ad18c4
1 changed files with 42 additions and 41 deletions
83
dislocker.py
83
dislocker.py
|
@ -6,6 +6,42 @@ import hashlib
|
||||||
import string
|
import string
|
||||||
import random
|
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):
|
class Bot(discord.Client):
|
||||||
def db_connect(self, host, db, port, user, password):
|
def db_connect(self, host, db, port, user, password):
|
||||||
self.db = psycopg2.connect(f"host={host} dbname={db} port={port} user={user} password={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]}")
|
await message.channel.send(f"使用が開始されました。\nパスワード | {register["password"]}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}")
|
||||||
elif len(msg_split) == 4:
|
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 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":
|
elif register["result"] == "user_data_not_found":
|
||||||
await message.channel.send("ユーザーとして登録されていないようです。管理者に問い合わせてください。")
|
await message.channel.send("ユーザーとして登録されていないようです。管理者に問い合わせてください。")
|
||||||
elif register["result"] == "pc_already_in_use_by_you":
|
elif register["result"] == "pc_already_in_use_by_you":
|
||||||
|
@ -164,9 +200,9 @@ class Bot(discord.Client):
|
||||||
await message.channel.send("使用されていないようです...")
|
await message.channel.send("使用されていないようです...")
|
||||||
elif stop["result"] == "ok":
|
elif stop["result"] == "ok":
|
||||||
await message.channel.send(f"PC番号 {stop["pc_number"]} の使用が終了されました。")
|
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()
|
msg_split = message.content.split()
|
||||||
if msg_split[0] == "/register":
|
if msg_split[0] == "/register":
|
||||||
if len(msg_split) <= 3:
|
if len(msg_split) <= 3:
|
||||||
|
@ -184,45 +220,10 @@ class Bot(discord.Client):
|
||||||
await message.channel.send("なんでかわからんけど不正です。")
|
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 = discord.Intents.default()
|
||||||
intents.message_content = True
|
intents.message_content = True
|
||||||
|
|
||||||
|
dislocker = Dislocker()
|
||||||
bot = Bot(intents=intents)
|
bot = Bot(intents=intents)
|
||||||
bot.db_connect(db_config["host"], db_config["db"], db_config["port"], db_config["username"], db_config["password"])
|
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(bot_config["token"])
|
bot.run(dislocker.server_config["bot"]["token"])
|
Loading…
Reference in a new issue