parent
f737fc50df
commit
0749dd3107
|
@ -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):
|
||||
|
|
|
@ -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...")
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
<div class="row">
|
||||
<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>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 %}
|
||||
<p>Connected User: {{ player.auth_user.username }}</p>
|
||||
{% endif %}
|
||||
|
|
Loading…
Reference in New Issue