diff --git a/src/disc.py b/src/disc.py new file mode 100644 index 0000000..1812b3c --- /dev/null +++ b/src/disc.py @@ -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 diff --git a/src/main.py b/src/main.py index 77bcb42..96870e4 100644 --- a/src/main.py +++ b/src/main.py @@ -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__) @@ -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/") @@ -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(): @@ -148,6 +151,51 @@ def user_settings_applied(): def guild_settings_applied(): return "

Settings Applied.

" +@app.route("/webui/user/stats/") +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//") +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(): diff --git a/src/static/global.css b/src/static/global.css index 10d669f..6cdb028 100644 --- a/src/static/global.css +++ b/src/static/global.css @@ -12,4 +12,15 @@ a,a:visited { text-align: center; margin-left: auto; margin-right: auto; -} \ No newline at end of file +} + +.tooltip span { + display: none; + background-color: yellow; + border: 1px dashed #000000; + cursor: pointer; +} + +.tooltip:hover span { + display: inline; +} diff --git a/src/static/information.css b/src/static/information.css new file mode 100644 index 0000000..dc31d89 --- /dev/null +++ b/src/static/information.css @@ -0,0 +1,12 @@ +footer { + position: fixed; + bottom: 0; + left: 0; + right: 0; + text-align: center; + font-size: 12pt; +} + +footer p { + margin: 0; +} diff --git a/src/templates/information.html b/src/templates/information.html index 9649089..8dca776 100644 --- a/src/templates/information.html +++ b/src/templates/information.html @@ -1,11 +1,13 @@ + - Information • AlphaGameBot Web UI + {{title}} • AlphaGameBot Web UI + {% if fulltitle %} @@ -13,8 +15,23 @@

{{title}}

{% else %}

{{title}}

{% endif %} -

{{content}}

-
AlphaGameBot Web UI © Damien Boisvert (AlphaGameDeveloper) 2024.
- +
+ {% for line in content %} + {% if line == "" %} + +

 

+ {% else %} +

{{line}}

+ {% endif %} + {% endfor %} +
+
+

AlphaGameBot Web UI © Damien Boisvert (AlphaGameDeveloper) 2024.

+

GitHub: + AlphaGameBot + and the + WebUI +

+ diff --git a/src/templates/user_stats.html b/src/templates/user_stats.html new file mode 100644 index 0000000..a0df66e --- /dev/null +++ b/src/templates/user_stats.html @@ -0,0 +1,27 @@ + + + + User Stats • AlphaGameBot + + + + + + +

User Stats for + {{username}} + ID: {{userid}} + + +

+
+

Messages Sent: {{messages_sent}}

+

Commands Ran: {{commands_ran}}

+ {% if guild %} + +

Guild

+

Guild Level: {{level}}

+ + {% endif %} + + diff --git a/webui.json b/webui.json index 961b2e3..0b9ba1e 100644 --- a/webui.json +++ b/webui.json @@ -1,3 +1,3 @@ { - "VERSION": "1.4" + "VERSION": "1.5" }