radiobot/bot.py

90 lines
3.7 KiB
Python
Raw Permalink Normal View History

2018-11-26 20:05:36 +00:00
import teambot, sys, tasks, json
2018-11-25 22:52:38 +00:00
import os.path as fs
2018-12-01 06:00:03 +00:00
def loadSubscribers():
if not fs.exists("subscribers.json"):
return []
with open("subscribers.json") as f:
return json.load(f)["subscribers"]
def saveSubscribers(subscribers):
with open("subscribers.json","w") as f:
return json.dump(dict(subscribers=subscribers),f)
2018-11-25 22:52:38 +00:00
class RadioBot(teambot.Handler):
2018-11-26 01:19:43 +00:00
def __init__(self,*args):
super(RadioBot,self).__init__(*args)
self.tasks = tasks.TaskPool(handler=self)
2018-12-01 09:23:20 +00:00
self.tasks.add_coroutine(self.check_nowplaying,1,dict(dj_live=False,dj=None,song="",listeners=0))
2018-11-26 20:05:36 +00:00
self.channels = [x.split()[0] for x in self._bot.chanlist]
2018-12-01 06:00:03 +00:00
self.subscribers = loadSubscribers()
2018-11-26 01:19:43 +00:00
self.tasks.run()
def check_nowplaying(self,state,basestate):
2018-12-01 03:42:04 +00:00
if not hasattr(self._bot,"conn"):
return {}
bot = basestate["handler"]
with open(fs.expanduser("~khuxkm/public_html/radiobot/now_playing.json")) as f:
resp = json.loads(f.read().rstrip())
if resp["dj"] is not None and not state.get("dj_live",False):
2018-12-01 09:25:53 +00:00
state["dj_live"]=True
2018-12-01 06:00:03 +00:00
notify = [bot.channels[0]]
notify.extend(self.subscribers)
for channel in notify:
bot.say(channel,"{} is now live!".format(resp["dj"]))
state["dj"] = resp["dj"]
if resp["dj"] is None and state.get("dj_live",True):
state["dj_live"]=False
2018-12-01 03:42:04 +00:00
if resp["song"]!=state.get("song") or resp["listeners"]!=state.get("listeners"):
2018-12-01 09:25:53 +00:00
bot.say(bot.channels[0],"now playing: {} ({!s} listeners{})".format(resp["song"],resp["listeners"],(", played by "+resp["dj"] if state["dj_live"] else "")))
2018-12-01 03:42:04 +00:00
state["song"]=resp["song"]
state["listeners"]=resp["listeners"]
basestate["handler"].task_state = state
2018-12-01 03:42:04 +00:00
return state
2018-11-25 22:52:38 +00:00
def on_pubmsg(self,channel,nick,text):
if text.startswith(self._bot.bot_nick+": "):
cmd = text[len(self._bot.bot_nick+": "):].strip()
2018-12-01 06:02:00 +00:00
if self.event.source.userhost=="khuxkm@sudoers.tilde.team" and cmd=="down":
self.tasks.stop()
self._bot.die("Stopping...")
2018-12-01 06:02:00 +00:00
elif self.event.source.userhost=="khuxkm@sudoers.tilde.team" and cmd=="test":
2018-12-01 06:01:29 +00:00
notify = [self.channels[0]]
notify.extend(self.subscribers)
for channel in notify:
self.say(channel,"'tis but a test. ignore me")
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.")
2018-12-01 05:42:56 +00:00
elif cmd=="np":
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 "")))
2018-12-01 06:00:03 +00:00
elif cmd=="subscribe":
if nick in self.subscribers:
self.say(channel,nick+": you're already on my list!")
else:
self.subscribers.append(nick)
saveSubscribers(self.subscribers)
self.say(channel,nick+": I'll notify you when someone goes live!")
elif cmd=="unsubscribe":
if nick not in self.subscribers:
self.say(channel,nick+": you're not on my list!")
else:
self.subscribers.remove(nick)
saveSubscribers(self.subscribers)
self.say(channel,nick+": I'll leave you alone then.")
2018-12-01 05:42:56 +00:00
elif cmd=="radio": # joke command
self.say(channel,"eat the poop or die!!1")
elif cmd=="paymybills": # joke command
self.say(channel,"whaddya mean?! i'm broker than you!")
elif text.strip()=="!botlist":
self.say(channel,"Maintainer: khuxkm@tilde.team | your friendly neighborhood radio bot | https://tildegit.org/khuxkm/radiobot")
2018-11-25 22:52:38 +00:00
if __name__=="__main__":
if not fs.exists("channels.txt"):
print("ERROR: No channels.txt. Use channels.txt.example as an example.")
sys.exit(1)
with open("channels.txt") as f:
channels = [l.rstrip() for l in f if l.rstrip()]
bot = teambot.TeamBot(channels,"radiobot2","localhost",chandler=RadioBot)
bot.start()