2021-07-01 03:07:45 +00:00
|
|
|
import discord
|
|
|
|
import requests
|
2018-09-20 02:56:17 +00:00
|
|
|
from django.conf import settings
|
2021-07-01 03:07:45 +00:00
|
|
|
from django.shortcuts import reverse
|
|
|
|
from django.templatetags.static import static
|
|
|
|
|
2018-12-13 22:28:33 +00:00
|
|
|
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(
|
2022-02-15 04:21:02 +00:00
|
|
|
url=f"https://minotar.net/helm/{application.username}/100.png")
|
2018-09-20 02:56:17 +00:00
|
|
|
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)
|
2022-02-15 03:12:23 +00:00
|
|
|
embed.add_field(name="Favorite Activity", value=application.player_type)
|
2018-09-20 02:56:17 +00:00
|
|
|
embed.add_field(name="Ever been banned", value=application.ever_banned)
|
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)
|
2021-05-06 17:50:18 +00:00
|
|
|
if application.reference:
|
2022-02-15 03:12:23 +00:00
|
|
|
embed.add_field(name="Referral", value=application.reference)
|
2018-09-20 02:56:17 +00:00
|
|
|
embed.add_field(name="Read the Rules", value=application.read_rules)
|
2021-07-01 03:07:45 +00:00
|
|
|
embed.timestamp = application.date
|
2018-09-20 02:56:17 +00:00
|
|
|
embed.add_field(name="Status", value=application.status)
|
2022-02-15 04:21:02 +00:00
|
|
|
embed.add_field(name="Link", value=full_reverse('application_info', application.id), inline=False)
|
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"
|
2021-07-01 03:07:45 +00:00
|
|
|
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)
|
2022-02-15 04:21:02 +00:00
|
|
|
embed.add_field(name="Link", value=link, inline=False)
|
2019-08-30 18:03:12 +00:00
|
|
|
return embed
|
2018-09-20 02:56:17 +00:00
|
|
|
|
|
|
|
|
2021-07-01 03:07:45 +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="Player", value=note.player.username.replace("_", "\\_"))
|
|
|
|
embed.add_field(name="Importance", value=note.importance_display)
|
|
|
|
embed.add_field(name="Message", value=note.message)
|
2022-02-15 04:21:02 +00:00
|
|
|
embed.add_field(name="Link", value=link, inline=False)
|
2021-07-01 03:07:45 +00:00
|
|
|
return embed
|
|
|
|
|
|
|
|
|
2018-09-20 02:56:17 +00:00
|
|
|
def validate_username(username):
|
2022-02-15 04:21:02 +00:00
|
|
|
response = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{username}")
|
2018-09-20 02:56:17 +00:00
|
|
|
if response.status_code == 200:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-07-01 03:07:45 +00:00
|
|
|
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:
|
2022-02-15 04:21:02 +00:00
|
|
|
self.errors.append(error)
|