From c9b8e23c61bf4a36c30b587a27a76696e1a8f1ce Mon Sep 17 00:00:00 2001 From: Etzelia Date: Tue, 20 Jul 2021 18:31:07 -0500 Subject: [PATCH 1/4] 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): -- 2.41.0 From f737fc50dfad7ecbf3ea24824ba1348c40b06a8b Mon Sep 17 00:00:00 2001 From: Etzelia Date: Tue, 20 Jul 2021 18:32:18 -0500 Subject: [PATCH 2/4] Optimize imports Signed-off-by: Etzelia --- bot/commands.py | 2 +- bot/discord.py | 2 +- models.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/commands.py b/bot/commands.py index 15902b6..9c7d1a7 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -5,8 +5,8 @@ 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): diff --git a/bot/discord.py b/bot/discord.py index db37568..ffe08bc 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, Player +from minecraft_manager.models import Application, Ticket from minecraft_manager.utils import full_reverse logger = logging.getLogger(__name__) diff --git a/models.py b/models.py index 3957297..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 -- 2.41.0 From 0749dd3107c7f84b120a79c788ef9f2f1d11c824 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Tue, 20 Jul 2021 21:35:28 -0500 Subject: [PATCH 3/4] UAT Signed-off-by: Etzelia --- bot/commands.py | 18 ++++++++++-------- bot/discord.py | 7 ++++--- templates/minecraft_manager/player_info.html | 1 + 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/bot/commands.py b/bot/commands.py index 9c7d1a7..693d496 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -1,4 +1,5 @@ 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 @@ -262,7 +263,7 @@ class Commands(commands.Cog): @commands.command() async def sync(self, ctx): def sync_player(player): - for member in ctx.get_guild(self.bot.sync_guild).members: + 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 @@ -270,18 +271,19 @@ class Commands(commands.Cog): 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 + synced = [] + not_synced = [] 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)) - + 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): diff --git a/bot/discord.py b/bot/discord.py index ffe08bc..4895dc1 100644 --- a/bot/discord.py +++ b/bot/discord.py @@ -22,11 +22,12 @@ 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): + 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") @@ -92,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/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 From 66cf464f3d13292d5e9fb51a87a652378ea7907f Mon Sep 17 00:00:00 2001 From: Etzelia Date: Mon, 26 Jul 2021 15:23:37 +0000 Subject: [PATCH 4/4] 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