From d5a03d0787b62379c67b58ab765946628ebcaf37 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Fri, 27 Mar 2020 12:01:54 +1300 Subject: [PATCH] Cache progress --- bot.py | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index 8783e0c..a7622d6 100644 --- a/bot.py +++ b/bot.py @@ -1,26 +1,59 @@ import discord import os import re +import time from dotenv import load_dotenv # https://discordpy.readthedocs.io/en/latest/api.html -class MyClient(discord.Client): +class DiscordKeywordBot(discord.Client): + CACHE_TTL = 900 + CACHE_DICT = {} + CACHE_LAST_PURGE = 0 + async def on_ready(self): print('Logged on as', self.user) async def on_message(self, message): if message.channel.id == int(os.getenv("SOURCE_CHANNEL")): if re.match(os.getenv("REGEX_MATCH"), message.content, re.IGNORECASE): - chan_list = [x.strip() for x in os.getenv("DISCORD_CHANNELS").split(',')] - for channel_id in chan_list: - channel = client.get_channel(int(channel_id)) - await channel.send(message.content) + DiscordKeywordBot.cache_purge() + if DiscordKeywordBot.cache_check(message.content): + chan_list = [x.strip() for x in os.getenv("DISCORD_CHANNELS").split(',')] + for channel_id in chan_list: + channel = client.get_channel(int(channel_id)) + await channel.send(message.content) + + def cache_check(msg): + if msg in DiscordKeywordBot.CACHE_DICT: + """ Has been sent before.. Check TTL """ + timestamp = DiscordKeywordBot.CACHE_DICT.get(msg) + if timestamp <=(int(time.time()) + DiscordKeywordBot.CACHE_TTL): + """ TTL Expired. """ + DiscordKeywordBot.CACHE_DICT[msg] = int(time.time()) + return True + else: + """ TTL Not Expired """ + return False + else: + DiscordKeywordBot.CACHE_DICT[msg] = int(time.time()) + return True + + def cache_purge(): + purge_timer = int(time.time()) - (DiscordKeywordBot.CACHE_TTL * 2) + if DiscordKeywordBot.CACHE_LAST_PURGE <= purge_timer: + print("Purging cache!!!") + DiscordKeywordBot.CACHE_LAST_PURGE = int(time.time()) + """ Lets Purge!!! """ + for uniq_msg in DiscordKeywordBot.CACHE_DICT: + if DiscordKeywordBot.CACHE_DICT.get(uniq_msg) <= purge_timer: + DiscordKeywordBot.CACHE_DICT.pop(msg, None) + load_dotenv() while True: try: - client = MyClient() + client = DiscordKeywordBot() client.run(os.getenv("DISCORD_TOKEN"), bot=False) except: print("Error Occurred... retrying")