diff --git a/api/bot.py b/api/bot.py index f242ad5..f3cf56c 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', []) + member_role = getattr(settings, 'DISCORD_BOT_MEMBER_ROLE', 'Member') token = None def __init__(self, token, **kwargs): @@ -52,13 +53,38 @@ 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__exact=True).count() + + if count > 0: + if count == 1: + player = Application.objects.filter(username__iexact=search, accepted__exact=True).all().values()[0] + nickname = player["username"] + + role = discord.utils.get(message.server.roles, name=self.member_role) + msg = "Successfully added {0} as a {1}.".format(nickname, self.member_role) + + yield from self.change_nickname(message.author, nickname) + yield from self.add_roles(message.author, role) + yield from self.discord_message(message.channel, msg) + + return + else: + msg = "An application for {0} could not be found, please apply first.".format(search) + yield from self.discord_message(message.channel, msg) + # 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: @@ -305,8 +331,5 @@ class OreAlert: self.playerList.append(p) except KeyboardInterrupt: api.discord_notification("OreAlert has been stopped manually.") - except Exception as e: - logger.exception("OreAlert has crashed") - api.discord_notification("OreAlert has crashed!", ping=True)