From c9b8e23c61bf4a36c30b587a27a76696e1a8f1ce Mon Sep 17 00:00:00 2001 From: Etzelia Date: Tue, 20 Jul 2021 18:31:07 -0500 Subject: [PATCH] Add discord ID and sync Signed-off-by: Etzelia --- bot/commands.py | 25 +++++++++++++++++++++++++ bot/discord.py | 3 ++- migrations/0017_player_discord_id.py | 18 ++++++++++++++++++ models.py | 2 +- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 migrations/0017_player_discord_id.py diff --git a/bot/commands.py b/bot/commands.py index 28eb4f3..15902b6 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -54,6 +54,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 +259,30 @@ 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.get_guild(self.bot.sync_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 + if not self.bot.sync_guild: + self.bot.discord_message(ctx.author, "DISCORD_SYNC_GUILD must be defined to sync players to Discord") + return + need_sync = Player.objects.filter(discord_id__isnull=True) + need_sync_count = need_sync.count() + synced = 0 + if need_sync_count > 0: + for ns in need_sync: + if sync_player(ns): + synced += 1 + self.bot.discord_message(ctx.author, "Successfully synced {}/{} players.".format(synced, need_sync_count)) + + def setup(bot): bot.add_cog(Commands(bot)) diff --git a/bot/discord.py b/bot/discord.py index 08de90d..db37568 100644 --- a/bot/discord.py +++ b/bot/discord.py @@ -6,7 +6,7 @@ import discord from discord.ext import commands from django.conf import settings -from minecraft_manager.models import Application, Ticket +from minecraft_manager.models import Application, Ticket, Player from minecraft_manager.utils import full_reverse logger = logging.getLogger(__name__) @@ -22,6 +22,7 @@ class Discord(commands.Bot): auth_roles = getattr(settings, 'DISCORD_BOT_ROLES', []) superuser_roles = getattr(settings, 'DISCORD_SUPERUSER_ROLES', []) error_users = getattr(settings, 'DISCORD_ERROR_USERS', []) + sync_guild = getattr(settings, 'DISCORD_SYNC_GUILD', '') def __init__(self, token): super().__init__(command_prefix=self.prefix, description=description, case_insensitive=True, help_command=None, 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..3957297 100644 --- a/models.py +++ b/models.py @@ -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):