From 790dca17665a8cdbf11e2d09a8b20ffd097bc38c Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Thu, 10 Jan 2019 02:07:12 +0000 Subject: [PATCH] Unban command --- discordbot/titanembeds/commands.py | 31 ++++++++++++++++ webapp/titanembeds/blueprints/api/api.py | 35 +++++++++++++++++++ .../templates/card_commands.html.j2 | 2 ++ 3 files changed, 68 insertions(+) diff --git a/discordbot/titanembeds/commands.py b/discordbot/titanembeds/commands.py index 02a5377..e1d2e9e 100644 --- a/discordbot/titanembeds/commands.py +++ b/discordbot/titanembeds/commands.py @@ -36,6 +36,37 @@ class Commands(): await message.channel.send(message.author.mention + " " + j["success"]) return await message.channel.send("Unhandled webservice error in banning guest user!") + + async def unban(self, message): + if not message.author.guild_permissions.ban_members: + await message.channel.send(message.author.mention + " I'm sorry, but you do not have permissions to unban guest members.") + return + content = message.content.strip() + if len(content.split()) == 2: + await message.channel.send(message.author.mention + " Please provide a username-query (or optionally a discriminator) to unban a guest user.\nExample: `unban Titan#0001`") + return + content = content.split() + username = content[2][:content[2].find("#")] if "#" in content[2] else content[2] + discriminator = int(content[2][content[2].find("#") + 1:]) if "#" in content[2] else None + headers = {"Authorization": self.config["titan-web-app-secret"]} + payload = { + "guild_id": message.guild.id, + "lifter_id": message.author.id, + "username": username + } + if discriminator: + payload["discriminator"] = discriminator + url = self.config["titan-web-url"] + "api/bot/unban" + async with aiohttp.ClientSession() as aioclient: + async with aioclient.post(url, json=payload, headers=headers) as resp: + j = await resp.json() + if "error" in j: + await message.channel.send(message.author.mention + " Unban error! " + j["error"]) + return + if "success" in j: + await message.channel.send(message.author.mention + " " + j["success"]) + return + await message.channel.send("Unhandled webservice error in unbanning guest user!") async def kick(self, message): if not message.author.guild_permissions.kick_members: diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index c57e8d8..ceb81cb 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -634,6 +634,41 @@ def bot_ban(): db.session.add(dbban) db.session.commit() return jsonify(success="Guest user, **{}#{}**, has successfully been added to the ban list!".format(dbban.last_username, dbban.last_discriminator)) + +@api.route("/bot/unban", methods=["POST"]) +def bot_unban(): + if request.headers.get("Authorization", "") != config.get("app-secret", ""): + return jsonify(error="Authorization header does not match."), 403 + incoming = request.get_json() + guild_id = incoming.get("guild_id", None) + lifter_id = incoming.get("lifter_id", None) + username = incoming.get("username", None) + discriminator = incoming.get("discriminator", None) + if not guild_id or not lifter_id or not username: + return jsonify(error="Missing required parameters."), 400 + if discriminator: + dbuser = db.session.query(UnauthenticatedUsers) \ + .filter(UnauthenticatedUsers.guild_id == str(guild_id)) \ + .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ + .filter(UnauthenticatedUsers.discriminator == discriminator) \ + .order_by(UnauthenticatedUsers.id.desc()).first() + else: + dbuser = db.session.query(UnauthenticatedUsers) \ + .filter(UnauthenticatedUsers.guild_id == str(guild_id)) \ + .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ + .order_by(UnauthenticatedUsers.id.desc()).first() + if not dbuser: + return jsonify(error="Guest user cannot be found."), 404 + dbban = db.session.query(UnauthenticatedBans) \ + .filter(UnauthenticatedBans.guild_id == str(guild_id)) \ + .filter(UnauthenticatedBans.ip_address == dbuser.ip_address).first() + if dbban is None: + return jsonify(error="Guest user **{}#{}** has not been banned.".format(dbuser.username, dbuser.discriminator)), 404 + if dbban.lifter_id is not None: + return jsonify(error="Guest user **{}#{}** ban has already been removed.".format(dbuser.username, dbuser.discriminator)), 409 + dbban.liftBan(int(lifter_id)) + db.session.commit() + return jsonify(success="Guest user, **{}#{}**, has successfully been removed from the ban list!".format(dbuser.username, dbuser.discriminator)) @api.route("/bot/revoke", methods=["POST"]) def bot_revoke(): diff --git a/webapp/titanembeds/templates/card_commands.html.j2 b/webapp/titanembeds/templates/card_commands.html.j2 index fb3fa70..1f50546 100644 --- a/webapp/titanembeds/templates/card_commands.html.j2 +++ b/webapp/titanembeds/templates/card_commands.html.j2 @@ -8,6 +8,7 @@

General

For your information needs.