Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/disc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import requests
import json

class DiscordException(Exception):
pass

class ShittyDiscordHandler:
def __init__(self, token: str, http_headers:dict={}):
self.CONSTRUCT_AVATAR_URL = "https://cdn.discordapp.com/avatars/%s/%s.png"
self.CONSTRUCT_USER_URL = "https://discord.com/api/users/%s"
self.BOT_USER_TOKEN = token

self.REQUEST_HEADERS = {
"Authorization": "Bot %s" % self.BOT_USER_TOKEN
}
self.REQUEST_HEADERS.update(http_headers)

def getUserInformation(self, userid) -> dict:
r = requests.get(self.CONSTRUCT_USER_URL % str(userid), headers=self.REQUEST_HEADERS)
d = json.loads(r.text)

if r.status_code != 200:
raise DiscordException(
d["message"])

return d

def getAvatarFromUser(self, userid, size=128, rc=False):
d = self.getUserInformation(userid)
avatar_hash = user_data["avatar"]

u = self.CONSTRUCT_AVATAR_URL % (str(userid), avatar_hash) + "?size=%s" % str(size)
if rc == False:
return u
else:
return u, user_data
54 changes: 51 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import utility
import json
from dotenv import load_dotenv

import disc
load_dotenv() # Load environment variables from .env file
app = Flask(__name__)

Expand All @@ -26,7 +26,10 @@


def message(title, message, fulltitle=True):
return render_template('information.html', title=title, content=message, fulltitle=fulltitle)
return render_template('information.html',
title=title,
content=[line.strip("\n") for line in message.split("\n")],
fulltitle=fulltitle)

# SERVE STATIC FILES
@app.route("/webui/static/<path:path>")
Expand Down Expand Up @@ -62,7 +65,7 @@ def validateToken(token, tokenType):

@app.route("/webui/")
def index():
return message("AlphaGameBot WebUI", "Welcome to the AlphaGameBot WebUI. Unfortunately, there is no real 'index'... Please use a command like /user settings in AlphaGameBot to get a link to interact with it.")
return message("AlphaGameBot WebUI", "Welcome to the AlphaGameBot WebUI.\nUnfortunately, there is no real 'index'... Please use a command like /user settings in AlphaGameBot to get a link to interact with it.")

@app.route("/webui/user/settings")
def user_settings():
Expand Down Expand Up @@ -148,6 +151,51 @@ def user_settings_applied():
def guild_settings_applied():
return "<h2>Settings Applied.</h2>"

@app.route("/webui/user/stats/<int:userid>")
def user_page_global(userid: int):
cnx.reconnect()
c = cnx.cursor()

c.execute("SELECT messages_sent, commands_ran FROM user_stats WHERE userid = %s", [userid])

try:
messages, commands = c.fetchone()
except TypeError:
return message("Error",
"This user is not recognized.\n"
"Remember that AlphaGameBot needs to be able to see this user (i.e, being in at least one server with them),"
" and that they need to have sent at least one message.\n\n"
"If you believe that this is an error, please make a GitHub issue."), 404


i = disc.ShittyDiscordHandler(os.getenv("TOKEN"))

pfp, d = i.getAvatarFromUser(userid, size=64, rc=True)
print(pfp)

return render_template("user_stats.html", messages_sent=messages, username=d['username'], commands_ran=commands, pfp=pfp, guild=True, userid=userid)




@app.route("/webui/user/stats/<int:userid>/<int:guildid>")
def user_page_guild(userid: int, guildid: int):
cnx.reconnect()
c = cnx.cursor()

c.execute("SELECT messages_sent, commands_ran, user_level FROM guild_user_stats WHERE userid = %s AND guildid = %s", (userid, guildid))

try:
messages, commands, level = c.fetchone()
except TypeError:
return message("Error", "This user and/or guild is not recognized."), 404

i = disc.ShittyDiscordHandler(os.getenv("TOKEN"))

pfp, d = i.getAvatarFromUser(userid, size=64, rc=True)
print(pfp)
return render_template("user_stats.html", messages_sent=messages, username=d['username'], commands_ran=commands, level=level, pfp=pfp, guild=True, userid=userid)

@app.route("/healthcheck")
@app.route("/webui/healthcheck")
def healthcheck():
Expand Down
13 changes: 12 additions & 1 deletion src/static/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ a,a:visited {
text-align: center;
margin-left: auto;
margin-right: auto;
}
}

.tooltip span {
display: none;
background-color: yellow;
border: 1px dashed #000000;
cursor: pointer;
}

.tooltip:hover span {
display: inline;
}
12 changes: 12 additions & 0 deletions src/static/information.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
footer {
position: fixed;
bottom: 0;
left: 0;
right: 0;
text-align: center;
font-size: 12pt;
}

footer p {
margin: 0;
}
25 changes: 21 additions & 4 deletions src/templates/information.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
<!-- Template: information.html; Path src/templates/information.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Information &bull; AlphaGameBot Web UI</title>
<title>{{title}} &bull; AlphaGameBot Web UI</title>
<link rel="stylesheet" type="text/css" href="/webui/static/pico.min.css" />
<link rel="stylesheet" type="text/css" href="/webui/static/global.css" />
<link rel="stylesheet" type="text/css" href="/webui/static/information.css" />
</head>
<body class="center">
{% if fulltitle %}
<h1>{{title}}</h1>
{% else %}
<h2>{{title}}</h2>
{% endif %}
<p>{{content}}</p>
<footer>AlphaGameBot Web UI &copy; Damien Boisvert (AlphaGameDeveloper) 2024.</footer>

<div id="information_content">
{% for line in content %}
{% if line == "" %}
<!-- empty line -->
<p>&nbsp;</p>
{% else %}
<p>{{line}}</p>
{% endif %}
{% endfor %}
</div>
</body>
<footer>
<p>AlphaGameBot Web UI &copy; Damien Boisvert (AlphaGameDeveloper) 2024.</p>
<p>GitHub:
<a href="https://github.com/AlphaGameBot/AlphaGameBot">AlphaGameBot</a>
and the
<a href="https://github.com/AlphaGameBot/WebUI">WebUI</a>
</footer>

</html>
27 changes: 27 additions & 0 deletions src/templates/user_stats.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<title>User Stats &bull; AlphaGameBot</title>
<link rel="stylesheet" type="text/css" href="/webui/static/global.css">
<link rel="stylesheet" type="text/css" href="/webui/static/pico.min.css">
<script src="/webui/static/htmx.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body class="center">
<h1>User Stats for
<span class="tooltip"><i>{{username}}</i>
<span>ID: {{userid}}</span>
</span>
<img src="{{pfp}}" />
</h1>
<hr />
<p>Messages Sent: <b>{{messages_sent}}</b></p>
<p>Commands Ran: <b>{{commands_ran}}</b></p>
{% if guild %}
<!-- GUILD -->
<p>Guild</p>
<p>Guild Level: <b>{{level}}</b></p>
<!-- END GUILD -->
{% endif %}
</body>
</html>
2 changes: 1 addition & 1 deletion webui.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"VERSION": "1.4"
"VERSION": "1.5"
}