From 66cf464f3d13292d5e9fb51a87a652378ea7907f Mon Sep 17 00:00:00 2001 From: Etzelia Date: Mon, 26 Jul 2021 15:23:37 +0000 Subject: [PATCH] Add discord ID and sync (#17) UAT Signed-off-by: Etzelia Optimize imports Signed-off-by: Etzelia Add discord ID and sync Signed-off-by: Etzelia Reviewed-on: https://git.canopymc.net/Canopy/minecraft_manager/pulls/17 Reviewed-by: ZeroHD Co-Authored-By: Etzelia Co-Committed-By: Etzelia --- bot/commands.py | 29 +++++++++++++++++++- bot/discord.py | 6 ++-- migrations/0017_player_discord_id.py | 18 ++++++++++++ models.py | 6 ++-- templates/minecraft_manager/player_info.html | 1 + 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 migrations/0017_player_discord_id.py diff --git a/bot/commands.py b/bot/commands.py index 28eb4f3..693d496 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -1,12 +1,13 @@ import discord +from io import StringIO from discord.ext import commands from django.contrib.auth.models import User from django.db import close_old_connections from minecraft_manager.api import api from minecraft_manager.bot.utils import get_application -from minecraft_manager.utils import build_application, full_static from minecraft_manager.models import Application, Player +from minecraft_manager.utils import build_application, full_static class Commands(commands.Cog): @@ -54,6 +55,7 @@ class Commands(commands.Cog): embed.add_field(name="{}demote ".format(self.bot.prefix), value="Demote a player to the role given to accepted applications.") embed.add_field(name="{}compare".format(self.bot.prefix), value="Compare Discord users to the Whitelist.") + embed.add_field(name="{}sync".format(self.bot.prefix), value="Sync Discord users with Player records.") await self.bot.discord_message(ctx.message.channel, embed) @commands.group("app", aliases=["application"]) @@ -258,6 +260,31 @@ class Commands(commands.Cog): header = "**The following users do not have an application or player match on the whitelist:**\n" await self.bot.discord_message(ctx.author, "{}```{}```".format(header, "\n".join(no_application))) + @commands.command() + async def sync(self, ctx): + def sync_player(player): + for member in ctx.guild.members: + name = member.nick if member.nick else member.name + if player.username.lower() == name.lower(): + player.discord_id = member.id + player.save() + return True + return False + # Attempt to sync users + need_sync = Player.objects.filter(discord_id__isnull=True) + need_sync_count = need_sync.count() + synced = [] + not_synced = [] + if need_sync_count > 0: + for ns in need_sync: + if sync_player(ns): + synced.append(ns.username) + else: + not_synced.append(ns.username) + txt = "Synced\n\t{}\n\nNot Synced\n\t{}".format('\n\t'.join(synced), '\n\t'.join(not_synced)) + attach = discord.File(fp=StringIO(txt), filename="sync.txt") + await ctx.channel.send(content="Successfully synced {}/{} players.".format(len(synced), need_sync_count), file=attach) + def setup(bot): bot.add_cog(Commands(bot)) diff --git a/bot/discord.py b/bot/discord.py index 08de90d..4895dc1 100644 --- a/bot/discord.py +++ b/bot/discord.py @@ -24,8 +24,10 @@ class Discord(commands.Bot): error_users = getattr(settings, 'DISCORD_ERROR_USERS', []) def __init__(self, token): + intents = discord.Intents.default() + intents.members = True super().__init__(command_prefix=self.prefix, description=description, case_insensitive=True, help_command=None, - activity=discord.Game(name=self.discord_game)) + activity=discord.Game(name=self.discord_game), intents=intents) self.token = token self.load_extension("minecraft_manager.bot.commands") @@ -91,5 +93,5 @@ class Discord(commands.Bot): print(e) logger.info('Bot encountered the following unhandled exception %s', e) finally: - loop.run_until_complete(self.logout()) + loop.run_until_complete(self.close()) logger.info("Bot shutting down...") diff --git a/migrations/0017_player_discord_id.py b/migrations/0017_player_discord_id.py new file mode 100644 index 0000000..0cbc657 --- /dev/null +++ b/migrations/0017_player_discord_id.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.3 on 2021-07-20 23:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('minecraft_manager', '0016_auto_20210328_0131'), + ] + + operations = [ + migrations.AddField( + model_name='player', + name='discord_id', + field=models.CharField(blank=True, max_length=30, null=True, unique=True), + ), + ] diff --git a/models.py b/models.py index c3246ad..4fc573a 100644 --- a/models.py +++ b/models.py @@ -1,11 +1,11 @@ import json import logging import os -import pytz -import yaml from datetime import datetime from os.path import basename +import pytz +import yaml from django.conf import settings from django.contrib.auth.models import User from django.db import models @@ -105,7 +105,7 @@ class Player(models.Model): application = models.ForeignKey(Application, on_delete=models.SET_NULL, null=True, blank=True) first_seen = models.DateField(null=True, blank=True) last_seen = models.DateField(null=True, blank=True) - + discord_id = models.CharField(max_length=30, unique=True, null=True, blank=True) @property def is_banned(self): diff --git a/templates/minecraft_manager/player_info.html b/templates/minecraft_manager/player_info.html index b9dd13d..3ca0bfe 100644 --- a/templates/minecraft_manager/player_info.html +++ b/templates/minecraft_manager/player_info.html @@ -7,6 +7,7 @@

UUID: {{ player.uuid }}

+

Discord ID: {% if player.discord_id %}{{ player.discord_id }} {% else %}N/A{% endif %}

{% if player.auth_user %}

Connected User: {{ player.auth_user.username }}

{% endif %}