38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
from Module import Module
|
|
from irclib import irclib
|
|
from ConfigParser import NoOptionError
|
|
|
|
class FactFile(Module):
|
|
# Returns a random fact/quote/whatever from one or more files
|
|
|
|
def __init__(self, config, server, modlist, rehash):
|
|
super(FactFile, self).__init__(config, server, modlist, rehash)
|
|
modlist.append(self)
|
|
|
|
def register_handlers(self, server):
|
|
server.add_global_handler('pubmsg', self.on_pubmsg)
|
|
server.add_global_handler('privmsg', self.on_privmsg)
|
|
|
|
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
|
whats = what.split(' ')
|
|
try:
|
|
filename = self.config.get('fact', whats[0])
|
|
|
|
# open file
|
|
if os.path.isfile(filename):
|
|
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
|
|
with open(filename, 'r') as file:
|
|
facts = file.readlines()
|
|
to_print = facts[random.randint(0, len(facts))]
|
|
|
|
# return text
|
|
if replypath is None:
|
|
return to_print.rstrip()
|
|
else:
|
|
connection.privmsg(replypath, to_print.rstrip())
|
|
else:
|
|
print('filename in config file for \'' + whats[0] + '\' is wrong')
|
|
|
|
except NoOptionError: pass
|
|
|