This commit is contained in:
Gordon Shumway 2019-04-14 00:37:40 -04:00 committed by GitHub
parent c460399e33
commit ff1e7f596b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,22 +32,6 @@ class Cayenne(callbacks.Plugin):
self.__parent = super(Cayenne, self) self.__parent = super(Cayenne, self)
self.__parent.__init__(irc) self.__parent.__init__(irc)
self.read_cat_facts_file()
def read_cat_facts_file(self):
"""
Read the cat facts file into a list so we can retrieve at random later
"""
try:
dir = os.path.dirname(__file__)
fact_file_path = os.path.join(dir, "./facts.txt")
for line in open(fact_file_path):
self.cat_facts.append(line.rstrip("\n"))
except:
self.log.error("Cayenne: error reading cat facts file: %s" % fact_file_path)
def get_fact(self): def get_fact(self):
""" """
Get a random cat fact Get a random cat fact
@ -61,17 +45,14 @@ class Cayenne(callbacks.Plugin):
which one was found, if any which one was found, if any
""" """
words = self.registryValue("triggerWords") words = self.registryValue("triggerWords")
if words: if words:
words = [word.strip() for word in words] words = [word.strip() for word in words]
if words: if words:
for word in words: for word in words:
if word and word in message: if word and word in message:
return word return word
else: else:
self.log.error("Cayenne: no trigger words set apparently") self.log.error("Cayenne: no trigger words set apparently")
return False return False
def get_link(self): def get_link(self):
@ -98,13 +79,10 @@ class Cayenne(callbacks.Plugin):
is_ctcp = ircmsgs.isCtcp(msg) is_ctcp = ircmsgs.isCtcp(msg)
message = msg.args[1] message = msg.args[1]
bot_nick = irc.nick bot_nick = irc.nick
# Check origin nick to make sure the trigger # Check origin nick to make sure the trigger
# didn"t come from the bot. # didn"t come from the bot.
origin_nick = msg.nick origin_nick = msg.nick
is_message_from_self = origin_nick.lower() == bot_nick.lower() is_message_from_self = origin_nick.lower() == bot_nick.lower()
# Only react to messages/actions in a channel and to messages that aren't from # Only react to messages/actions in a channel and to messages that aren't from
# the bot itself. # the bot itself.
if is_channel and not is_ctcp and not is_message_from_self and self.registryValue('enable', channel): if is_channel and not is_ctcp and not is_message_from_self and self.registryValue('enable', channel):
@ -115,36 +93,27 @@ class Cayenne(callbacks.Plugin):
triggered = self.message_contains_trigger_word(message) triggered = self.message_contains_trigger_word(message)
now = datetime.datetime.now() now = datetime.datetime.now()
seconds = 0 seconds = 0
if self.last_message_timestamp: if self.last_message_timestamp:
seconds = (now - self.last_message_timestamp).total_seconds() seconds = (now - self.last_message_timestamp).total_seconds()
throttled = seconds < throttle_seconds throttled = seconds < throttle_seconds
else: else:
throttled = False throttled = False
if triggered is not False: if triggered is not False:
self.log.debug("Cayenne triggered because message contained %s" % (triggered)) self.log.debug("Cayenne triggered because message contained %s" % (triggered))
fact_rand = random.randrange(0, 100) <= fact_chance fact_rand = random.randrange(0, 100) <= fact_chance
link_rand = random.randrange(0, 100) <= link_chance link_rand = random.randrange(0, 100) <= link_chance
if fact_rand or link_rand: if fact_rand or link_rand:
if throttled: if throttled:
self.log.info("Cayenne throttled. Not meowing: it has been %s seconds" % (round(seconds, 1))) self.log.info("Cayenne throttled. Not meowing: it has been %s seconds" % (round(seconds, 1)))
else: else:
self.last_message_timestamp = now self.last_message_timestamp = now
if fact_rand: if fact_rand:
output = self.get_fact() output = self.get_fact()
if link_rand: if link_rand:
output = self.get_link() output = self.get_link()
if output: if output:
irc.sendMsg(ircmsgs.privmsg(channel, output)) irc.sendMsg(ircmsgs.privmsg(channel, output))
else: else:
self.log.error("Cayenne: error retrieving output") self.log.error("Cayenne: error retrieving output")
Class = Cayenne Class = Cayenne