import discord import requests 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 def build_application(application): embed = discord.Embed(colour=discord.Colour(0x417505)) embed.title = "Application" embed.set_thumbnail( 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) 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 and application.ever_banned_explanation: embed.add_field(name="Reason for being banned", value=application.ever_banned_explanation) if application.reference: embed.add_field(name="Reference", value=application.reference) 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), inline=False) return embed 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 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, inline=False) return embed 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) embed.add_field(name="Link", value=link, inline=False) return embed def validate_username(username): response = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{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}" 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)