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

@ -27,53 +27,34 @@ class Cayenne(callbacks.Plugin):
threaded = True
last_message_timestamp = False
cat_facts = []
def __init__(self, irc):
self.__parent = super(Cayenne, self)
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):
"""
Get a random cat fact
"""
"""
data = requests.get("https://catfact.ninja/fact").json()
return data['fact']
def message_contains_trigger_word(self, message):
"""
Check prefined list of trigger words and return
which one was found, if any
"""
words = self.registryValue("triggerWords")
if words:
words = [word.strip() for word in words]
if words:
for word in words:
if word and word in message:
return word
else:
self.log.error("Cayenne: no trigger words set apparently")
return False
def get_link(self):
"""
Query cat URL to get a random link
@ -88,63 +69,51 @@ class Cayenne(callbacks.Plugin):
self.log.error("Received unexpected response from http://edgecats.net/random")
except:
self.log.exception("Error fetching URL")
def doPrivmsg(self, irc, msg):
"""
Checks each channel message to see if it contains a trigger word
"""
channel = msg.args[0]
is_channel = irc.isChannel(channel)
is_ctcp = ircmsgs.isCtcp(msg)
is_ctcp = ircmsgs.isCtcp(msg)
message = msg.args[1]
bot_nick = irc.nick
# Check origin nick to make sure the trigger
# didn"t come from the bot.
origin_nick = msg.nick
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
# 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):
if type(message) is str and len(message):
fact_chance = int(self.registryValue("factChance"))
link_chance = int(self.registryValue("linkChance"))
link_chance = int(self.registryValue("linkChance"))
throttle_seconds = int(self.registryValue("throttleInSeconds"))
triggered = self.message_contains_trigger_word(message)
now = datetime.datetime.now()
seconds = 0
if self.last_message_timestamp:
if self.last_message_timestamp:
seconds = (now - self.last_message_timestamp).total_seconds()
throttled = seconds < throttle_seconds
else:
throttled = False
if triggered is not False:
self.log.debug("Cayenne triggered because message contained %s" % (triggered))
fact_rand = random.randrange(0, 100) <= fact_chance
link_rand = random.randrange(0, 100) <= link_chance
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)))
else:
self.last_message_timestamp = now
if fact_rand:
output = self.get_fact()
if link_rand:
output = self.get_link()
if output:
irc.sendMsg(ircmsgs.privmsg(channel, output))
else:
self.log.error("Cayenne: error retrieving output")
Class = Cayenne