Make task state accessible to the handler, fix up task some, and add commands

This commit is contained in:
Robert Miles 2018-12-01 00:28:50 -05:00
parent cc9ce6041b
commit df35d00b5d
1 changed files with 18 additions and 5 deletions

23
bot.py
View File

@ -16,18 +16,31 @@ class RadioBot(teambot.Handler):
resp = json.loads(f.read().rstrip())
if resp["dj"] is not None and not state.get("dj_live",False):
bot.say(bot.channels[0],"{} is now live!".format(resp["dj"]))
state["dj"] = resp["dj"]
state["dj_live"]=True
if resp["dj"] is None and state.get("dj_live",True):
state["dj_live"]=False
if resp["song"]!=state.get("song") or resp["listeners"]!=state.get("listeners"):
bot.say(bot.channels[0],"now playing: {} ({!s} listeners{})".format(resp["song"],resp["listeners"],(", played by "+resp["dj"] if resp["dj"] is not None else "")))
state["song"]=resp["song"]
state["listeners"]=resp["listeners"]
basestate["handler"].task_state = state
return state
def on_pubmsg(self,channel,nick,text):
if nick.startswith("khuxkm") and text.rstrip()=="!down":
self.tasks.stop()
self._bot.die("Stopping...")
else:
self.say(channel,json.dumps(self.channels))
if text.startswith(self._bot.bot_nick+": "):
cmd = text[len(self._bot.bot_nick+": "):].strip()
if self.event.source.userhost=="khuxkm@oper.tilde.chat" and cmd=="down":
self.tasks.stop()
self._bot.die("Stopping...")
elif cmd=="dj":
if self.task_state["dj_live"]:
self.say(channel,nick+": {} is the current dj!".format(self.task_state["dj"]))
else:
self.say(channel,nick+": nobody is live right now.")
elif cmd=="nowplaying":
self.say(channel,nick+": now playing: {} ({!s} listeners{})".format(self.task_state["song"],self.task_state["listeners"],(", played by "+self.task_state["dj"] if self.task_state["dj_live"] else "")))
elif text.strip()=="!botlist":
self.say(channel,"Maintainer: khuxkm@tilde.team | your friendly neighborhood radio bot | https://tildegit.org/khuxkm/radiobot")
if __name__=="__main__":
if not fs.exists("channels.txt"):