Add discord ID and sync

Signed-off-by: Etzelia <etzelia@hotmail.com>
pull/17/head
Etzelia 2021-07-20 18:31:07 -05:00
parent 03608edfae
commit c9b8e23c61
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
4 changed files with 46 additions and 2 deletions

View File

@ -54,6 +54,7 @@ class Commands(commands.Cog):
embed.add_field(name="{}demote <username>".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))

View File

@ -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,

View File

@ -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),
),
]

View File

@ -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):