🪲 Fixed again the folder creation for new chats.

🪲 Fixed /period giving an error message despite successfully
saving the new period.
🪲 Fixed /answer not updating.
This commit is contained in:
vylion 2019-03-31 14:55:51 +02:00
parent d075624263
commit f015e4bc60
4 changed files with 41 additions and 30 deletions

View file

@ -24,24 +24,27 @@ class Archivist(object):
self.readOnly = readOnly
self.filterCids = filterCids
self.bypass = bypass
self.scribeFolder = chatdir + "chat_{tag}"
self.scribePath = chatdir + "chat_{tag}/{file}{ext}"
def openfile(self, filename, mode):
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as e:
if e.errno != errno.EEXIST:
raise
return open(filename, mode)
def store(self, tag, log, gen):
scribefolder = self.scribeFolder.format(tag=tag)
cardfile = self.scribePath.format(tag=tag, file="card", ext=".txt")
if self.readOnly:
return
file = self.openfile(self.scribePath.format(tag=tag, file="card", ext=".txt"), 'w')
try:
if not os.path.exists(scribefolder):
os.makedirs(scribefolder, exist_ok=True)
self.logger.info("Storing a new chat. Folder {} created.".format(scribefolder))
except:
self.logger.error("Failed creating {} folder.".format(scribefolder))
return
file = open(cardfile, 'w')
file.write(log)
file.close()
file = self.openfile(self.scribePath.format(tag=tag, file="record", ext=self.chatext), 'w')
if gen is not None:
recordfile = self.scribePath.format(tag=tag, file="record", ext=self.chatext)
file = open(recordfile, 'w')
file.write(gen)
file.close()
@ -80,8 +83,8 @@ class Archivist(object):
file.close()
return Markov.loads(record)
except:
self.logger.error("Parrot file {} not found. Assuming first time parrot.".format(filepath))
return Markov()
self.logger.error("Parrot file {} not found.".format(filepath))
return None
def wakeScriptorium(self):
scriptorium = {}

View file

@ -46,9 +46,10 @@ class Scribe(object):
self.countdown = self.chat.freq
self.logger = self.archivist.logger
def FromChat(chat, archivist):
def FromChat(chat, archivist, newchat=False):
chatlog = Chatlog(chat.id, chat.type, getTitle(chat))
return Scribe(chatlog, archivist)
scribe = Scribe(chatlog, archivist)
return scribe
def FromData(data, archivist):
return None

View file

@ -2,6 +2,7 @@
import random
from scribe import Scribe
from markov import Markov
from telegram.error import *
def send(bot, cid, text, replying=None, format=None, logger=None, **kwargs):
@ -78,7 +79,7 @@ class Speaker(object):
def getScribe(self, chat):
cid = str(chat.id)
if not cid in self.scriptorium:
scribe = Scribe.FromChat(chat, self.archivist)
scribe = Scribe.FromChat(chat, self.archivist, newchat=True)
self.scriptorium[cid] = scribe
return scribe
else:
@ -103,6 +104,16 @@ class Speaker(object):
else:
scribe.store(self.parrot.dumps())
def loadParrot(self, scribe):
newParrot = False
self.parrot = self.archivist.wakeParrot(scribe.cid())
if self.parrot is None:
newParrot = True
self.parrot = Markov()
scribe.teachParrot(self.parrot)
self.store(scribe)
return newParrot
def read(self, bot, update):
chat = update.message.chat
scribe = self.getScribe(chat)
@ -122,9 +133,7 @@ class Speaker(object):
rid = scribe.getReference() if random.random() <= self.reply else None
self.say(bot, scribe, replying=rid)
elif (scribe.freq() - scribe.countdown) % self.archivist.saveCount == 0:
self.parrot = self.archivist.wakeParrot(scribe.cid())
scribe.teachParrot(self.parrot)
self.store(scribe)
self.loadParrot(scribe)
def speak(self, bot, update):
chat = (update.message.chat)
@ -158,9 +167,7 @@ class Speaker(object):
if self.filterCids is not None and not scribe.cid() in self.filterCids:
return
self.parrot = self.archivist.wakeParrot(scribe.cid())
scribe.teachParrot(self.parrot)
scribe.store(self.parrot)
self.loadParrot(scribe)
try:
send(bot, scribe.cid(), self.speech(scribe), replying, logger=self.logger, **kwargs)
if self.bypass:
@ -204,7 +211,7 @@ class Speaker(object):
freq = int(words[1])
freq = scribe.setFreq(freq)
update.message.reply_text("Period of speaking set to {}.".format(freq))
scribe.store()
scribe.store(None)
except:
update.message.reply_text("Format was confusing; period unchanged from {}.".format(scribe.freq()))
@ -223,10 +230,10 @@ class Speaker(object):
update.message.reply_text("You do not have permissions to do that.")
return
try:
afreq = int(words[1])
afreq = scribe.setAnswer(afreq)
update.message.reply_text("Answer probability set to {}.".format(afreq))
scribe.store()
answ = float(words[1])
answ = scribe.setAnswer(answ)
update.message.reply_text("Period of speaking set to {}.".format(answ))
scribe.store(None)
except:
update.message.reply_text("Format was confusing; answer probability unchanged from {}.".format(scribe.answer()))

View file

@ -83,7 +83,7 @@ def main():
chatext=".vls",
admin=args.admin_id,
filterCids=filterCids,
readOnly=True
readOnly=False
)
speakerbot = Speaker("velasco", "@" + username, archivist, logger, wakeup=args.wakeup)