いらないコードを削除、configにログ送信先チャンネルのIDを記載するように

This commit is contained in:
suti7yk5032 2024-06-05 16:13:57 +09:00
parent b084ead6b7
commit b01ce7b373

View file

@ -4,43 +4,6 @@ import os
import psycopg2 import psycopg2
import datetime 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): 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}")
@ -66,9 +29,15 @@ class Bot(discord.Client):
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),)) cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),))
member_info = cursor.fetchall() 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 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]))) 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() self.db.commit()
await message.channel.send(f"使用が開始されました。\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") await message.channel.send(f"使用が開始されました。\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: else:
await message.channel.send("パソコンの台数が\n# だめです") await message.channel.send("パソコンの台数が\n# だめです")
else: else:
@ -80,9 +49,15 @@ class Bot(discord.Client):
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),)) cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),))
member_info = cursor.fetchall() 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 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]))) 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() self.db.commit()
await message.channel.send(f"使用が開始されました。\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") await message.channel.send(f"使用が開始されました。\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: else:
await message.channel.send("パソコンの台数が\n# だめです") await message.channel.send("パソコンの台数が\n# だめです")
else: else:
@ -95,10 +70,13 @@ class Bot(discord.Client):
member_info = cursor.fetchall() 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],)) 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() history_info = cursor.fetchall()
if history_info[0][5]:
await message.channel.send("使用されていないようです...")
else:
cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (history_info[0][0],)) cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (history_info[0][0],))
self.db.commit() self.db.commit()
await message.channel.send(f"使用が終了されました。") 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: else:
@ -136,6 +114,7 @@ if not os.path.isfile(bot_config_path):
bot_config = { bot_config = {
"token": "TYPE HERE BOTS TOKEN KEY" "token": "TYPE HERE BOTS TOKEN KEY"
"log_channel_id" "TYPE HERE CHANNEL ID"
} }
with open(bot_config_path, "w") as w: with open(bot_config_path, "w") as w:
json.dump(bot_config, w, indent=4) json.dump(bot_config, w, indent=4)