Added register command to the MCM Discord Bot

Register queries the applications for the user and applies the rank and the nickname if found
reminder
Joey Hines 2018-12-09 10:22:52 -06:00
parent 938ec65422
commit f80de5a85a
1 changed files with 29 additions and 6 deletions

View File

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