2020-03-21 01:11:44 +00:00
|
|
|
import discord
|
|
|
|
import os
|
|
|
|
import re
|
2020-03-26 23:01:54 +00:00
|
|
|
import time
|
2020-03-21 01:11:44 +00:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
# https://discordpy.readthedocs.io/en/latest/api.html
|
|
|
|
|
2020-03-26 23:01:54 +00:00
|
|
|
class DiscordKeywordBot(discord.Client):
|
|
|
|
CACHE_TTL = 900
|
|
|
|
CACHE_DICT = {}
|
2020-03-27 19:40:17 +00:00
|
|
|
CACHE_NEXT_PURGE = int(time.time()) + 1800
|
2020-03-26 23:01:54 +00:00
|
|
|
|
2020-03-21 01:11:44 +00:00
|
|
|
async def on_ready(self):
|
|
|
|
print('Logged on as', self.user)
|
|
|
|
|
|
|
|
async def on_message(self, message):
|
2020-03-21 01:19:53 +00:00
|
|
|
if message.channel.id == int(os.getenv("SOURCE_CHANNEL")):
|
2020-03-21 01:11:44 +00:00
|
|
|
if re.match(os.getenv("REGEX_MATCH"), message.content, re.IGNORECASE):
|
2020-03-26 23:01:54 +00:00
|
|
|
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))
|
2020-03-27 19:38:42 +00:00
|
|
|
await channel.send('test: ' + message.content)
|
2020-03-26 23:01:54 +00:00
|
|
|
|
|
|
|
def cache_check(msg):
|
|
|
|
if msg in DiscordKeywordBot.CACHE_DICT:
|
|
|
|
""" Has been sent before.. Check TTL """
|
2020-03-27 19:38:42 +00:00
|
|
|
if DiscordKeywordBot.CACHE_DICT.get(msg) <= int(time.time()):
|
2020-03-26 23:01:54 +00:00
|
|
|
""" TTL Expired. """
|
2020-03-27 19:38:42 +00:00
|
|
|
DiscordKeywordBot.CACHE_DICT[msg] = (int(time.time()) + DiscordKeywordBot.CACHE_TTL)
|
2020-03-26 23:01:54 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
""" TTL Not Expired """
|
|
|
|
return False
|
|
|
|
else:
|
2020-03-27 19:38:42 +00:00
|
|
|
DiscordKeywordBot.CACHE_DICT[msg] = (int(time.time()) + DiscordKeywordBot.CACHE_TTL)
|
2020-03-26 23:01:54 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def cache_purge():
|
2020-03-27 19:38:42 +00:00
|
|
|
if DiscordKeywordBot.CACHE_NEXT_PURGE <= int(time.time()) :
|
2020-03-26 23:01:54 +00:00
|
|
|
""" Lets Purge!!! """
|
2020-03-27 19:38:42 +00:00
|
|
|
print("Total cache older than TTL*2. Purging old records.")
|
|
|
|
DiscordKeywordBot.CACHE_NEXT_PURGE = (int(time.time()) + (DiscordKeywordBot.CACHE_TTL * 2))
|
2020-03-26 23:01:54 +00:00
|
|
|
for uniq_msg in DiscordKeywordBot.CACHE_DICT:
|
2020-03-27 19:38:42 +00:00
|
|
|
if DiscordKeywordBot.CACHE_DICT.get(uniq_msg) <= int(time.time()):
|
|
|
|
""" If TTL <= now, remove from cache """
|
2020-03-26 23:01:54 +00:00
|
|
|
DiscordKeywordBot.CACHE_DICT.pop(msg, None)
|
|
|
|
|
2020-03-21 01:11:44 +00:00
|
|
|
|
|
|
|
load_dotenv()
|
2020-03-23 06:45:55 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2020-03-26 23:01:54 +00:00
|
|
|
client = DiscordKeywordBot()
|
2020-03-23 06:45:55 +00:00
|
|
|
client.run(os.getenv("DISCORD_TOKEN"), bot=False)
|
|
|
|
except:
|
|
|
|
print("Error Occurred... retrying")
|
|
|
|
|