diff --git a/Chatbot.py b/Chatbot.py index 7c4097a..45a4ce0 100644 --- a/Chatbot.py +++ b/Chatbot.py @@ -20,10 +20,32 @@ class Chatbot(object): np.save("markov_dict.npy", initFile) except: print("ERROR! - Could not open or create Markov chat file") + + + #Do stuff when a message comes in: + @self.bot.event + @asyncio.coroutine + def on_message(message): + if message.author.bot: + return + + if message.content.strip()[0] != '!': #don't respond to a command with a chat + if message.content.lower() == "nihao": + yield from message.channel.send("kankerlauw") + return + + if "DMChannel" in type(message.channel).__name__: #respond to messages in PM channels + yield from self.respondToMessage(message) + else: + pass #TODO respond to other channels at random + + #process commands: + yield from self.bot.process_commands(message) + def __check(self, ctx): return True - #TODO laat Stroop reageren op persoonlijke berichten + #Make word pairs: def makePairs(self, corpus): @@ -109,8 +131,7 @@ class Chatbot(object): return newRespPairDict - - + @commands.command(pass_context=True, hidden=True) @asyncio.coroutine def trainmarkovchat(self, ctx, channelId=409751773595566081, nrOfMessages=50): @@ -176,15 +197,6 @@ class Chatbot(object): Stroop leert van onze gesprekken. Hij kan zich nu voordoen als een van ons. Soort van. """ - MAX_N_WORDS = 100 #Don't use more words than this - - #Get dictionary/Markov chain - wordDict = np.load('markov_dict.npy').item() - wordPairDict = wordDict['pairs'] - wordTrairDict = wordDict['trairs'] - responsePairDict = wordDict['responsePairs'] - #TODO store in global variable to minimise IO? - #Get previous message to respond to messageIterator = ctx.channel.history(limit=3, before=None, after=None, reverse=False, around=None) messageToRespondTo = yield from messageIterator.next() #this will be the !reageer command. skip @@ -203,6 +215,29 @@ class Chatbot(object): except: pass + response = self.getResponseForMessage(wordsToRespondTo) + yield from ctx.channel.send(response) + + #Respond to a message. + #Assumes the message isn't a command + def respondToMessage(self, message): + wordsToRespondTo = message.content.strip().split() + + response = self.getResponseForMessage(wordsToRespondTo) + yield from message.channel.send(response) + + #Gets a response + def getResponseForMessage(self, wordsToRespondTo): + + MAX_N_WORDS = 100 #Don't use more words than this + + #Get dictionary/Markov chain + wordDict = np.load('markov_dict.npy').item() + wordPairDict = wordDict['pairs'] + wordTrairDict = wordDict['trairs'] + responsePairDict = wordDict['responsePairs'] + #TODO store in global variable to minimise IO? + #Wtart with chain = [self.START_OF_MESSAGE] @@ -250,5 +285,6 @@ class Chatbot(object): chain[n] = "@jemoeder" #print(' '.join(chain)) - yield from ctx.channel.send(' '.join(chain)) + #yield from ctx.channel.send(' '.join(chain)) + return ' '.join(chain) \ No newline at end of file