mirror of
https://github.com/TitanEmbeds/Titan.git
synced 2024-12-23 21:47:04 +01:00
Very basic proof of concept, able to post and recieve messages
This commit is contained in:
parent
e5c45d4c3f
commit
cd9e21fa87
3
.gitignore
vendored
3
.gitignore
vendored
@ -87,3 +87,6 @@ ENV/
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# Project specifc
|
||||
config.py
|
||||
|
54
app.py
Normal file
54
app.py
Normal file
@ -0,0 +1,54 @@
|
||||
from config import config
|
||||
from flask import Flask, render_template, request, jsonify, session, url_for, redirect
|
||||
import requests
|
||||
import json
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = "doafkjgasfjk"
|
||||
|
||||
_DISCORD_API_BASE = "https://discordapp.com/api/v6"
|
||||
|
||||
@app.route("/api/Get_Channel_Messages")
|
||||
def get_channel_messages():
|
||||
channel_id = request.args.get('channel_id')
|
||||
after_snowflake = request.args.get('after', None, type=int)
|
||||
_endpoint = _DISCORD_API_BASE + "/channels/{channel_id}/messages".format(channel_id=channel_id)
|
||||
payload = {}
|
||||
if after_snowflake is not None:
|
||||
payload = {'after': after_snowflake}
|
||||
headers = {'Authorization': 'Bot ' + config['bot-token']}
|
||||
r = requests.get(_endpoint, params=payload, headers=headers)
|
||||
return jsonify(j=json.loads(r.content))
|
||||
|
||||
@app.route("/api/Create_Message", methods=['POST'])
|
||||
def post_create_message():
|
||||
channel_id = request.form.get('channel_id')
|
||||
content = request.form.get('content')
|
||||
username = session['username']
|
||||
_endpoint = _DISCORD_API_BASE + "/channels/{channel_id}/messages".format(channel_id=channel_id)
|
||||
payload = {'content': username + ": " + content}
|
||||
headers = {'Authorization': 'Bot ' + config['bot-token'], 'Content-Type': 'application/json'}
|
||||
r = requests.post(_endpoint, headers=headers, data=json.dumps(payload))
|
||||
return jsonify(j=json.loads(r.content))
|
||||
|
||||
@app.route("/set_username", methods=["GET"])
|
||||
def get_set_username():
|
||||
return render_template("set_username.html")
|
||||
|
||||
@app.route("/set_username", methods=["POST"])
|
||||
def post_set_username():
|
||||
session['username'] = request.form.get('username')
|
||||
return redirect(url_for("embed_get", channelid=1))
|
||||
|
||||
@app.route("/")
|
||||
def hello():
|
||||
return "This page is not blank"
|
||||
|
||||
@app.route("/embed/<channelid>")
|
||||
def embed_get(channelid):
|
||||
if 'username' not in session:
|
||||
return redirect(url_for("get_set_username"))
|
||||
return render_template("embed.html")
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0",port=3000,debug=True)
|
78
templates/embed.html
Normal file
78
templates/embed.html
Normal file
@ -0,0 +1,78 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
<script>
|
||||
function escapeHTML(unsafe) {
|
||||
return unsafe
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
var lastSnowflake = undefined
|
||||
function updateMessages(channelid, aftersnowflake=null) {
|
||||
setTimeout(function() {
|
||||
$.ajax({
|
||||
url: "/api/Get_Channel_Messages",
|
||||
data: {
|
||||
channel_id: channelid,
|
||||
after: aftersnowflake
|
||||
},
|
||||
dataType: "json",
|
||||
success: function(data, status){
|
||||
data = data['j']
|
||||
for (i = data.length-1; i >= 0; i--) {
|
||||
item = data[i]
|
||||
$("#messagesview").append("<p>"+item['id'] + " " + item['timestamp'] + " " + item['author']['username'] + " " + escapeHTML(item['content']) + "</p>")
|
||||
lastSnowflake = item['id']
|
||||
|
||||
if($("#messagesview").scrollTop() + $("#messagesview").innerHeight() >= $("#messagesview")[0].scrollHeight - 160) {
|
||||
$('#messagesview').scrollTop($('#messagesview').prop("scrollHeight"));
|
||||
}
|
||||
}
|
||||
},
|
||||
complete: function() {
|
||||
updateMessages("140252024666062848", lastSnowflake)
|
||||
}
|
||||
});
|
||||
}, 5000);
|
||||
}
|
||||
updateMessages("140252024666062848")
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div style="height: 95vh; overflow-y: auto;" id="messagesview"></div>
|
||||
|
||||
<div style="height: 5vh;">
|
||||
<input type="text" id="messagebox" style="height:100%; width:100%; font-size:15px;">
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var channelid = "140252024666062848"
|
||||
$('#messagebox').bind("enterKey",function(e){
|
||||
var content = $('#messagebox').val();
|
||||
$('#messagebox').val('')
|
||||
$.ajax({
|
||||
url: "/api/Create_Message",
|
||||
method: "post",
|
||||
data: {
|
||||
channel_id: channelid,
|
||||
content: content
|
||||
},
|
||||
dataType: "json"
|
||||
})
|
||||
});
|
||||
$('#messagebox').keyup(function(e){
|
||||
if(e.keyCode == 13)
|
||||
{
|
||||
var content = $('#messagebox').val();
|
||||
if (/\S/.test(content)) {
|
||||
$(this).trigger("enterKey");
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
21
templates/set_username.html
Normal file
21
templates/set_username.html
Normal file
@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<form method="post">
|
||||
<h1>Set username:<h1>
|
||||
<input type="text" name="username" id="username" required>
|
||||
</form>
|
||||
<script>
|
||||
$("input").keypress(function(event) {
|
||||
var content = $('#username').val();
|
||||
if (event.which == 13 && /\S/.test(content)) {
|
||||
event.preventDefault();
|
||||
$("form").submit();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user