diff --git a/apps.py b/apps.py index cc3695e..a5fc31c 100644 --- a/apps.py +++ b/apps.py @@ -1,7 +1,12 @@ 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/bot/commands.py b/bot/commands.py index 693d496..28eb4f3 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -1,13 +1,12 @@ 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.models import Application, Player from minecraft_manager.utils import build_application, full_static +from minecraft_manager.models import Application, Player class Commands(commands.Cog): @@ -55,7 +54,6 @@ 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"]) @@ -260,31 +258,6 @@ 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 4895dc1..08de90d 100644 --- a/bot/discord.py +++ b/bot/discord.py @@ -24,10 +24,8 @@ 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), intents=intents) + activity=discord.Game(name=self.discord_game)) self.token = token self.load_extension("minecraft_manager.bot.commands") @@ -93,5 +91,5 @@ class Discord(commands.Bot): print(e) logger.info('Bot encountered the following unhandled exception %s', e) finally: - loop.run_until_complete(self.close()) + loop.run_until_complete(self.logout()) logger.info("Bot shutting down...") diff --git a/migrations/0017_player_discord_id.py b/migrations/0017_player_discord_id.py deleted file mode 100644 index 0cbc657..0000000 --- a/migrations/0017_player_discord_id.py +++ /dev/null @@ -1,18 +0,0 @@ -# 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 4fc573a..c447198 100644 --- a/models.py +++ b/models.py @@ -1,17 +1,10 @@ -import json -import logging -import os -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 +from django.contrib.auth.models import User from django.db.models import Q -from django.db.models.signals import pre_delete -from django.dispatch import receiver +from os.path import basename +import logging, yaml, pytz, json, os +from django.conf import settings +from datetime import datetime logger = logging.getLogger(__name__) @@ -105,7 +98,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): @@ -383,11 +376,6 @@ 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 new file mode 100644 index 0000000..e69de29 diff --git a/signals/pre_delete.py b/signals/pre_delete.py new file mode 100644 index 0000000..cb46ea4 --- /dev/null +++ b/signals/pre_delete.py @@ -0,0 +1,2 @@ +def attachment_delete(sender, instance, **kwargs): + instance.file.delete(False) diff --git a/templates/minecraft_manager/player_info.html b/templates/minecraft_manager/player_info.html index 3ca0bfe..b9dd13d 100644 --- a/templates/minecraft_manager/player_info.html +++ b/templates/minecraft_manager/player_info.html @@ -7,7 +7,6 @@

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 %}