2017-05-04 05:22:27 +02:00
|
|
|
from config import config
|
2017-05-04 07:16:49 +02:00
|
|
|
from titanembeds.database import DatabaseInterface
|
2017-05-04 05:22:27 +02:00
|
|
|
import discord
|
2017-05-04 07:16:49 +02:00
|
|
|
import aiohttp
|
|
|
|
import asyncio
|
2017-05-14 23:05:51 +02:00
|
|
|
import sys
|
2017-05-15 00:09:38 +02:00
|
|
|
import logging
|
2017-05-15 00:19:15 +02:00
|
|
|
logging.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
|
2017-05-15 00:09:38 +02:00
|
|
|
logging.getLogger('TitanBot')
|
|
|
|
logging.getLogger('sqlalchemy')
|
2017-05-04 05:22:27 +02:00
|
|
|
|
2017-05-04 07:16:49 +02:00
|
|
|
class Titan(discord.Client):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.aiosession = aiohttp.ClientSession(loop=self.loop)
|
|
|
|
self.http.user_agent += ' TitanEmbeds-Bot'
|
|
|
|
self.database = DatabaseInterface(self)
|
2017-05-04 05:22:27 +02:00
|
|
|
|
2017-05-04 07:16:49 +02:00
|
|
|
def _cleanup(self):
|
|
|
|
try:
|
|
|
|
self.loop.run_until_complete(self.logout())
|
|
|
|
except: # Can be ignored
|
|
|
|
pass
|
|
|
|
pending = asyncio.Task.all_tasks()
|
|
|
|
gathered = asyncio.gather(*pending)
|
|
|
|
try:
|
|
|
|
gathered.cancel()
|
|
|
|
self.loop.run_until_complete(gathered)
|
|
|
|
gathered.exception()
|
|
|
|
except: # Can be ignored
|
|
|
|
pass
|
2017-05-04 05:22:27 +02:00
|
|
|
|
2017-05-04 07:16:49 +02:00
|
|
|
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):
|
|
|
|
print('Titan [DiscordBot]')
|
|
|
|
print('Logged in as the following user:')
|
|
|
|
print(self.user.name)
|
|
|
|
print(self.user.id)
|
|
|
|
print('------')
|
|
|
|
|
|
|
|
await self.change_presence(
|
2017-05-20 03:24:21 +02:00
|
|
|
game=discord.Game(name="Embed your Discord server! Visit https://TitanEmbeds.tk/"), status=discord.Status.online
|
2017-05-04 07:16:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2017-05-07 00:27:07 +02:00
|
|
|
await self.database.connect(config["database-uri"] + "?charset=utf8")
|
2017-05-04 07:16:49 +02:00
|
|
|
except Exception:
|
|
|
|
self.logger.error("Unable to connect to specified database!")
|
|
|
|
traceback.print_exc()
|
|
|
|
await self.logout()
|
|
|
|
return
|
2017-05-14 23:05:51 +02:00
|
|
|
|
|
|
|
if "no-init" not in sys.argv:
|
|
|
|
for server in self.servers:
|
|
|
|
await self.database.update_guild(server)
|
|
|
|
if server.large:
|
|
|
|
await request_offline_members(server)
|
|
|
|
server_bans = await self.get_bans(server)
|
|
|
|
for member in server.members:
|
|
|
|
banned = member.id in [u.id for u in server_bans]
|
|
|
|
await self.database.update_guild_member(
|
|
|
|
member,
|
|
|
|
True,
|
|
|
|
banned
|
|
|
|
)
|
|
|
|
await self.database.flag_unactive_guild_members(server.id, server.members)
|
|
|
|
await self.database.flag_unactive_bans(server.id, server_bans)
|
|
|
|
await self.database.remove_unused_guilds(self.servers)
|
|
|
|
else:
|
|
|
|
print("Skipping indexing server due to no-init flag")
|
2017-05-07 00:27:07 +02:00
|
|
|
|
2017-05-06 10:03:52 +02:00
|
|
|
async def on_message(self, message):
|
|
|
|
await self.database.push_message(message)
|
|
|
|
# TODO: Will add command handler + ban/kick command
|
|
|
|
|
|
|
|
async def on_message_edit(self, message_before, message_after):
|
|
|
|
await self.database.update_message(message_after)
|
2017-05-07 00:27:07 +02:00
|
|
|
|
|
|
|
async def on_message_delete(self, message):
|
|
|
|
await self.database.delete_message(message)
|
|
|
|
|
|
|
|
async def on_server_join(self, guild):
|
2017-05-20 03:45:38 +02:00
|
|
|
await asyncio.sleep(1)
|
2017-05-20 03:22:58 +02:00
|
|
|
if not guild.me.server_permissions.administrator:
|
2017-05-20 03:45:38 +02:00
|
|
|
await self.leave_server(guild)
|
|
|
|
return
|
2017-05-20 03:22:58 +02:00
|
|
|
|
2017-05-07 00:27:07 +02:00
|
|
|
await self.database.update_guild(guild)
|
2017-05-09 05:19:45 +02:00
|
|
|
for channel in guild.channels:
|
|
|
|
async for message in self.logs_from(channel, limit=50, reverse=True):
|
|
|
|
await self.database.push_message(message)
|
2017-05-09 05:49:37 +02:00
|
|
|
for member in guild.members:
|
2017-05-20 03:45:38 +02:00
|
|
|
await self.database.update_guild_member(member, True, False)
|
2017-05-09 05:49:37 +02:00
|
|
|
banned = self.get_bans(guild)
|
|
|
|
for ban in banned:
|
2017-05-20 03:45:38 +02:00
|
|
|
await self.database.update_guild_member(ban, False, True)
|
2017-05-07 00:27:07 +02:00
|
|
|
|
|
|
|
async def on_server_remove(self, guild):
|
|
|
|
await self.database.remove_guild(guild)
|
|
|
|
|
|
|
|
async def on_server_update(self, guildbefore, guildafter):
|
|
|
|
await self.database.update_guild(guildafter)
|
|
|
|
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
await self.database.update_guild(roleafter.server)
|
|
|
|
|
|
|
|
async def on_channel_delete(self, channel):
|
|
|
|
await self.database.update_guild(channel.server)
|
|
|
|
|
|
|
|
async def on_channel_create(self, channel):
|
|
|
|
await self.database.update_guild(channel.server)
|
|
|
|
|
|
|
|
async def on_channel_update(self, channelbefore, channelafter):
|
|
|
|
await self.database.update_guild(channelafter.server)
|
|
|
|
|
|
|
|
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):
|
|
|
|
await self.database.update_guild_member(member, active=False, banned=False)
|
|
|
|
|
|
|
|
async def on_member_update(self, memberbefore, memberafter):
|
|
|
|
await self.database.update_guild_member(memberafter)
|
|
|
|
|
|
|
|
async def on_member_ban(self, member):
|
2017-05-14 23:05:51 +02:00
|
|
|
if self.user.id == member.id:
|
2017-05-07 00:27:07 +02:00
|
|
|
return
|
|
|
|
await self.database.update_guild_member(member, active=False, banned=True)
|
|
|
|
|
2017-05-14 23:05:51 +02:00
|
|
|
async def on_member_unban(self, server, user):
|
|
|
|
await self.database.unban_server_user(user, server)
|