From d7b7cdf04c4a04eae1a44b53a8091fd1cda9d90f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sun, 23 Oct 2016 15:17:07 +0200 Subject: Load mod file every time new message is going to be printed --- mcwrapper/mod.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'mcwrapper/mod.py') diff --git a/mcwrapper/mod.py b/mcwrapper/mod.py index 3d3967c..73200f1 100644 --- a/mcwrapper/mod.py +++ b/mcwrapper/mod.py @@ -10,20 +10,17 @@ class MoD: def __init__(self, mcwrapper, file, period=900): self.mcwrapper = mcwrapper self.file = file - self.load_mods() alarm.set("mod-time", period, self.__handler__, repeat=True) - def load_mods(self): - "Loads messages from self.file" + def __handler__(self): + lines = [] try: with open(self.file, "r") as f: - self.lines = f.readlines() + lines = f.readlines() except OSError as e: prints.warning("Loading of MoD file failed: " + str(e)) - self.lines = [] - - def __handler__(self): - if len(self.lines) > 0: - i = random.randint(0, len(self.lines) - 1) - self.mcwrapper.write_to_terminal("/say " + self.lines[i].rstrip() - + "\n") + return + if len(lines) > 0: + i = random.randint(0, len(lines) - 1) + self.mcwrapper.write_to_terminal("/say " + lines[i].rstrip() + + "\n") -- cgit v1.2.3