From 8a7e3322b9cb7ec650e4f817ef47e51b31f3c604 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Mon, 11 Sep 2017 20:33:20 +0000 Subject: [PATCH] Remove global header when the bot is not connected to the database --- discordbot/config.example.py | 2 - discordbot/titanembeds/bot.py | 46 ------------------- discordbot/titanembeds/database/__init__.py | 14 ------ .../database/keyvalue_properties.py | 17 ------- webapp/titanembeds/app.py | 5 +- webapp/titanembeds/templates/embed.html.j2 | 2 - .../templates/nobot_header.html.j2 | 10 ---- .../titanembeds/templates/site_layout.html.j2 | 1 - webapp/titanembeds/utils.py | 16 +------ 9 files changed, 3 insertions(+), 110 deletions(-) delete mode 100644 discordbot/titanembeds/database/keyvalue_properties.py delete mode 100644 webapp/titanembeds/templates/nobot_header.html.j2 diff --git a/discordbot/config.example.py b/discordbot/config.example.py index a6304d1..b600ae4 100644 --- a/discordbot/config.example.py +++ b/discordbot/config.example.py @@ -5,7 +5,5 @@ config = { 'redis-uri': "redis://", - 'errorreporting-channelid': "", - 'logging-location': "/home/titan/Titan/discordbot/titanbot.log", } diff --git a/discordbot/titanembeds/bot.py b/discordbot/titanembeds/bot.py index df63af1..7353b7a 100644 --- a/discordbot/titanembeds/bot.py +++ b/discordbot/titanembeds/bot.py @@ -21,9 +21,6 @@ class Titan(discord.Client): self.database = DatabaseInterface(self) self.command = Commands(self, self.database) self.socketio = SocketIOInterface(self, config["redis-uri"]) - - self.database_connected = False - self.loop.create_task(self.send_webserver_heartbeat()) def _cleanup(self): try: @@ -38,29 +35,6 @@ class Titan(discord.Client): gathered.exception() except: # Can be ignored pass - - async def wait_until_dbonline(self): - while not self.database_connected: - await asyncio.sleep(1) # Wait until db is connected - - async def send_webserver_heartbeat(self): - await self.wait_until_ready() - await self.wait_until_dbonline() - last_db_conn_status = False - while not self.is_closed: - try: - await self.database.send_webserver_heartbeat() - self.database_connected = True - except: - self.database_connected = False - if last_db_conn_status != self.database_connected and config.get("errorreporting-channelid"): - error_channel = self.get_channel(config["errorreporting-channelid"]) - if self.database_connected: - await self.send_message(error_channel, "Titan has obtained connection to the database!") - else: - await self.send_message(error_channel, "Titan has lost connection to the database! Don't panic!! We'll sort this out... hopefully soon.") - last_db_conn_status = self.database_connected - await asyncio.sleep(60) def run(self): try: @@ -87,7 +61,6 @@ class Titan(discord.Client): try: await self.database.connect(config["database-uri"]) - self.database_connected = True except Exception: self.logger.error("Unable to connect to specified database!") traceback.print_exc() @@ -117,7 +90,6 @@ class Titan(discord.Client): print("Skipping indexing server due to no-init flag") async def on_message(self, message): - await self.wait_until_dbonline() await self.database.push_message(message) await self.socketio.on_message(message) @@ -131,17 +103,14 @@ class Titan(discord.Client): 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.wait_until_dbonline() await self.database.update_message(message_after) await self.socketio.on_message_update(message_after) async def on_message_delete(self, message): - await self.wait_until_dbonline() await self.database.delete_message(message) await self.socketio.on_message_delete(message) async def on_server_join(self, guild): - await self.wait_until_dbonline() await self.database.update_guild(guild) for channel in guild.channels: if not channel.permissions_for(channel.server.me).read_messages: @@ -156,75 +125,61 @@ class Titan(discord.Client): await self.database.update_guild_member(ban, False, True) async def on_server_remove(self, guild): - await self.wait_until_dbonline() await self.database.remove_guild(guild) async def on_server_update(self, guildbefore, guildafter): - await self.wait_until_dbonline() await self.database.update_guild(guildafter) await self.socketio.on_guild_update(guildafter) async def on_server_role_create(self, role): - await self.wait_until_dbonline() if role.name == self.user.name and role.managed: await asyncio.sleep(2) await self.database.update_guild(role.server) await self.socketio.on_guild_role_create(role) async def on_server_role_delete(self, role): - await self.wait_until_dbonline() if role.server.me not in role.server.members: return await self.database.update_guild(role.server) await self.socketio.on_guild_role_delete(role) async def on_server_role_update(self, rolebefore, roleafter): - await self.wait_until_dbonline() await self.database.update_guild(roleafter.server) await self.socketio.on_guild_role_update(roleafter) async def on_channel_delete(self, channel): - await self.wait_until_dbonline() await self.database.update_guild(channel.server) await self.socketio.on_channel_delete(channel) async def on_channel_create(self, channel): - await self.wait_until_dbonline() await self.database.update_guild(channel.server) await self.socketio.on_channel_create(channel) async def on_channel_update(self, channelbefore, channelafter): - await self.wait_until_dbonline() await self.database.update_guild(channelafter.server) await self.socketio.on_channel_update(channelafter) async def on_member_join(self, member): - await self.wait_until_dbonline() await self.database.update_guild_member(member, active=True, banned=False) await self.socketio.on_guild_member_add(member) async def on_member_remove(self, member): - await self.wait_until_dbonline() await self.database.update_guild_member(member, active=False, banned=False) await self.socketio.on_guild_member_remove(member) async def on_member_update(self, memberbefore, memberafter): - await self.wait_until_dbonline() await self.database.update_guild_member(memberafter) await self.socketio.on_guild_member_update(memberafter) async def on_member_ban(self, member): - await self.wait_until_dbonline() 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): - await self.wait_until_dbonline() await self.database.unban_server_user(user, server) async def on_server_emojis_update(self, before, after): - await self.wait_until_dbonline() if len(after) == 0: await self.database.update_guild(before[0].server) await self.socketio.on_guild_emojis_update(before) @@ -233,7 +188,6 @@ class Titan(discord.Client): await self.socketio.on_guild_emojis_update(after) async def on_webhooks_update(self, server): - await self.wait_until_dbonline() await self.database.update_guild(server) async def on_socket_raw_receive(self, msg): diff --git a/discordbot/titanembeds/database/__init__.py b/discordbot/titanembeds/database/__init__.py index 082c12d..ec4e372 100644 --- a/discordbot/titanembeds/database/__init__.py +++ b/discordbot/titanembeds/database/__init__.py @@ -7,7 +7,6 @@ from sqlalchemy.ext.declarative import declarative_base import json import discord -import time Base = declarative_base() @@ -16,7 +15,6 @@ from titanembeds.database.messages import Messages from titanembeds.database.guild_members import GuildMembers from titanembeds.database.unauthenticated_users import UnauthenticatedUsers from titanembeds.database.unauthenticated_bans import UnauthenticatedBans -from titanembeds.database.keyvalue_properties import KeyValueProperties from titanembeds.utils import get_message_author, get_message_mentions, get_webhooks_list, get_emojis_list, get_roles_list, get_channels_list, list_role_ids @@ -289,15 +287,3 @@ class DatabaseInterface(object): dbuser.revoked = True session.commit() return "Successfully kicked **{}#{}**!".format(dbuser.username, dbuser.discriminator) - - async def send_webserver_heartbeat(self): - async with threadpool(): - with self.get_session() as session: - key = "bot_heartbeat" - q = session.query(KeyValueProperties).filter(KeyValueProperties.key == key) - if q.count() == 0: - session.add(KeyValueProperties(key=key, value=time.time())) - else: - firstobj = q.first() - firstobj.value = time.time() - session.commit() diff --git a/discordbot/titanembeds/database/keyvalue_properties.py b/discordbot/titanembeds/database/keyvalue_properties.py deleted file mode 100644 index 32ec8d7..0000000 --- a/discordbot/titanembeds/database/keyvalue_properties.py +++ /dev/null @@ -1,17 +0,0 @@ -from titanembeds.database import db, Base -import datetime - -class KeyValueProperties(Base): - __tablename__ = "keyvalue_properties" - id = db.Column(db.Integer, primary_key=True) # Auto incremented id - key = db.Column(db.String(255), nullable=False) # Property Key - value = db.Column(db.Text()) # Property value - expiration = db.Column(db.TIMESTAMP) # Suggested Expiration for value (None = no expire) in secs - - def __init__(self, key, value, expiration=None): - self.key = key - self.value = value - if expiration: - self.expiration = datetime.now() + timedelta(seconds = expiration) - else: - self.expiration = None \ No newline at end of file diff --git a/webapp/titanembeds/app.py b/webapp/titanembeds/app.py index 72bb0cc..93aaaaf 100644 --- a/webapp/titanembeds/app.py +++ b/webapp/titanembeds/app.py @@ -2,7 +2,7 @@ from config import config from .database import db from flask import Flask, render_template, request, session, url_for, redirect, jsonify from flask_sslify import SSLify -from titanembeds.utils import rate_limiter, discord_api, bot_alive, socketio +from titanembeds.utils import rate_limiter, discord_api, socketio from .blueprints import api, user, admin, embed, gateway import os from titanembeds.database import get_administrators_list @@ -53,5 +53,4 @@ def before_request(): @app.context_processor def context_processor(): - bot_status = bot_alive() - return {"bot_status": bot_status, "devs": get_administrators_list()} \ No newline at end of file + return {"devs": get_administrators_list()} \ No newline at end of file diff --git a/webapp/titanembeds/templates/embed.html.j2 b/webapp/titanembeds/templates/embed.html.j2 index 52a9961..c26ccbb 100644 --- a/webapp/titanembeds/templates/embed.html.j2 +++ b/webapp/titanembeds/templates/embed.html.j2 @@ -24,7 +24,6 @@ {% endif %} - {% include 'nobot_header.html.j2' %}