From d4b0a58b19fc243477bfc00f293a664a9c614603 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 21 Oct 2018 16:43:36 -0500 Subject: [PATCH 01/10] Added logging for OreAlert crashes. Hopefully this will help find the source of all the crashes. --- api/bot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/bot.py b/api/bot.py index 942bbc3..2a9e2f2 100644 --- a/api/bot.py +++ b/api/bot.py @@ -290,7 +290,8 @@ class OreAlert: self.playerList.append(p) except KeyboardInterrupt: api.discord_notification("OreAlert has been stopped manually.") - except: + except Exception as e: + logger.exception("OreAlert has crashed") api.discord_notification("OreAlert has crashed!", ping=True) From f80de5a85a5a8f48ddb17a3fa949bed6166a4072 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 9 Dec 2018 10:22:52 -0600 Subject: [PATCH 02/10] Added register command to the MCM Discord Bot Register queries the applications for the user and applies the rank and the nickname if found --- api/bot.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) 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) From 6a0d9d56e2cbcbc35e28ad79c52827a33dd57eab Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 9 Dec 2018 10:35:37 -0600 Subject: [PATCH 03/10] Reverted OreAlert changes --- api/bot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/bot.py b/api/bot.py index f3cf56c..df6a1e3 100644 --- a/api/bot.py +++ b/api/bot.py @@ -331,5 +331,7 @@ class OreAlert: self.playerList.append(p) except KeyboardInterrupt: api.discord_notification("OreAlert has been stopped manually.") + except: + api.discord_notification("OreAlert has crashed!", ping=True) From 03f5c87334758da51ae266eadb4e49ca530069be Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 10 Dec 2018 12:44:31 -0600 Subject: [PATCH 04/10] Added a check to register to check for username changes and if the player is banned If the username is not found in the applications, players is queried to find a matching player is_banned property is now checked before adding them to the server --- api/bot.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/api/bot.py b/api/bot.py index df6a1e3..9275d34 100644 --- a/api/bot.py +++ b/api/bot.py @@ -64,22 +64,31 @@ class Discord(discord.Client): search = match.group(1) count = Application.objects.filter(username__iexact=search, accepted__exact=True).count() + if count == 0: + count = Player.objects.filter(username__iexact=search, application__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"] + player = Player.objects.filter(username__iexact=search, application__accepted__exact=True).all()[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) + if not player.is_banned: + 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) + yield from self.change_nickname(message.author, nickname) + yield from self.add_roles(message.author, role) + yield from self.discord_message(message.channel, msg) + else: + msg = "{0} You are currently banned, appeal on the subreddit.".format(message.author.mention) + + 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) + return # IF MEMBER IS NOT AUTHORIZED, IGNORE if not any(role in self.auth_roles for role in member_roles): From fdb51af5c29b78204782a4768ded5dd78e6c101a Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 10 Dec 2018 13:26:14 -0600 Subject: [PATCH 05/10] register now checks if a user with a nickname matching to the player is already on the server --- api/bot.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/api/bot.py b/api/bot.py index 9275d34..91a4ad7 100644 --- a/api/bot.py +++ b/api/bot.py @@ -73,12 +73,18 @@ class Discord(discord.Client): nickname = player.username if not player.is_banned: - role = discord.utils.get(message.server.roles, name=self.member_role) - msg = "Successfully added {0} as a {1}.".format(nickname, self.member_role) + member = discord.utils.get(message.server.members, display_name=nickname) - yield from self.change_nickname(message.author, nickname) - yield from self.add_roles(message.author, role) - yield from self.discord_message(message.channel, msg) + 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: + 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) else: msg = "{0} You are currently banned, appeal on the subreddit.".format(message.author.mention) From d3a1cd063f0daf1927111898f535ab38dd99b784 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 10 Dec 2018 14:58:53 -0600 Subject: [PATCH 06/10] Added settings for appeal link and allowed a list of roles to get new members. --- api/bot.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/api/bot.py b/api/bot.py index 91a4ad7..52c35ec 100644 --- a/api/bot.py +++ b/api/bot.py @@ -15,7 +15,8 @@ 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') + new_member_roles = getattr(settings, 'DISCORD_BOT_NEW_MEMBER_ROLES', []) + appeal_self = getattr(settings, 'DISCORD_BOT_APPEAL_LINK', None) token = None def __init__(self, token, **kwargs): @@ -79,15 +80,19 @@ class Discord(discord.Client): 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: - role = discord.utils.get(message.server.roles, name=self.member_role) - msg = "Successfully added {0} as a {1}.".format(nickname, self.member_role) + 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.add_roles(message.author, role) yield from self.discord_message(message.channel, msg) else: - msg = "{0} You are currently banned, appeal on the subreddit.".format(message.author.mention) - + if self.appeal_self is None: + msg = "{0} You are currently banned, please appeal here: <{1}>".format(message.author.mention, self.appeal_link) + else: + msg = "{0} You are currently banned, please appeal." yield from self.discord_message(message.channel, msg) return From 6fdba9ff42a9feef6f342eff14d47d087e24fa14 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 10 Dec 2018 15:04:47 -0600 Subject: [PATCH 07/10] Added documentation for the new Discord Bot settings. --- docs/source/django-settings.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/source/django-settings.rst b/docs/source/django-settings.rst index 495131d..b7e0f70 100644 --- a/docs/source/django-settings.rst +++ b/docs/source/django-settings.rst @@ -57,6 +57,10 @@ 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. + +``DISCORD_BOT_APPEAL_LINK`` - Link of where to appeal bans on the server. + ``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 From 6000251a1a2af874a6f06084ffa9be9e7db02249 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 10 Dec 2018 15:10:41 -0600 Subject: [PATCH 08/10] Removed __exact from the end of accepted filters. --- api/bot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/bot.py b/api/bot.py index 439efb0..714b6a4 100644 --- a/api/bot.py +++ b/api/bot.py @@ -63,14 +63,14 @@ class Discord(discord.Client): 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() + count = Application.objects.filter(username__iexact=search, accepted=True).count() if count == 0: - count = Player.objects.filter(username__iexact=search, application__accepted__exact=True).count() + 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__exact=True).all()[0] + player = Player.objects.filter(username__iexact=search, application__accepted=True).all()[0] nickname = player.username if not player.is_banned: From a423de0cb87e2a37370bae4a005381d62270c9dc Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 10 Dec 2018 15:22:02 -0600 Subject: [PATCH 09/10] Removed appeal link and added register to the help command. --- api/bot.py | 9 +++------ docs/source/django-settings.rst | 2 -- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/api/bot.py b/api/bot.py index 714b6a4..5b3897c 100644 --- a/api/bot.py +++ b/api/bot.py @@ -16,7 +16,6 @@ class Discord(discord.Client): auth_roles = getattr(settings, 'DISCORD_BOT_ROLES', []) error_users = getattr(settings, 'DISCORD_ERROR_USERS', []) new_member_roles = getattr(settings, 'DISCORD_BOT_NEW_MEMBER_ROLES', []) - appeal_self = getattr(settings, 'DISCORD_BOT_APPEAL_LINK', None) token = None def __init__(self, token, **kwargs): @@ -89,15 +88,12 @@ class Discord(discord.Client): yield from self.change_nickname(message.author, nickname) yield from self.discord_message(message.channel, msg) else: - if self.appeal_self is None: - msg = "{0} You are currently banned, please appeal here: <{1}>".format(message.author.mention, self.appeal_link) - else: - msg = "{0} You are currently banned, please appeal." + msg = "{0} You are currently banned.".format(message.author.mention) yield from self.discord_message(message.channel, msg) return else: - msg = "An application for {0} could not be found, please apply first.".format(search) + 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 @@ -111,6 +107,7 @@ class Discord(discord.Client): 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 server if they have applied.") 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 b7e0f70..a7ef2fb 100644 --- a/docs/source/django-settings.rst +++ b/docs/source/django-settings.rst @@ -59,8 +59,6 @@ Optional ``DISCORD_BOT_NEW_MEMBER_ROLES`` - A list of Discord Roles to give new players when they register. -``DISCORD_BOT_APPEAL_LINK`` - Link of where to appeal bans on the server. - ``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 From b09d4f69f69ebc66477fbba4d74bf72a388d5ee4 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 10 Dec 2018 15:28:32 -0600 Subject: [PATCH 10/10] Clarified register help message. --- api/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/bot.py b/api/bot.py index 5b3897c..ca9881c 100644 --- a/api/bot.py +++ b/api/bot.py @@ -107,7 +107,7 @@ class Discord(discord.Client): 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 server if they have applied.") + 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.")