From f46d33c9437af9ac753b1f3d97fc300ac0eb228a Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Fri, 22 Sep 2017 01:39:43 +0000 Subject: [PATCH] Delete the messages before fetching new messages --- discordbot/fetch_last_messages.py | 2 ++ discordbot/titanembeds/database/__init__.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/discordbot/fetch_last_messages.py b/discordbot/fetch_last_messages.py index d2635f2..bc9b8a8 100644 --- a/discordbot/fetch_last_messages.py +++ b/discordbot/fetch_last_messages.py @@ -6,6 +6,7 @@ import aiohttp import asyncio import sys import logging +import gc logging.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') logging.getLogger('TitanBot') logging.getLogger('sqlalchemy') @@ -72,6 +73,7 @@ class Titan(discord.Client): try: if str(channel.type) == "text": print("Processing channel: ID-{} Name-'{}' ServerID-{} Server-'{}'".format(channel.id, channel.name, channel.server.id, channel.server.name)) + await self.database.delete_all_messages_from_channel(channel.id) async for message in self.logs_from(channel, limit=50, reverse=True): await self.database.push_message(message) except: diff --git a/discordbot/titanembeds/database/__init__.py b/discordbot/titanembeds/database/__init__.py index ec4e372..fe4ef23 100644 --- a/discordbot/titanembeds/database/__init__.py +++ b/discordbot/titanembeds/database/__init__.py @@ -287,3 +287,9 @@ class DatabaseInterface(object): dbuser.revoked = True session.commit() return "Successfully kicked **{}#{}**!".format(dbuser.username, dbuser.discriminator) + + async def delete_all_messages_from_channel(self, channel_id): + async with threadpool(): + with self.get_session() as session: + session.query(Messages).filter(Messages.channel_id == channel_id).delete() + session.commit() \ No newline at end of file