cosmicbot/bot.py

33 lines
1.2 KiB
Python

import teambot,tasks,rss,time
class CosmicBot(teambot.Handler):
def __init__(self,bot):
super(CosmicBot,self).__init__(bot)
self.tasks = tasks.TaskPool()
self.tasks.add_coroutine(self.check_rss,60,dict(url="https://cosmic.voyage/rss.xml",known=[],channel="#cosmic"))
self.tasks.load_state(0)
def on_connection_established(self):
self.tasks.run()
def check_rss(self,state,base_state):
memory = state["known"]
newtrans = rss.fetchNew(state["url"],memory)
if newtrans:
memory.extend(x.guid for x in newtrans)
for trans in newtrans:
self.say(state["channel"],"Transmission recieved: {transmission.title} ({transmission.link})".format(transmission=trans))
time.sleep(1)
state["known"]=memory
return state
def on_pubmsg(self,channel,nick,text):
if self.event.source.userhost == "khuxkm@sudoers.tilde.team" and text.strip() == "!down":
self.tasks.stop()
self.tasks.save_state(0)
self._bot.die("Stopping...")
elif self.event.source.userhost == "khuxkm@sudoers.tilde.team" and text.strip() == "!check":
self.tasks.states[0]=self.check_rss(self.tasks.states[0],self.tasks.base_state)
if __name__=="__main__":
channels = "#cosmic".split()
bot = teambot.TeamBot(channels,"cosmicbot","localhost",chandler=CosmicBot)
bot.start()