Merge pull request #11 from EndenDragon/botcommands

Kick and Ban commands to bot
This commit is contained in:
Jeremy "EndenDragon" Zhang 2017-05-26 19:04:29 -07:00 committed by GitHub
commit 464b2c0e8c
4 changed files with 123 additions and 7 deletions

View File

@ -2,7 +2,27 @@ class Commands():
def __init__(self, client, database): def __init__(self, client, database):
self.client = client self.client = client
self.database = database self.database = database
async def ban(self, message): async def ban(self, message):
pass serverid = message.server.id
#await self.client.send_message(message.channel, "test test!") 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 kick a guest user.\nExample: `kick 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)

View File

@ -13,6 +13,8 @@ Base = declarative_base()
from titanembeds.database.guilds import Guilds from titanembeds.database.guilds import Guilds
from titanembeds.database.messages import Messages from titanembeds.database.messages import Messages
from titanembeds.database.guild_members import GuildMembers from titanembeds.database.guild_members import GuildMembers
from titanembeds.database.unauthenticated_users import UnauthenticatedUsers
from titanembeds.database.unauthenticated_bans import UnauthenticatedBans
class DatabaseInterface(object): class DatabaseInterface(object):
# Courtesy of https://github.com/SunDwarf/Jokusoramame # Courtesy of https://github.com/SunDwarf/Jokusoramame
@ -290,12 +292,59 @@ class DatabaseInterface(object):
True, True,
"[]" "[]"
) )
db.session.add(dbusr) session.add(dbusr)
if changed: if changed:
session.commit() 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(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
pass dbuser = None
#dbusr = session.query() 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)

View File

@ -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 '<UnauthenticatedBans {0} {1} {2} {3} {4} {5}'.format(self.id, self.guild_id, self.ip_address, self.last_username, self.last_discriminator, self.timestamp)

View File

@ -0,0 +1,19 @@
from titanembeds.database import db, Base
import datetime
import time
import random
import string
class UnauthenticatedUsers(Base):
__tablename__ = "unauthenticated_users"
id = db.Column(db.Integer, primary_key=True) # Auto increment id
guild_id = db.Column(db.String(255)) # Guild pretaining to the unauthenticated user
username = db.Column(db.String(255)) # The username of the user
discriminator = db.Column(db.Integer) # The discriminator to distinguish unauth users with each other
user_key = db.Column(db.Text()) # The secret key used to identify the user holder
ip_address = db.Column(db.String(255)) # The IP Address of the user
last_timestamp = db.Column(db.TIMESTAMP) # The timestamp of when the user has last sent the heartbeat
revoked = db.Column(db.Boolean()) # If the user's key has been revoked and a new one is required to be generated
def __repr__(self):
return '<UnauthenticatedUsers {0} {1} {2} {3} {4} {5} {6} {7}>'.format(self.id, self.guild_id, self.username, self.discriminator, self.user_key, self.ip_address, self.last_timestamp, self.revoked)