From afda68d138fbdd3db92f81f1c9443f22f89ed0f4 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Fri, 26 May 2017 19:00:50 -0700 Subject: [PATCH] Added kick and ban commands to bot --- discordbot/titanembeds/commands.py | 26 ++++++++- discordbot/titanembeds/database/__init__.py | 57 +++++++++++++++++-- .../database/unauthenticated_bans.py | 28 +++++++++ .../database/unauthenticated_users.py | 19 +++++++ 4 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 discordbot/titanembeds/database/unauthenticated_bans.py create mode 100644 discordbot/titanembeds/database/unauthenticated_users.py diff --git a/discordbot/titanembeds/commands.py b/discordbot/titanembeds/commands.py index 0551521..a2d356c 100644 --- a/discordbot/titanembeds/commands.py +++ b/discordbot/titanembeds/commands.py @@ -2,7 +2,27 @@ class Commands(): def __init__(self, client, database): self.client = client self.database = database - + async def ban(self, message): - pass - #await self.client.send_message(message.channel, "test test!") \ No newline at end of file + serverid = message.server.id + content = message.content.strip() + if len(content.split()) == 2: + await self.client.send_message(message.channel, message.author.mention + " Please provide a username-query (or optionally a discriminator) to ban a guest user.\nExample: `ban 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 + reason = await self.database.ban_unauth_user_by_query(message.server.id, message.author.id, username, discriminator) + await self.client.send_message(message.channel, message.author.mention + " " + reason) + + async def kick(self, message): + serverid = message.server.id + content = message.content.strip() + if len(content.split()) == 2: + await self.client.send_message(message.channel, message.author.mention + " Please provide a username-query (or optionally a discriminator) to ban a guest user.\nExample: `ban 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 + reason = await self.database.revoke_unauth_user_by_query(message.server.id, username, discriminator) + await self.client.send_message(message.channel, message.author.mention + " " + reason) diff --git a/discordbot/titanembeds/database/__init__.py b/discordbot/titanembeds/database/__init__.py index 7fde1e1..bd63774 100644 --- a/discordbot/titanembeds/database/__init__.py +++ b/discordbot/titanembeds/database/__init__.py @@ -13,6 +13,8 @@ Base = declarative_base() from titanembeds.database.guilds import Guilds from titanembeds.database.messages import Messages from titanembeds.database.guild_members import GuildMembers +from titanembeds.database.unauthenticated_users import UnauthenticatedUsers +from titanembeds.database.unauthenticated_bans import UnauthenticatedBans class DatabaseInterface(object): # Courtesy of https://github.com/SunDwarf/Jokusoramame @@ -290,12 +292,59 @@ class DatabaseInterface(object): True, "[]" ) - db.session.add(dbusr) + session.add(dbusr) if changed: session.commit() - async def ban_unauth_user_by_query(self, query): + async def ban_unauth_user_by_query(self, guild_id, placer_id, username, discriminator): async with threadpool(): with self.get_session() as session: - pass - #dbusr = session.query() \ No newline at end of file + dbuser = None + if discriminator: + dbuser = session.query(UnauthenticatedUsers) \ + .filter(UnauthenticatedUsers.guild_id == guild_id) \ + .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ + .filter(UnauthenticatedUsers.discriminator == discriminator) \ + .order_by(UnauthenticatedUsers.id.desc()).first() + else: + dbuser = session.query(UnauthenticatedUsers) \ + .filter(UnauthenticatedUsers.guild_id == guild_id) \ + .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ + .order_by(UnauthenticatedUsers.id.desc()).first() + if not dbuser: + return "Ban error! Guest user cannot be found." + dbban = session.query(UnauthenticatedBans) \ + .filter(UnauthenticatedBans.guild_id == guild_id) \ + .filter(UnauthenticatedBans.last_username == dbuser.username) \ + .filter(UnauthenticatedBans.last_discriminator == dbuser.discriminator).first() + if dbban is not None: + if dbban.lifter_id is None: + return "Ban error! Guest user, **{}#{}**, has already been banned.".format(dbban.last_username, dbban.last_discriminator) + session.delete(dbban) + dbban = UnauthenticatedBans(guild_id, dbuser.ip_address, dbuser.username, dbuser.discriminator, "", placer_id) + session.add(dbban) + session.commit() + return "Guest user, **{}#{}**, has successfully been added to the ban list!".format(dbban.last_username, dbban.last_discriminator) + + async def revoke_unauth_user_by_query(self, guild_id, username, discriminator): + async with threadpool(): + with self.get_session() as session: + dbuser = None + if discriminator: + dbuser = session.query(UnauthenticatedUsers) \ + .filter(UnauthenticatedUsers.guild_id == guild_id) \ + .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ + .filter(UnauthenticatedUsers.discriminator == discriminator) \ + .order_by(UnauthenticatedUsers.id.desc()).first() + else: + dbuser = session.query(UnauthenticatedUsers) \ + .filter(UnauthenticatedUsers.guild_id == guild_id) \ + .filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \ + .order_by(UnauthenticatedUsers.id.desc()).first() + if not dbuser: + return "Kick error! Guest user cannot be found." + elif dbuser.revoked: + return "Kick error! Guest user **{}#{}** has already been kicked!".format(dbuser.username, dbuser.discriminator) + dbuser.revoked = True + session.commit() + return "Successfully kicked **{}#{}**!".format(dbuser.username, dbuser.discriminator) diff --git a/discordbot/titanembeds/database/unauthenticated_bans.py b/discordbot/titanembeds/database/unauthenticated_bans.py new file mode 100644 index 0000000..b68e8c2 --- /dev/null +++ b/discordbot/titanembeds/database/unauthenticated_bans.py @@ -0,0 +1,28 @@ +from titanembeds.database import db, Base +import datetime +import time + +class UnauthenticatedBans(Base): + __tablename__ = "unauthenticated_bans" + id = db.Column(db.Integer, primary_key=True) # Auto increment id + guild_id = db.Column(db.String(255)) # Guild pretaining to the unauthenticated user + ip_address = db.Column(db.String(255)) # The IP Address of the user + last_username = db.Column(db.String(255)) # The username when they got banned + last_discriminator = db.Column(db.Integer) # The discrim when they got banned + timestamp = db.Column(db.TIMESTAMP) # The timestamp of when the user got banned + reason = db.Column(db.Text()) # The reason of the ban set by the guild moderators + lifter_id = db.Column(db.String(255)) # Discord Client ID of the user who lifted the ban + placer_id = db.Column(db.String(255)) # The id of who placed the ban + + def __init__(self, guild_id, ip_address, last_username, last_discriminator, reason, placer_id): + self.guild_id = guild_id + self.ip_address = ip_address + self.last_username = last_username + self.last_discriminator = last_discriminator + self.timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + self.reason = reason + self.lifter_id = None + self.placer_id = placer_id + + def __repr__(self): + return ''.format(self.id, self.guild_id, self.username, self.discriminator, self.user_key, self.ip_address, self.last_timestamp, self.revoked)