Added commands I think

This commit is contained in:
JustMaffie 2017-05-27 23:38:28 +02:00
parent 27f5b2407d
commit 691625f6a8

View File

@ -10,15 +10,24 @@ logging.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime
logging.getLogger('TitanBot')
logging.getLogger('sqlalchemy')
bot = commands.Bot(command_prefix=config['command-prefix'])
self.aiosession = aiohttp.ClientSession(loop=self.loop)
self.http.user_agent += ' TitanEmbeds-Bot'
self.database = DatabaseInterface(self)
class Titan(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(command_prefix = config['command-prefix'])
self.aiosession = aiohttp.ClientSession(loop=self.loop)
self.http.user_agent += ' TitanEmbeds-Bot'
self.database = DatabaseInterface(self)
def run(self):
try:
self.loop.run_until_complete(self.start(config["bot-token"]))
except discord.errors.LoginFailure:
print("Invalid bot token in config!")
finally:
try:
self._cleanup()
except Exception as e:
print("Error in cleanup:", e)
self.loop.close()
def _cleanup(self):
def _cleanup(self):
try:
self.loop.run_until_complete(self.logout())
except: # Can be ignored
@ -32,29 +41,17 @@ class Titan(commands.Bot):
except: # Can be ignored
pass
def run(self):
try:
self.loop.run_until_complete(self.start(config["bot-token"]))
except discord.errors.LoginFailure:
print("Invalid bot token in config!")
finally:
try:
self._cleanup()
except Exception as e:
print("Error in cleanup:", e)
self.loop.close()
async def on_ready(self):
@bot.event
async def on_ready(self):
print('Titan [DiscordBot]')
print('Logged in as the following user:')
print(self.user.name)
print(self.user.id)
print('------')
await self.change_presence(
game=discord.Game(name="Embed your Discord server! Visit https://TitanEmbeds.tk/"), status=discord.Status.online
)
try:
await self.database.connect(config["database-uri"] + "?charset=utf8mb4")
except Exception:
@ -62,7 +59,6 @@ class Titan(commands.Bot):
traceback.print_exc()
await self.logout()
return
if "no-init" not in sys.argv:
for server in self.servers:
await self.database.update_guild(server)
@ -82,25 +78,27 @@ class Titan(commands.Bot):
else:
print("Skipping indexing server due to no-init flag")
async def on_message(self, message):
@bot.event
async def on_message(self, message):
await self.database.push_message(message)
if message.server:
await self.process_commands(message)
async def on_message_edit(self, message_before, message_after):
@bot.event
async def on_message_edit(self, message_before, message_after):
await self.database.update_message(message_after)
async def on_message_delete(self, message):
@bot.event
async def on_message_delete(self, message):
await self.database.delete_message(message)
async def on_server_join(self, guild):
@bot.event
async def on_server_join(self, guild):
await asyncio.sleep(1)
if not guild.me.server_permissions.administrator:
await asyncio.sleep(1)
await self.leave_server(guild)
return
await self.database.update_guild(guild)
for channel in guild.channels:
async for message in self.logs_from(channel, limit=50, reverse=True):
@ -111,54 +109,67 @@ class Titan(commands.Bot):
for ban in banned:
await self.database.update_guild_member(ban, False, True)
async def on_server_remove(self, guild):
@bot.event
async def on_server_remove(self, guild):
await self.database.remove_guild(guild)
async def on_server_update(self, guildbefore, guildafter):
@bot.event
async def on_server_update(self, guildbefore, guildafter):
await self.database.update_guild(guildafter)
async def on_server_role_create(self, role):
@bot.event
async def on_server_role_create(self, role):
if role.name == self.user.name and role.managed:
await asyncio.sleep(2)
await self.database.update_guild(role.server)
async def on_server_role_delete(self, role):
@bot.event
async def on_server_role_delete(self, role):
if role.server.me not in role.server.members:
return
await self.database.update_guild(role.server)
async def on_server_role_update(self, rolebefore, roleafter):
@bot.event
async def on_server_role_update(self, rolebefore, roleafter):
await self.database.update_guild(roleafter.server)
async def on_channel_delete(self, channel):
@bot.event
async def on_channel_delete(self, channel):
await self.database.update_guild(channel.server)
async def on_channel_create(self, channel):
@bot.event
async def on_channel_create(self, channel):
await self.database.update_guild(channel.server)
async def on_channel_update(self, channelbefore, channelafter):
@bot.event
async def on_channel_update(self, channelbefore, channelafter):
await self.database.update_guild(channelafter.server)
async def on_member_join(self, member):
@bot.event
async def on_member_join(self, member):
await self.database.update_guild_member(member, active=True, banned=False)
async def on_member_remove(self, member):
@bot.event
async def on_member_remove(self, member):
await self.database.update_guild_member(member, active=False, banned=False)
async def on_member_update(self, memberbefore, memberafter):
@bot.event
async def on_member_update(self, memberbefore, memberafter):
await self.database.update_guild_member(memberafter)
async def on_member_ban(self, member):
@bot.event
async def on_member_ban(self, member):
if self.user.id == member.id:
return
await self.database.update_guild_member(member, active=False, banned=True)
async def on_member_unban(self, server, user):
@bot.event
async def on_member_unban(self, server, user):
await self.database.unban_server_user(user, server)
@commands.command(pass_context=True)
async def ban(ctx, self):
@commands.command(pass_context=True)
async def ban(ctx, self):
message = ctx.message
if not message.author.server_permissions.ban_members:
await self.send_message(message.channel, message.author.mention + " I'm sorry, but you do not have permissions to ban guest members.")
@ -174,8 +185,8 @@ class Titan(commands.Bot):
reason = await self.database.ban_unauth_user_by_query(message.server.id, message.author.id, username, discriminator)
await self.send_message(message.channel, message.author.mention + " " + reason)
@commands.command(pass_context=True)
async def kick(ctx, self):
@commands.command(pass_context=True)
async def kick(ctx, self):
message = ctx.message
if not message.author.server_permissions.kick_members:
await self.send_message(message.channel, message.author.mention + " I'm sorry, but you do not have permissions to kick guest members.")