aboutsummaryrefslogtreecommitdiff
path: root/mcwrapper/mod.py
blob: bb580e8f7f40fd00523b9d3c1b239b87d8d7d31d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# vim: expandtab ft=python ts=4 sw=4 sts=4:
import random

from . import alarm
from . import prints


class MoD:
    "Message of the day handler"
    def __init__(self, mcwrapper, file, period=900):
        self.mcwrapper = mcwrapper
        self.file = file
        alarm.set("mod-time", period, self.__handler__, repeat=True)

    def clean(self):
        alarm.unset("mod-time")

    def __handler__(self):
        lines = []
        try:
            with open(self.file, "r") as f:
                lines = f.readlines()
        except OSError as e:
            prints.warning("Loading of MoD file failed: " + str(e))
            return
        if len(lines) > 0:
            i = random.randint(0, len(lines) - 1)
            self.mcwrapper.write_to_terminal("/say " + lines[i].rstrip() +
                                             "\n")