From 9ced39de7568fa5623a7e2c251eaef095589a909 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Mon, 14 Feb 2022 22:21:02 -0600 Subject: [PATCH] Some small fixes Signed-off-by: jolheiser --- api/views.py | 2 +- discord.py | 29 +++++++++++++++++++++++++++++ middleware.py | 2 +- utils.py | 25 ++++++------------------- 4 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 discord.py diff --git a/api/views.py b/api/views.py index fdba1eb..af4a8e1 100644 --- a/api/views.py +++ b/api/views.py @@ -314,7 +314,7 @@ class PluginAPI(View): warning.save() json['message'] = "Warning issued." link = mcm_utils.full_reverse('note_info', warning.id) - msg = mcm_utils.build_warning(warning, link) + msg = mcm_utils.build_note(warning, link) mcm_api.discord_mcm(embed=msg) except Exception as ex: json['status'] = False diff --git a/discord.py b/discord.py new file mode 100644 index 0000000..d6829ea --- /dev/null +++ b/discord.py @@ -0,0 +1,29 @@ +from enum import Enum +from datetime import datetime +from typing import Union + + +class TimestampStyle(Enum): + SHORT_TIME = "t" + """16:20""" + LONG_TIME = "T" + """16:20:30""" + SHORT_DATE = "d" + """20/04/2021""" + LONG_DATE = "D" + """20 April 2021""" + SHORT_DATETIME = "f" + """20 April 2021 16:20""" + LONG_DATETIME = "F" + """Tuesday, 20 April 2021 16:20""" + RELATIVE = "R" + """2 months ago""" + + +def format_timestamp(unix: Union[int, datetime], style: TimestampStyle = TimestampStyle.SHORT_DATETIME) -> str: + t = 0 + if isinstance(unix, int): + t = unix + elif isinstance(unix, datetime): + t = unix.timestamp() + return f"" diff --git a/middleware.py b/middleware.py index 69fe329..c7727b9 100644 --- a/middleware.py +++ b/middleware.py @@ -13,4 +13,4 @@ class TimezoneMiddleware(MiddlewareMixin): try: timezone.activate(pytz.timezone(request.user.usersettings.default_timezone)) except: - timezone.deactivate() \ No newline at end of file + timezone.deactivate() diff --git a/utils.py b/utils.py index d9af097..4df443b 100644 --- a/utils.py +++ b/utils.py @@ -30,7 +30,7 @@ def build_application(application): embed = discord.Embed(colour=discord.Colour(0x417505)) embed.title = "Application" embed.set_thumbnail( - url="https://minotar.net/helm/{0}/100.png".format(application.username)) + url=f"https://minotar.net/helm/{application.username}/100.png") embed.add_field(name="Application ID", value=application.id) embed.add_field(name="Username", value=application.username.replace("_", "\\_")) embed.add_field(name="Age", value=application.age) @@ -43,7 +43,7 @@ def build_application(application): embed.add_field(name="Read the Rules", value=application.read_rules) embed.timestamp = application.date embed.add_field(name="Status", value=application.status) - embed.add_field(name="Link", value=full_reverse('application_info', application.id)) + embed.add_field(name="Link", value=full_reverse('application_info', application.id), inline=False) return embed @@ -57,19 +57,7 @@ def build_ticket(ticket, link): if ticket.x and ticket.y and ticket.z and ticket.world: embed.add_field(name="Location", value=ticket.location) embed.add_field(name="Message", value=ticket.message) - embed.add_field(name="Link", value=link) - return embed - - -def build_warning(warning, link): - embed = discord.Embed(colour=discord.Colour(0x417505)) - embed.title = "Warning" - embed.set_thumbnail(url=full_static("favicon.png")) - embed.timestamp = warning.date - embed.add_field(name="Player", value=warning.player.username.replace("_", "\\_")) - embed.add_field(name="Importance", value=warning.importance_display) - embed.add_field(name="Message", value=warning.message) - embed.add_field(name="Link", value=link) + embed.add_field(name="Link", value=link, inline=False) return embed @@ -78,16 +66,15 @@ def build_note(note, link): embed.title = "Note" embed.set_thumbnail(url=full_static("favicon.png")) embed.timestamp = note.date - embed.add_field(name="Date", value=note.date_display) embed.add_field(name="Player", value=note.player.username.replace("_", "\\_")) embed.add_field(name="Importance", value=note.importance_display) embed.add_field(name="Message", value=note.message) - embed.add_field(name="Link", value=link) + embed.add_field(name="Link", value=link, inline=False) return embed def validate_username(username): - response = requests.get("https://api.mojang.com/users/profiles/minecraft/{}".format(username)) + response = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{username}") if response.status_code == 200: return True return False @@ -137,4 +124,4 @@ class Captcha: def add_error(self, error): if error not in self.errors: - self.errors.append(error) \ No newline at end of file + self.errors.append(error)