Geoffrey-Django/GeoffreyApp/management/commands/update_mc_username.py

29 lines
1.0 KiB
Python
Raw Normal View History

from django.core.management.base import BaseCommand, CommandError
from GeoffreyApp.models import Player
from GeoffreyApp.minecraft_api import grab_playername
from GeoffreyApp.errors import UsernameLookupFailed
import logging
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = "Updates all the minecraft usernames in the database"
def handle(self, *args, **options):
players = Player.objects.all()
for player in players:
try:
mc_username = grab_playername(player.mc_uuid)
if player.name != mc_username:
player.name = mc_username
player.save()
except UsernameLookupFailed:
logger.error("Error updating MC UUID %s" % player.mc_uuid)
except Exception as e:
error_str = "Error updating MC usernames: %s" % e.__str__()
logger.error(error_str)
raise CommandError(error_str)
self.stdout.write("Done updating MC usernames")