forked from Minecraft/minecraft_manager
Add discord ID and sync (#17)
UAT Signed-off-by: Etzelia <etzelia@hotmail.com> Optimize imports Signed-off-by: Etzelia <etzelia@hotmail.com> Add discord ID and sync Signed-off-by: Etzelia <etzelia@hotmail.com> Reviewed-on: https://git.canopymc.net/Canopy/minecraft_manager/pulls/17 Reviewed-by: ZeroHD <joey@ahines.net> Co-Authored-By: Etzelia <etzelia@hotmail.com> Co-Committed-By: Etzelia <etzelia@hotmail.com>roll
parent
03608edfae
commit
66cf464f3d
|
@ -1,12 +1,13 @@
|
||||||
import discord
|
import discord
|
||||||
|
from io import StringIO
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db import close_old_connections
|
from django.db import close_old_connections
|
||||||
|
|
||||||
from minecraft_manager.api import api
|
from minecraft_manager.api import api
|
||||||
from minecraft_manager.bot.utils import get_application
|
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.models import Application, Player
|
||||||
|
from minecraft_manager.utils import build_application, full_static
|
||||||
|
|
||||||
|
|
||||||
class Commands(commands.Cog):
|
class Commands(commands.Cog):
|
||||||
|
@ -54,6 +55,7 @@ class Commands(commands.Cog):
|
||||||
embed.add_field(name="{}demote <username>".format(self.bot.prefix),
|
embed.add_field(name="{}demote <username>".format(self.bot.prefix),
|
||||||
value="Demote a player to the role given to accepted applications.")
|
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="{}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)
|
await self.bot.discord_message(ctx.message.channel, embed)
|
||||||
|
|
||||||
@commands.group("app", aliases=["application"])
|
@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"
|
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)))
|
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):
|
def setup(bot):
|
||||||
bot.add_cog(Commands(bot))
|
bot.add_cog(Commands(bot))
|
||||||
|
|
|
@ -24,8 +24,10 @@ class Discord(commands.Bot):
|
||||||
error_users = getattr(settings, 'DISCORD_ERROR_USERS', [])
|
error_users = getattr(settings, 'DISCORD_ERROR_USERS', [])
|
||||||
|
|
||||||
def __init__(self, token):
|
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,
|
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.token = token
|
||||||
self.load_extension("minecraft_manager.bot.commands")
|
self.load_extension("minecraft_manager.bot.commands")
|
||||||
|
|
||||||
|
@ -91,5 +93,5 @@ class Discord(commands.Bot):
|
||||||
print(e)
|
print(e)
|
||||||
logger.info('Bot encountered the following unhandled exception %s', e)
|
logger.info('Bot encountered the following unhandled exception %s', e)
|
||||||
finally:
|
finally:
|
||||||
loop.run_until_complete(self.logout())
|
loop.run_until_complete(self.close())
|
||||||
logger.info("Bot shutting down...")
|
logger.info("Bot shutting down...")
|
||||||
|
|
|
@ -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),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,11 +1,11 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import pytz
|
|
||||||
import yaml
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from os.path import basename
|
from os.path import basename
|
||||||
|
|
||||||
|
import pytz
|
||||||
|
import yaml
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db import models
|
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)
|
application = models.ForeignKey(Application, on_delete=models.SET_NULL, null=True, blank=True)
|
||||||
first_seen = models.DateField(null=True, blank=True)
|
first_seen = models.DateField(null=True, blank=True)
|
||||||
last_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
|
@property
|
||||||
def is_banned(self):
|
def is_banned(self):
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-xs-9 col-md-6">
|
<div class="col-xs-9 col-md-6">
|
||||||
<p>UUID: <code>{{ player.uuid }}</code> <button data-copy="{{ player.uuid }}" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-copy"></i></button></p>
|
<p>UUID: <code>{{ player.uuid }}</code> <button data-copy="{{ player.uuid }}" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-copy"></i></button></p>
|
||||||
|
<p>Discord ID: {% if player.discord_id %}<code>{{ player.discord_id }}</code> <button data-copy="{{ player.discord_id }}" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-copy"></i></button>{% else %}N/A{% endif %}</p>
|
||||||
{% if player.auth_user %}
|
{% if player.auth_user %}
|
||||||
<p>Connected User: {{ player.auth_user.username }}</p>
|
<p>Connected User: {{ player.auth_user.username }}</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
Loading…
Reference in New Issue