90 lines
3.7 KiB
Python
90 lines
3.7 KiB
Python
import teambot, sys, tasks, json
|
|
import os.path as fs
|
|
|
|
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)
|
|
|
|
class RadioBot(teambot.Handler):
|
|
def __init__(self,*args):
|
|
super(RadioBot,self).__init__(*args)
|
|
self.tasks = tasks.TaskPool(handler=self)
|
|
self.tasks.add_coroutine(self.check_nowplaying,1,dict(dj_live=False,dj=None,song="",listeners=0))
|
|
self.channels = [x.split()[0] for x in self._bot.chanlist]
|
|
self.subscribers = loadSubscribers()
|
|
self.tasks.run()
|
|
def check_nowplaying(self,state,basestate):
|
|
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):
|
|
state["dj_live"]=True
|
|
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
|
|
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 state["dj_live"] else "")))
|
|
state["song"]=resp["song"]
|
|
state["listeners"]=resp["listeners"]
|
|
basestate["handler"].task_state = state
|
|
return state
|
|
def on_pubmsg(self,channel,nick,text):
|
|
if text.startswith(self._bot.bot_nick+": "):
|
|
cmd = text[len(self._bot.bot_nick+": "):].strip()
|
|
if self.event.source.userhost=="khuxkm@sudoers.tilde.team" and cmd=="down":
|
|
self.tasks.stop()
|
|
self._bot.die("Stopping...")
|
|
elif self.event.source.userhost=="khuxkm@sudoers.tilde.team" and cmd=="test":
|
|
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.")
|
|
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 "")))
|
|
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.")
|
|
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")
|
|
|
|
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()
|