minecraft_manager/utils.py

140 lines
5.7 KiB
Python
Raw Normal View History

import discord
import requests
2018-09-20 02:56:17 +00:00
from django.conf import settings
from django.shortcuts import reverse
from django.templatetags.static import static
from minecraft_manager.models import Player, Application
def resolve_player(username):
if Player.objects.filter(username__iexact=username).exists():
return Player.objects.get(username__iexact=username)
if Application.objects.filter(username__iexact=username).exists():
application = Application.objects.get(username__iexact=username)
if Player.objects.filter(application=application).exists():
return Player.objects.get(application=application)
return None
def resolve_application(username):
if Application.objects.filter(username__iexact=username).exists():
return Application.objects.get(username__iexact=username)
if Player.objects.filter(username__iexact=username, application__isnull=False).exists():
player = Player.objects.get(username__iexact=username)
return player.application
return None
2018-09-20 02:56:17 +00:00
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))
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)
embed.add_field(name="Type of Player", value=application.player_type)
embed.add_field(name="Ever been banned", value=application.ever_banned)
attachments (#2) Add migration for longer timezone Signed-off-by: Etzelia <etzelia@hotmail.com> Merge branch 'master' of birbmc.com:BirbMC/minecraft_manager into birb # Conflicts: # external/views.py Fix col Signed-off-by: Etzelia <etzelia@hotmail.com> Add attachments, clean up UI, etc. Signed-off-by: Etzelia <etzelia@hotmail.com> Fix Discord embeds (#59) Fix Discord embeds Reviewed-on: https://git.etztech.xyz/MMS/MinecraftManagerDjango/pulls/59 Reviewed-by: ZeroHD <joey@ahines.net> Add requirements (not txt) (#57) Add requirements (not txt) Signed-off-by: Etzelia <etzelia@hotmail.com> Reviewed-on: https://git.etztech.xyz/MMS/MinecraftManagerDjango/pulls/57 Reviewed-by: ZeroHD <joey@ahines.net> Interim Patch (#54) Birb Patches (#1) Birb Patches Signed-off-by: Etzelia <etzelia@hotmail.com> Co-authored-by: Etzelia <etzelia@hotmail.com> Reviewed-by: ZeroHD <joey@ahines.net> Add 'LICENSE' (#53) Add 'LICENSE' Reviewed-by: ZeroHD <joey@ahines.net> Fix captcha and generify community invite (#52) Add validation for final question and some minor CSS Signed-off-by: Etzelia <etzelia@hotmail.com> Change some docs Signed-off-by: Etzelia <etzelia@hotmail.com> Fix captcha and generify community invite Signed-off-by: Etzelia <etzelia@hotmail.com> Co-authored-by: Etzelia <etzelia@hotmail.com> Reviewed-by: ZeroHD <joey@ahines.net> Co-authored-by: Etz Elia <etzelia@hotmail.com> Reviewed-on: https://git.birbmc.com/BirbMC/minecraft_manager/pulls/2 Co-Authored-By: Etzelia <etzelia@hotmail.com> Co-Committed-By: Etzelia <etzelia@hotmail.com>
2021-05-06 17:50:18 +00:00
if application.ever_banned and application.ever_banned_explanation:
2018-09-20 02:56:17 +00:00
embed.add_field(name="Reason for being banned", value=application.ever_banned_explanation)
attachments (#2) Add migration for longer timezone Signed-off-by: Etzelia <etzelia@hotmail.com> Merge branch 'master' of birbmc.com:BirbMC/minecraft_manager into birb # Conflicts: # external/views.py Fix col Signed-off-by: Etzelia <etzelia@hotmail.com> Add attachments, clean up UI, etc. Signed-off-by: Etzelia <etzelia@hotmail.com> Fix Discord embeds (#59) Fix Discord embeds Reviewed-on: https://git.etztech.xyz/MMS/MinecraftManagerDjango/pulls/59 Reviewed-by: ZeroHD <joey@ahines.net> Add requirements (not txt) (#57) Add requirements (not txt) Signed-off-by: Etzelia <etzelia@hotmail.com> Reviewed-on: https://git.etztech.xyz/MMS/MinecraftManagerDjango/pulls/57 Reviewed-by: ZeroHD <joey@ahines.net> Interim Patch (#54) Birb Patches (#1) Birb Patches Signed-off-by: Etzelia <etzelia@hotmail.com> Co-authored-by: Etzelia <etzelia@hotmail.com> Reviewed-by: ZeroHD <joey@ahines.net> Add 'LICENSE' (#53) Add 'LICENSE' Reviewed-by: ZeroHD <joey@ahines.net> Fix captcha and generify community invite (#52) Add validation for final question and some minor CSS Signed-off-by: Etzelia <etzelia@hotmail.com> Change some docs Signed-off-by: Etzelia <etzelia@hotmail.com> Fix captcha and generify community invite Signed-off-by: Etzelia <etzelia@hotmail.com> Co-authored-by: Etzelia <etzelia@hotmail.com> Reviewed-by: ZeroHD <joey@ahines.net> Co-authored-by: Etz Elia <etzelia@hotmail.com> Reviewed-on: https://git.birbmc.com/BirbMC/minecraft_manager/pulls/2 Co-Authored-By: Etzelia <etzelia@hotmail.com> Co-Committed-By: Etzelia <etzelia@hotmail.com>
2021-05-06 17:50:18 +00:00
if application.reference:
embed.add_field(name="Reference", value=application.reference)
2018-09-20 02:56:17 +00:00
embed.add_field(name="Read the Rules", value=application.read_rules)
embed.timestamp = application.date
2018-09-20 02:56:17 +00:00
embed.add_field(name="Status", value=application.status)
embed.add_field(name="Link", value=full_reverse('application_info', application.id))
2019-08-30 18:03:12 +00:00
return embed
2018-09-20 02:56:17 +00:00
def build_ticket(ticket, link):
embed = discord.Embed(colour=discord.Colour(0x417505))
embed.title = "Ticket"
embed.set_thumbnail(url=full_static("favicon.png"))
embed.timestamp = ticket.date
2018-09-20 02:56:17 +00:00
embed.add_field(name="Player", value=ticket.player.username.replace("_", "\\_"))
embed.add_field(name="Priority", value=ticket.priority_display)
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)
2019-08-30 18:03:12 +00:00
return embed
2018-09-20 02:56:17 +00:00
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
2018-09-20 02:56:17 +00:00
embed.add_field(name="Player", value=warning.player.username.replace("_", "\\_"))
2018-12-18 16:35:44 +00:00
embed.add_field(name="Importance", value=warning.importance_display)
2018-09-20 02:56:17 +00:00
embed.add_field(name="Message", value=warning.message)
embed.add_field(name="Link", value=link)
2019-08-30 18:03:12 +00:00
return embed
2018-09-20 02:56:17 +00:00
def build_note(note, link):
embed = discord.Embed(colour=discord.Colour(0x417505))
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)
return embed
2018-09-20 02:56:17 +00:00
def validate_username(username):
response = requests.get("https://api.mojang.com/users/profiles/minecraft/{}".format(username))
if response.status_code == 200:
return True
return False
def full_reverse(viewname, *args):
base = settings.MCM_DOMAIN.rstrip('/')
view = reverse(viewname, args=args)
return f"{base}{view}"
def full_static(assetname):
base = settings.MCM_DOMAIN.rstrip('/')
asset = static(assetname)
return f"{base}{asset}"
2018-09-20 02:56:17 +00:00
class Captcha:
success = False
challenge_ts = None
hostname = None
errors = []
def __init__(self, post):
if not hasattr(settings, 'CAPTCHA_SECRET'):
self.success = True
if 'g-recaptcha-response' in post:
response = requests.post("https://www.google.com/recaptcha/api/siteverify",
{"secret": settings.CAPTCHA_SECRET, "response": post['g-recaptcha-response']})
json = response.json()
self.success = json['success']
self.challenge_ts = json['challenge_ts'] if 'challenge_ts' in json else None
self.hostname = json['hostname'] if 'hostname' in json else None
if 'error-codes' in json:
codes = json['error-codes']
if 'missing-input-secret' in codes:
self.add_error('The secret parameter is missing.')
if 'invalid-input-secret' in codes:
self.add_error('The secret parameter is invalid or malformed.')
if 'missing-input-response' in codes:
self.add_error('The reCAPTCHA must be filled out.')
if 'invalid-input-response' in codes:
self.add_error('The response parameter is invalid or malformed.')
if 'bad-request' in codes:
self.add_error('The request is invalid or malformed.')
def add_error(self, error):
if error not in self.errors:
self.errors.append(error)