This commit is contained in:
suti7yk5032 2024-06-04 09:36:29 +09:00
parent 374033c02b
commit f35f32110e
10 changed files with 282 additions and 0 deletions

1
.gitignore vendored
View file

@ -160,3 +160,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
config

67
dislocker.py Normal file
View file

@ -0,0 +1,67 @@
import json
import discord
import os
import psycopg2
class Bot(discord.Client):
async def on_ready(self):
print("ログイン成功")
async def on_message(self, message):
if message.author.bot:
return
if message.content == "/hello":
await message.channel.send("こんにちは!!!!")
if message.content == "/password":
await message.channel.send("gjhioragiopwjhgiopweap")
class Cert():
def __init__(self):
self.db = psycopg2.connect("host=localhost dbname=dislocker user=suti7 password=testing")
def register(self, username, userid):
cursor = self.db.cursor()
insert_sql = "INSERT INTO user_list (discord_username, discord_userid) VALUES (%s, %s);"
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 = %s AND password = %s "
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 Web(flask.Flask):
async def temp(self):
print("")
cert = Cert()
print(cert.get_username("suti7", "0126"))
bot_config_path = "./config/bot.json"
if not os.path.isfile(bot_config_path):
bot_config = {
"token": ""
}
with open(bot_config_path, "w") as w:
json.dump(bot_config, w, indent=4)
elif os.path.isfile(bot_config_path):
with open("./config/bot.json", "r") as r:
bot_config = json.load(r)
intents = discord.Intents.default()
intents.message_content = True
bot = Bot(intents=intents)
bot.run(bot_config["token"])

View file

@ -0,0 +1,13 @@
# ログイン
psql -U suti7 -d dislocker
CREATE TABLE user
(user_id INTEGER PRIMARY KEY NOT NULL,
discord_id INTEGER NOT NULL,
discord_username VARCHAR(32) NOT NULL,
PRIMARY KEY (user_id));
CREATE TABLE user_list (user_id INTEGER NOT NULL, discord_username VARCHAR(32) NOT NULL, discord_userid INTEGER NOT NULL, PRIMARY KEY (user_id));
# テーブルを作る
CREATE TABLE web_auth (id SERIAL, username VARCHAR(10) NOT NULL, password VARCHAR(32) NOT NULL, PRIMARY KEY(id));

9
resource/console.js Normal file
View file

@ -0,0 +1,9 @@
const send_button = document.querySelector("#send_button");
const success_alert = document.querySelector("#alert_success");
const main = function(){
send_button.addEventListener("click", function(){
success_alert.
})
}

BIN
resource/damedesu.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

45
templates/dacs.html Normal file
View file

@ -0,0 +1,45 @@
<html>
<head>
<title>管理コンソール</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<form method="POST">
<div class="container text-center">
<div class="row">
<div class="col">
</div>
<div class="col">
<h1>登録</h1>
</div>
<div class="col">
</div>
</div>
</div>
<div class="row">
<div class="col">
<label>ユーザーネーム</label>
<p></p>
<input type="text" id="discord_username" name="discord_username" required>
</div>
<div class="col">
<label>ユーザーID</label>
<p></p>
<input type="text" id="discord_userid" name="discord_userid" required>
</div>
<div class="col">
<p></p>
<button id="send_button" type="button submit" class="btn btn-primary">送信</button>
</div>
</div>
</div>
<p></p>
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

9
templates/damedesu.html Normal file
View file

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>だめです いいえ 帰ってください</title>
</head>
<body>
<img src="{{url_for('static', filename='/damedesu.webp')}}">
</body>
</html>

15
templates/login.html Normal file
View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST">
<label for="user_id">User ID</label>
<input type="text" id="user_id" name="user_id" required>
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<button type="submit">Login</button>
</form>
</body>
</html>

34
test.py Normal file
View file

@ -0,0 +1,34 @@
# インストールした discord.py を読み込む
import discord
import secrets
import string
# 接続に必要なオブジェクトを生成
client = discord.Client()
# 起動時に動作する処理
@client.event
async def on_ready():
# 起動したらターミナルにログイン通知が表示される
print('ログインしました')
# メッセージ受信時に動作する処理
@client.event
async def on_message(message):
# メッセージ送信者がBotだった場合は無視する
if message.author.bot:
return
# 「/neko」と発言したら「にゃーん」が返る処理
if message.content == '/neko':
await message.channel.send('にゃーん')
if message.content == '/password':
await message.channel.send(get_random_password_string(4))
def get_random_password_string(length):
pass_chars = string.ascii_letters + string.digits
password = ''.join(secrets.choice(pass_chars) for x in range(length))
return password
# Botの起動とDiscordサーバーへの接続
client.run('MTI0NzA1Mzc1NzUxOTM2NDEyNw.Gh5gIt.kz1acBMxphff9mEZLLWrEdEoVD4RJwgBW5P14o')

89
webui.py Normal file
View file

@ -0,0 +1,89 @@
from flask import Flask, request, render_template, redirect, url_for
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user
import psycopg2
app = Flask(__name__, static_folder="./resource/")
app.secret_key = 'your_secret_key'
login_manager = LoginManager()
login_manager.init_app(app)
class Cert():
def __init__(self):
self.db = psycopg2.connect("host=localhost dbname=dislocker user=suti7 password=testing")
def register(self, username, userid):
cursor = self.db.cursor()
insert_sql = "INSERT INTO user_list (discord_username, discord_userid) VALUES (%s, %s);"
cursor.execute(insert_sql, (username, userid))
self.db.commit()
def login(self, username, password):
cursor = self.db.cursor()
get_sql = "SELECT * FROM web_auth WHERE username = %s AND password = %s "
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
cert = Cert()
class User(UserMixin):
def __init__(self, id):
self.id = id
def get_id(self):
return self.id
@login_manager.user_loader
def load_user(user_id):
return User(user_id)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
user_id = request.form.get('user_id')
password = request.form.get('password')
# ユーザーIDとパスワードの検証を行う
# ここでは省略しますが、実際にはデータベースなどからユーザー情報を取得し、パスワードを検証します
if cert.login(user_id, password) == 0:
user = User(user_id)
login_user(user)
return redirect(url_for('dacs'))
else:
print("WEB UI LOGIN ERROR")
return redirect(url_for("damedesu"))
return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/dacs', methods=["GET", "POST"])
@login_required
def dacs():
if request.method == "POST":
discord_userid = request.form.get("discord_userid")
discord_username = request.form.get("discord_username")
cert.register(discord_username, discord_userid)
return render_template("dacs.html")
@app.route("/damedesu")
def damedesu():
return render_template("damedesu.html")
@app.route("/")
def hello_world():
return "<p>Hello world!</p>"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)