diff --git a/api/bot.py b/api/bot.py index b9825ce..ca9881c 100644 --- a/api/bot.py +++ b/api/bot.py @@ -15,6 +15,7 @@ class Discord(discord.Client): prefix = getattr(settings, 'DISCORD_BOT_PREFIX', '!') auth_roles = getattr(settings, 'DISCORD_BOT_ROLES', []) error_users = getattr(settings, 'DISCORD_ERROR_USERS', []) + new_member_roles = getattr(settings, 'DISCORD_BOT_NEW_MEMBER_ROLES', []) token = None def __init__(self, token, **kwargs): @@ -52,19 +53,61 @@ class Discord(discord.Client): member_roles = [role.id for role in message.author.roles] + # FIX STALE DB CONNECTIONS + close_old_connections() + + # IF NOT A MEMBER YET + if len(member_roles) == 1: + # REGISTER + match = re.match("[{0}]register (\S+)?$".format(self.prefix), message.content) + if match: + search = match.group(1) + count = Application.objects.filter(username__iexact=search, accepted=True).count() + + if count == 0: + count = Player.objects.filter(username__iexact=search, application__accepted=True).count() + + if count > 0: + if count == 1: + player = Player.objects.filter(username__iexact=search, application__accepted=True).all()[0] + nickname = player.username + + if not player.is_banned: + member = discord.utils.get(message.server.members, display_name=nickname) + + if member is not None and member is not message.author: + msg = "{0}, a member with that name is already exists, please contact the staff".format(message.author.mention) + yield from self.discord_message(message.channel, msg) + else: + for role_id in self.new_member_roles: + role = discord.utils.get(message.server.roles, id=role_id) + yield from self.add_roles(message.author, role) + + msg = "Successfully added {0} as a member".format(nickname) + + yield from self.change_nickname(message.author, nickname) + yield from self.discord_message(message.channel, msg) + else: + msg = "{0} You are currently banned.".format(message.author.mention) + yield from self.discord_message(message.channel, msg) + + return + else: + msg = "{0}, an application for {1} could not be found, please check your username and make sure you have applied.".format(message.author.mention, search) + yield from self.discord_message(message.channel, msg) + return + # IF MEMBER IS NOT AUTHORIZED, IGNORE if not any(role in self.auth_roles for role in member_roles): return - # FIX STALE DB CONNECTIONS - close_old_connections() - # HELP match = re.match("[{0}]help$".format(self.prefix), message.content) if match: embed = discord.Embed(colour=discord.Colour(0x417505)) embed.set_thumbnail(url="https://cdn.discordapp.com/avatars/454457830918062081/b5792489bc43d9e17b8f657880a17dd4.png") embed.add_field(name="Minecraft Manager Help", value="-----------------------------") + embed.add_field(name="{}register ".format(self.prefix), value="Allows new members to join the Discord server if they have applied and been accepted.") embed.add_field(name="{}[app ]search ".format(self.prefix), value="Search for applications by partial or exact username.") embed.add_field(name="{}[app ]info ".format(self.prefix), value="Get detailed information about a specific application.") embed.add_field(name="{}[app ]accept|deny ".format(self.prefix), value="Take action on an application.") diff --git a/docs/source/django-settings.rst b/docs/source/django-settings.rst index 495131d..a7ef2fb 100644 --- a/docs/source/django-settings.rst +++ b/docs/source/django-settings.rst @@ -57,6 +57,8 @@ Optional ``DISCORD_BOT_ROLES`` - A list of Discord Roles allowed to use the bot. If this list is empty, no one can use the bot! +``DISCORD_BOT_NEW_MEMBER_ROLES`` - A list of Discord Roles to give new players when they register. + ``CAPTCHA_SECRET`` - Your secret key used for reCAPTCHA ``STATS_FILTER`` - A python list of partial strings used to filter out stats. e.g. ``['broken', 'dropped', 'picked_up']`` to filter out broken, dropped and picked up stats \ No newline at end of file