諸々の処理を関数に導入

This commit is contained in:
suti7yk5032 2024-06-09 18:39:42 +09:00
parent 128ebc88b3
commit 0427c021a0

View file

@ -13,12 +13,69 @@ class Bot(discord.Client):
def password_generate(self, length):
numbers = string.digits # (1)
password = ''.join(random.choice(numbers) for _ in range(length)) # (2)
return str(password)
return password
def hash_genarate(self, source):
hashed = hashlib.md5(source.encode())
return hashed.hexdigest()
def register(self, **kwrags):
discord_user_id = str(kwrags["user_id"])
pc_number = int(kwrags["pc_number"])
device_number = int(kwrags["device_number"])
if "detail" in kwrags:
detail = str(kwrags["detail"])
else:
detail = None
#パスワード生成、ハッシュ化
password = self.password_generate(4)
password_hash = self.hash_genarate(password)
#メンバーリストと送信者を照合
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[0]:
return {"result": "user_data_not_found"}
#ユーザーデータが見つかった場合(登録済みの場合)
elif user_record[0]:
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:
return {"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,))
pc_list_record = cursor.fetchall()
if not pc_list_record[0][1] == None:
return {"result": "pc_already_in_use_by_other"}
elif pc_list_record[0][1] == None:
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("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))
self.db.commit()
return {"result": "ok", "password": str(password), "name": str(user_record[0][1])}
def stop(self, **kwrags):
discord_user_id = str(kwrags["user_id"])
cursor = self.db.cursor()
cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (discord_user_id,))
user_record = cursor.fetchall()
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 not pc_usage_history_record[0][5] == None:
return {"result": "unused"}
elif pc_usage_history_record[0][5] == None:
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],))
self.db.commit()
return {"result": "ok", "pc_number": str(pc_usage_history_record[0][2]), "name": str(user_record[0][1])}
async def on_ready(self):
print("ログイン成功")
@ -27,113 +84,39 @@ class Bot(discord.Client):
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 msg_split[0] == "/password" or "/start":
#メッセージの要素が2つ以下の場合は拒否
if len(msg_split) <= 2:
await message.channel.send("PC番号、もしくはデバイス番号が入力されていません。")
#メッセージの要素が3つ(コマンド、PC番号、デバイス番号)の場合
elif len(msg_split) == 3:
#番号が数字であることを確認
if msg_split[1].isdigit() and msg_split[2].isdigit():
#PC番号が1以上10以下であることを確認
if int(msg_split[1]) <= 10 and int(msg_split[1]) >= 1:
password = self.password_generate(4)
password_hash = self.hash_genarate(password)
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()
if len(history_info) == 0:
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()
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1]))
self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, msg_split[1]))
self.db.commit()
await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{msg_split[1]}を使用しています')
elif not history_info[0][5]:
await message.channel.send(f"もう使用されているようです。解除するには /stop で使用終了をお知らせください。\n使用中のPC番号 | {str(history_info[0][2])}\n使用中のデバイス番号 | {str(history_info[0][3])}\n使用開始時刻 | {str(history_info[0][4])}\n使用目的 | {str(history_info[0][6])}")
else:
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()
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1]))
self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, msg_split[1]))
self.db.commit()
await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{msg_split[1]}を使用しています')
register = self.register(user_id=message.author.id, pc_number=msg_split[1], device_number=msg_split[2])
if register["result"] == "ok":
await message.channel.send(f"使用が開始されました。\nパスワード | {register["password"]}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
await self.get_channel(bot_config["log_channel_id"]).send(f'{register["name"]} さんがPC{register["pc_number"]}を使用しています')
elif register["result"] == "user_data_not_found":
await message.channel.send("ユーザーとして登録されていないようです。管理者に問い合わせてください。")
elif register["result"] == "pc_already_in_use_by_you":
await message.channel.send(f"あなたはPCをもう使用されているようです。使用状態を解除するには /stop で使用終了をお知らせください。\nPC番号 | {register["pc_number"]}\nデバイス番号 | {register["device_number"]}\n使用開始時刻 | {register["start_time"]}\n使用目的 | {register["detail"]}")
elif register["result"] == "pc_already_in_use_by_other":
await message.channel.send(f"PCはもう使用されています。別のPC番号を指定して、再度お試しください。")
else:
await message.channel.send("パソコンの台数が\n# だめです")
await message.channel.send("番号がおかしいようです。")
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:
password = self.password_generate(4)
password_hash = self.hash_genarate(password)
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()
if len(history_info) == 0:
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()
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1]))
self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, msg_split[1]))
self.db.commit()
await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{msg_split[1]}を使用しています')
elif not history_info[0][5]:
await message.channel.send(f"もう使用されているようです。解除するには /stop で使用終了をお知らせください。\n使用中のPC番号 | {str(history_info[0][2])}\n使用中のデバイス番号 | {str(history_info[0][3])}\n使用開始時刻 | {str(history_info[0][4])}\n使用目的 | {str(history_info[0][6])}")
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, %s)", (member_info[0][0], int(msg_split[1]), int(msg_split[2]), str(msg_split[3])))
self.db.commit()
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1]))
self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, msg_split[1]))
self.db.commit()
await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{msg_split[1]}を使用しています')
else:
await message.channel.send("パソコンの台数が\n# だめです")
else:
await message.channel.send("構文が不正です。")
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()
if history_info[0][5]:
stop = self.stop(user_id=message.author.id)
if stop["result"] == "unused":
await message.channel.send("使用されていないようです...")
else:
cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (history_info[0][0],))
self.db.commit()
cursor.execute("UPDATE pc_list SET using_user_id = NULL WHERE pc_number = %s", (history_info[0][2],))
self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (history_info[0][2],))
self.db.commit()
await message.channel.send(f"使用が終了されました。")
await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{history_info[0][2]}の使用を終了しました')
else:
if message.content == "/hello":
await message.channel.send("こんにちは!!!!")
else:
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"]}の使用を終了しました')
config_dir_path = "./config/"