From 00de2706c09667cb916e2053a5b17c5b098d8ce1 Mon Sep 17 00:00:00 2001 From: JP Date: Sun, 18 Nov 2018 13:57:44 +0100 Subject: [PATCH] de Guus filter --- Chatbot.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Chatbot.py b/Chatbot.py index 0025146..e5be78f 100644 --- a/Chatbot.py +++ b/Chatbot.py @@ -3,6 +3,7 @@ import discord from discord.ext import commands import numpy as np import random +from datetime import datetime class Chatbot(object): """Een heuse chatbot. Under construction...""" @@ -12,6 +13,8 @@ class Chatbot(object): self.voice_states = {} self.END_OF_MESSAGE = "" self.START_OF_MESSAGE = "" + self.pmLast = datetime(2018, 1, 1, 1, 1, 1) + self.pmCount = 0 try: np.load('markov_dict.npy') except FileNotFoundError: @@ -35,7 +38,10 @@ class Chatbot(object): return if "DMChannel" in type(message.channel).__name__: #respond to messages in PM channels - yield from self.learnFromMessagesInChannel(message.channel.id, 1) + if (self.isAllowedToLearnFromPm(message)): #avoid spam-learning + self.pmCount += 1 + self.pmLast = datetime.now() + yield from self.learnFromMessagesInChannel(message.channel.id, 1) yield from self.respondToMessage(message) elif message.channel.id == self.bot.hdChannels["belangrijk"]: #don't abuse the "belangrijk" channel pass @@ -54,7 +60,20 @@ class Chatbot(object): def __check(self, ctx): return True - + + #The "Guus filter" + #Don't allow to learn from very large personal messages + #Only learn from 10 personal messages per hour at most + def isAllowedToLearnFromPm(self, message): + if len(message.content.strip()) > 150: + return False + diff = datetime.now() - self.pmLast + if diff.days > 0 or diff.seconds > 3600: + self.pmCount = 0 + return True + if self.pmCount > 10: + return False + return True #Make word pairs: def makePairs(self, corpus): @@ -209,7 +228,7 @@ class Chatbot(object): @commands.command(pass_context=True, hidden=False) @asyncio.coroutine def reageer(self, ctx): - """UNDER CONSTRUCTION. Laat Stroop ergens op reageren. + """Laat Stroop ergens op reageren. Stroop leert van onze gesprekken. Hij kan zich nu voordoen als een van ons. Soort van. """