From 817d2e016e4fd3c25d5586cfb35725475b2b8a6b Mon Sep 17 00:00:00 2001 From: Etzelia Date: Fri, 16 Jul 2021 20:47:57 -0500 Subject: [PATCH 1/3] Fix receiver for pre_delete Signed-off-by: Etzelia --- apps.py | 5 ----- models.py | 24 ++++++++++++++++++------ signals/__init__.py | 0 signals/pre_delete.py | 2 -- 4 files changed, 18 insertions(+), 13 deletions(-) delete mode 100644 signals/__init__.py delete mode 100644 signals/pre_delete.py diff --git a/apps.py b/apps.py index a5fc31c..cc3695e 100644 --- a/apps.py +++ b/apps.py @@ -1,12 +1,7 @@ from django.apps import AppConfig -from django.db.models.signals import pre_delete -from minecraft_manager.signals.pre_delete import attachment_delete class MinecraftManagerAppConfig(AppConfig): name = 'minecraft_manager' verbose_name = "Minecraft Manager" - def ready(self): - pre_delete.connect(attachment_delete) - diff --git a/models.py b/models.py index c447198..c3246ad 100644 --- a/models.py +++ b/models.py @@ -1,10 +1,17 @@ -from django.db import models -from django.contrib.auth.models import User -from django.db.models import Q -from os.path import basename -import logging, yaml, pytz, json, os -from django.conf import settings +import json +import logging +import os +import pytz +import yaml from datetime import datetime +from os.path import basename + +from django.conf import settings +from django.contrib.auth.models import User +from django.db import models +from django.db.models import Q +from django.db.models.signals import pre_delete +from django.dispatch import receiver logger = logging.getLogger(__name__) @@ -376,6 +383,11 @@ class Attachment(models.Model): return self.file.name +@receiver(pre_delete, sender=Attachment, dispatch_uid="delete_attachments") +def attachment_delete(sender, instance, **kwargs): + instance.file.delete(False) + + class IPManager(models.Manager): def get_queryset(self): users = User.objects.filter(is_active=True) diff --git a/signals/__init__.py b/signals/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/signals/pre_delete.py b/signals/pre_delete.py deleted file mode 100644 index cb46ea4..0000000 --- a/signals/pre_delete.py +++ /dev/null @@ -1,2 +0,0 @@ -def attachment_delete(sender, instance, **kwargs): - instance.file.delete(False) -- 2.41.0 From 03608edfae773ee2cc07cd84593a5b611ab2f363 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Sat, 17 Jul 2021 01:55:45 +0000 Subject: [PATCH 2/3] Fix receiver for pre_delete (#12) Fix receiver for pre_delete Signed-off-by: Etzelia Reviewed-on: https://git.canopymc.net/Canopy/minecraft_manager/pulls/12 Co-Authored-By: Etzelia Co-Committed-By: Etzelia --- apps.py | 5 ----- models.py | 24 ++++++++++++++++++------ signals/__init__.py | 0 signals/pre_delete.py | 2 -- 4 files changed, 18 insertions(+), 13 deletions(-) delete mode 100644 signals/__init__.py delete mode 100644 signals/pre_delete.py diff --git a/apps.py b/apps.py index a5fc31c..cc3695e 100644 --- a/apps.py +++ b/apps.py @@ -1,12 +1,7 @@ from django.apps import AppConfig -from django.db.models.signals import pre_delete -from minecraft_manager.signals.pre_delete import attachment_delete class MinecraftManagerAppConfig(AppConfig): name = 'minecraft_manager' verbose_name = "Minecraft Manager" - def ready(self): - pre_delete.connect(attachment_delete) - diff --git a/models.py b/models.py index c447198..c3246ad 100644 --- a/models.py +++ b/models.py @@ -1,10 +1,17 @@ -from django.db import models -from django.contrib.auth.models import User -from django.db.models import Q -from os.path import basename -import logging, yaml, pytz, json, os -from django.conf import settings +import json +import logging +import os +import pytz +import yaml from datetime import datetime +from os.path import basename + +from django.conf import settings +from django.contrib.auth.models import User +from django.db import models +from django.db.models import Q +from django.db.models.signals import pre_delete +from django.dispatch import receiver logger = logging.getLogger(__name__) @@ -376,6 +383,11 @@ class Attachment(models.Model): return self.file.name +@receiver(pre_delete, sender=Attachment, dispatch_uid="delete_attachments") +def attachment_delete(sender, instance, **kwargs): + instance.file.delete(False) + + class IPManager(models.Manager): def get_queryset(self): users = User.objects.filter(is_active=True) diff --git a/signals/__init__.py b/signals/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/signals/pre_delete.py b/signals/pre_delete.py deleted file mode 100644 index cb46ea4..0000000 --- a/signals/pre_delete.py +++ /dev/null @@ -1,2 +0,0 @@ -def attachment_delete(sender, instance, **kwargs): - instance.file.delete(False) -- 2.41.0 From 66cf464f3d13292d5e9fb51a87a652378ea7907f Mon Sep 17 00:00:00 2001 From: Etzelia Date: Mon, 26 Jul 2021 15:23:37 +0000 Subject: [PATCH 3/3] 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 %} -- 2.41.0