いらないコードを削除、configにログ送信先チャンネルのIDを記載するように
This commit is contained in:
parent
b084ead6b7
commit
b01ce7b373
1 changed files with 27 additions and 48 deletions
75
dislocker.py
75
dislocker.py
|
@ -4,43 +4,6 @@ import os
|
|||
import psycopg2
|
||||
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):
|
||||
def db_connect(self, host, db, port, user, 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.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),))
|
||||
member_info = cursor.fetchall()
|
||||
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()
|
||||
await message.channel.send(f"使用が開始されました。\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
|
||||
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])))
|
||||
self.db.commit()
|
||||
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:
|
||||
await message.channel.send("パソコンの台数が\n# だめです")
|
||||
else:
|
||||
|
@ -80,9 +49,15 @@ class Bot(discord.Client):
|
|||
cursor = self.db.cursor()
|
||||
cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),))
|
||||
member_info = cursor.fetchall()
|
||||
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()
|
||||
await message.channel.send(f"使用が開始されました。\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
|
||||
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])))
|
||||
self.db.commit()
|
||||
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:
|
||||
await message.channel.send("パソコンの台数が\n# だめです")
|
||||
else:
|
||||
|
@ -95,16 +70,19 @@ class Bot(discord.Client):
|
|||
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()
|
||||
cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (history_info[0][0],))
|
||||
self.db.commit()
|
||||
|
||||
await message.channel.send(f"使用が終了されました。")
|
||||
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],))
|
||||
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("どうしましたか?")
|
||||
|
||||
|
@ -136,6 +114,7 @@ if not os.path.isfile(bot_config_path):
|
|||
|
||||
bot_config = {
|
||||
"token": "TYPE HERE BOTS TOKEN KEY"
|
||||
"log_channel_id" "TYPE HERE CHANNEL ID"
|
||||
}
|
||||
with open(bot_config_path, "w") as w:
|
||||
json.dump(bot_config, w, indent=4)
|
||||
|
|
Loading…
Reference in a new issue