From 7ff578bbddc9806a6f797f6130c6e3b1cf296bad Mon Sep 17 00:00:00 2001 From: JustMaffie Date: Tue, 5 Sep 2017 11:02:48 +0200 Subject: [PATCH] ADD COMMANDS EXT FROM DISCORD.PY --- discordbot/titanembeds/bot.py | 58 +++++++++++++++++++++++------- discordbot/titanembeds/commands.py | 34 ------------------ 2 files changed, 45 insertions(+), 47 deletions(-) delete mode 100644 discordbot/titanembeds/commands.py diff --git a/discordbot/titanembeds/bot.py b/discordbot/titanembeds/bot.py index 520839e..128168c 100644 --- a/discordbot/titanembeds/bot.py +++ b/discordbot/titanembeds/bot.py @@ -1,7 +1,7 @@ from config import config from titanembeds.database import DatabaseInterface -from titanembeds.commands import Commands from titanembeds.socketio import SocketIOInterface +from discord.ext import commands import discord import aiohttp import asyncio @@ -11,13 +11,50 @@ logging.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime logging.getLogger('TitanBot') logging.getLogger('sqlalchemy') -class Titan(discord.Client): +class CommandCog: + def __init__(self, bot, database): + self.bot = bot + self.database = database + + @commands.command() + async def ban(self, message): + if not message.author.server_permissions.ban_members: + await self.bot.reply("I'm sorry, but you do not have permissions to ban guest members.") + return + serverid = message.server.id + content = message.content.strip() + if len(content.split()) == 2: + await self.bot.reply("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.bot.reply(reason) + + @commands.command() + async def kick(self, message): + if not message.author.server_permissions.kick_members: + await self.bot.reply("I'm sorry, but you do not have permissions to kick guest members.") + return + serverid = message.server.id + content = message.content.strip() + if len(content.split()) == 2: + await self.bot.reply("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.bot.reply(reason) + + +class Titan(commands.Bot): def __init__(self): - super().__init__() + super().__init__(*args, command_prefix=commands.when_mentioned, **kwargs) self.aiosession = aiohttp.ClientSession(loop=self.loop) self.http.user_agent += ' TitanEmbeds-Bot' self.database = DatabaseInterface(self) - self.command = Commands(self, self.database) self.socketio = SocketIOInterface(self, config["redis-uri"]) self.database_connected = False @@ -83,6 +120,9 @@ class Titan(discord.Client): game=discord.Game(name="Embed your Discord server! Visit https://TitanEmbeds.com/"), status=discord.Status.online ) + commands = CommandsCog(self, self.database) + self.add_cog(commands) + try: await self.database.connect(config["database-uri"]) self.database_connected = True @@ -126,15 +166,7 @@ class Titan(discord.Client): await self.wait_until_dbonline() await self.database.push_message(message) await self.socketio.on_message(message) - - 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 + await self.process_commands(message) async def on_message_edit(self, message_before, message_after): await self.wait_until_dbonline() diff --git a/discordbot/titanembeds/commands.py b/discordbot/titanembeds/commands.py deleted file mode 100644 index 3ee25b0..0000000 --- a/discordbot/titanembeds/commands.py +++ /dev/null @@ -1,34 +0,0 @@ -class Commands(): - def __init__(self, client, database): - self.client = client - self.database = database - - async def ban(self, message): - if not message.author.server_permissions.ban_members: - await self.client.send_message(message.channel, message.author.mention + " I'm sorry, but you do not have permissions to ban guest members.") - return - 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): - if not message.author.server_permissions.kick_members: - await self.client.send_message(message.channel, message.author.mention + " I'm sorry, but you do not have permissions to kick guest members.") - return - 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)