minecraft_manager/utils.py

123 lines
5.2 KiB
Python

import discord, requests
from django.conf import settings
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
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)
if application.ever_banned:
embed.add_field(name="Reason for being banned", value=application.ever_banned_explanation)
embed.add_field(name="Reference", value=application.reference)
embed.add_field(name="Read the Rules", value=application.read_rules)
embed.add_field(name="Date", value=application.date_display)
embed.add_field(name="Status", value=application.status)
return [embed.to_dict()]
def build_ticket(ticket, link):
embed = discord.Embed(colour=discord.Colour(0x417505))
embed.title = "Ticket"
embed.set_thumbnail(
url="https://cdn.discordapp.com/avatars/454457830918062081/b5792489bc43d9e17b8f657880a17dd4.png")
embed.add_field(name="Date", value=ticket.date_display)
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)
return [embed.to_dict()]
def build_warning(warning, link):
embed = discord.Embed(colour=discord.Colour(0x417505))
embed.title = "Warning"
embed.set_thumbnail(
url="https://cdn.discordapp.com/avatars/454457830918062081/b5792489bc43d9e17b8f657880a17dd4.png")
embed.add_field(name="Date", value=warning.date_display)
embed.add_field(name="Player", value=warning.player.username.replace("_", "\\_"))
embed.add_field(name="Severity", value=warning.severity_display)
embed.add_field(name="Message", value=warning.message)
embed.add_field(name="Link", value=link)
return [embed.to_dict()]
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 url_path(*args):
value = []
for arg in args:
arg = str(arg)
if arg.startswith('/'):
arg = arg[1:]
if arg.endswith('/'):
arg = arg[:-1]
value.append(arg)
return '/'.join(value)
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)