add command for loading logs in to markov (via http)

This commit is contained in:
jesopo 2019-09-23 10:03:32 +01:00
parent 72ff24227d
commit e448a4abb8

View file

@ -1,6 +1,8 @@
import random import random
from src import ModuleManager, utils from src import ModuleManager, utils
NO_MARKOV = "Markov chains not enabled in this channel"
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def on_load(self): def on_load(self):
if not self.bot.database.has_table("markov"): if not self.bot.database.has_table("markov"):
@ -12,9 +14,32 @@ class Module(ModuleManager.BaseModule):
@utils.hook("received.message.channel") @utils.hook("received.message.channel")
def channel_message(self, event): def channel_message(self, event):
words = [word.lower() for word in event["message_split"]] if event["channel"].get_setting("markov", False):
self._create(event["channel"].id, event["message_split"])
@utils.hook("received.command.markovlog")
@utils.kwarg("min_args", 1)
@utils.kwarg("permission", "markovlog")
@utils.kwarg("help", "Load a message-only newline-delimited log in to this "
"channel's markov chain")
def load_log(self, event):
if not event["target"].get_setting("markov", False):
raise utils.EventError(NO_MARKOV)
page = utils.http.request(event["args_split"][0])
if page.code == 200:
for line in page.data.split("\n"):
self._create(event["target"].id, line.strip("\r").split(" "))
else:
event["stdout"].write("Failed to load log (%d)" % page.code)
def _create(self, channel_id, words):
words = list(filter(None, words))
words = [word.lower() for word in words]
words_n = len(words) words_n = len(words)
if words_n > 2 and event["channel"].get_setting("markov", False):
if not words_n > 2:
return
inserts = [] inserts = []
inserts.append([None, None, words[0]]) inserts.append([None, None, words[0]])
@ -29,12 +54,12 @@ class Module(ModuleManager.BaseModule):
frequency = self.bot.database.execute_fetchone("""SELECT frequency = self.bot.database.execute_fetchone("""SELECT
frequency FROM markov WHERE channel_id=? AND first_word=? frequency FROM markov WHERE channel_id=? AND first_word=?
AND second_word=? AND third_word=?""", AND second_word=? AND third_word=?""",
[event["channel"].id]+insert) [channel_id]+insert)
frequency = (frequency or [0])[0]+1 frequency = (frequency or [0])[0]+1
self.bot.database.execute( self.bot.database.execute(
"INSERT OR REPLACE INTO markov VALUES (?, ?, ?, ?, ?)", "INSERT OR REPLACE INTO markov VALUES (?, ?, ?, ?, ?)",
[event["channel"].id]+insert+[frequency]) [channel_id]+insert+[frequency])
def _choose(self, words): def _choose(self, words):
words, frequencies = list(zip(*words)) words, frequencies = list(zip(*words))
@ -60,7 +85,7 @@ class Module(ModuleManager.BaseModule):
def _markov_for(self, channel, stdout, stderr): def _markov_for(self, channel, stdout, stderr):
if not channel.get_setting("markov", False): if not channel.get_setting("markov", False):
stderr.write("Markov chains not enabled in this channel") stderr.write(NO_MARKOV)
else: else:
out = self._generate(channel.id) out = self._generate(channel.id)
if not out == None: if not out == None: