ユーザー登録が指定テキストチャンネルでできるように

This commit is contained in:
suti7yk5032 2024-06-09 19:56:59 +09:00
parent de96b457eb
commit 5c15c0b092

View file

@ -38,22 +38,22 @@ class Bot(discord.Client):
if not user_record[0]:
result = {"result": "user_data_not_found"}
#ユーザーデータが見つかった場合(登録済みの場合)
elif user_record[0]:
else:
cursor.execute("SELECT * FROM pc_usage_history WHERE member_id=%s ORDER BY id DESC LIMIT 1", (user_record[0][0],))
pc_usage_history_record = cursor.fetchall()
if pc_usage_history_record[0][5] == None:
result = {"result": "pc_already_in_use_by_you", "pc_number": str(pc_usage_history_record[0][2]), "device_number": str(pc_usage_history_record[0][3]), "start_time": str(pc_usage_history_record[0][4]), "detail": str(pc_usage_history_record[0][6])}
elif pc_usage_history_record[0][5] == None:
cursor.execute("SELECT * FROM pc_list WHERE pc_number=%s ORDER BY id DESC LIMIT 1", (pc_number,))
else:
cursor.execute("SELECT * FROM pc_list WHERE pc_number=%s", (pc_number,))
pc_list_record = cursor.fetchall()
if not pc_list_record[0][1] == None:
result = {"result": "pc_already_in_use_by_other"}
elif pc_list_record[0][1] == None:
else:
if detail == None:
cursor.execute("INSERT INTO pc_usage_history (member_id, pc_number, device_number, start_use_time) VALUES (%s, %s, %s, current_timestamp)", (user_record[0][0], pc_number, device_number))
#使用用途があるとき
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)", (user_record[0][0], pc_number, device_number, detail))
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)", (user_record[0][0], pc_number, device_number, detail))
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (user_record[0][0], pc_number))
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, pc_number))
@ -72,7 +72,7 @@ class Bot(discord.Client):
if not pc_usage_history_record[0][5] == None:
result = {"result": "unused"}
elif pc_usage_history_record[0][5] == None:
else:
cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (pc_usage_history_record[0][0],))
cursor.execute("UPDATE pc_list SET using_user_id = NULL WHERE pc_number = %s", (pc_usage_history_record[0][2],))
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (pc_usage_history_record[0][2],))
@ -81,6 +81,22 @@ class Bot(discord.Client):
return result
def user_register(self, **kwrags):
discord_user_id = str(kwrags["discord_user_id"])
discord_user_name = str(kwrags["discord_user_name"])
name = str(kwrags["name"])
cursor = self.db.cursor()
cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (discord_user_id,))
user_record = cursor.fetchall()
if not user_record:
cursor.execute("INSERT INTO club_member (name, discord_username, discord_userid) VALUES (%s, %s, %s)", (name, discord_user_name, discord_user_id))
self.db.commit()
result = {"result": "ok"}
else:
result = {"result": "already_exists"}
return result
async def on_ready(self):
print("ログイン成功")
@ -88,10 +104,9 @@ class Bot(discord.Client):
if message.author.bot:
pass
if isinstance(message.channel, discord.DMChannel):
elif isinstance(message.channel, discord.DMChannel):
msg_split = message.content.split()
print(msg_split)
if msg_split[0] == "/password" or "/start":
if msg_split[0] == "/password" or msg_split[0] == "/start":
#メッセージの要素が2つ以下の場合は拒否
if len(msg_split) <= 2:
await message.channel.send("PC番号、もしくはデバイス番号が入力されていません。")
@ -111,7 +126,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{register["pc_number"]}を使用しています')
await self.get_channel(bot_config["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":
@ -129,7 +144,27 @@ 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(bot_config["log_channel_id"]).send(f'{stop["name"]} さんがPC {stop["pc_number"]} の使用を終了しました')
elif message.channel.id == bot_config["config_channel_id"]:
msg_split = message.content.split()
if msg_split[0] == "/register":
if len(msg_split) <= 3:
await message.channel.send("名前、Discordのユーザー名、DiscordのユーザーIDのいずれかが入力されていません。")
elif len(msg_split) == 4:
if msg_split[3].isdigit():
register = self.user_register(name=msg_split[1], discord_user_name=msg_split[2], discord_user_id=msg_split[3])
if register["result"] == "ok":
await message.channel.send(f"登録が完了しました。\n名前 | {msg_split[1]}\nDiscordのユーザー名 | {msg_split[2]}\nDiscordのユーザーID | {msg_split[3]}")
elif register["result"] == "already_exists":
await message.channel.send("そのDiscordアカウントはすでに登録されています。")
else:
await message.channel.send("DiscordのユーザーIDが不正です。")
else:
await message.channel.send("なんでかわからんけど不正です。")
config_dir_path = "./config/"
@ -158,7 +193,8 @@ if not os.path.isfile(bot_config_path):
bot_config = {
"token": "TYPE HERE BOTS TOKEN KEY",
"log_channel_id" : "TYPE HERE CHANNEL ID"
"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)