From b7b41870945a1d9563baef6119877bb635ba03bf Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sat, 20 May 2017 07:50:29 +0000 Subject: [PATCH] Initial command structure for bot --- discordbot/titanembeds/bot.py | 12 +++++++++++- discordbot/titanembeds/commands.py | 8 ++++++++ discordbot/titanembeds/database/__init__.py | 6 ++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 discordbot/titanembeds/commands.py diff --git a/discordbot/titanembeds/bot.py b/discordbot/titanembeds/bot.py index 996f708..ef76912 100644 --- a/discordbot/titanembeds/bot.py +++ b/discordbot/titanembeds/bot.py @@ -1,5 +1,6 @@ from config import config from titanembeds.database import DatabaseInterface +from titanembeds.commands import Commands import discord import aiohttp import asyncio @@ -15,6 +16,7 @@ class Titan(discord.Client): self.aiosession = aiohttp.ClientSession(loop=self.loop) self.http.user_agent += ' TitanEmbeds-Bot' self.database = DatabaseInterface(self) + self.command = Commands(self, self.database) def _cleanup(self): try: @@ -82,7 +84,15 @@ class Titan(discord.Client): async def on_message(self, message): await self.database.push_message(message) - # TODO: Will add command handler + ban/kick command + + msg_arr = message.content.split() # split the message + if len(message.content.split()) > 1 and message.server: #making sure there is actually stuff in the message and have arguments and check if it is sent in server (not PM) + if msg_arr[0] == "<@{}>".format(self.user.id): #make sure it is mention + msg_cmd = msg_arr[1].lower() # get command + cmd = getattr(self.command, msg_cmd, None) #check if cmd exist, if not its none + if cmd: # if cmd is not none... + await self.send_typing(message.channel) #this looks nice + await getattr(self.command, msg_cmd)(message) #actually run cmd, passing in msg obj async def on_message_edit(self, message_before, message_after): await self.database.update_message(message_after) diff --git a/discordbot/titanembeds/commands.py b/discordbot/titanembeds/commands.py new file mode 100644 index 0000000..0551521 --- /dev/null +++ b/discordbot/titanembeds/commands.py @@ -0,0 +1,8 @@ +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 diff --git a/discordbot/titanembeds/database/__init__.py b/discordbot/titanembeds/database/__init__.py index edc4ca3..7fde1e1 100644 --- a/discordbot/titanembeds/database/__init__.py +++ b/discordbot/titanembeds/database/__init__.py @@ -293,3 +293,9 @@ class DatabaseInterface(object): db.session.add(dbusr) if changed: session.commit() + + async def ban_unauth_user_by_query(self, query): + async with threadpool(): + with self.get_session() as session: + pass + #dbusr = session.query() \ No newline at end of file