This repository has been archived on 2018-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
sotdbot/bot.py

71 lines
2.2 KiB
Python

import teambot, time, datastore
from urllib.request import urlopen
USAGE = dict(admin="Stop trying to use this command!",sotd="{}sotd <link> <name>",listsotd="{}listsotd")
PUBLIC_COMMANDS = ["sotd","listsotd"]
SOTDS = datastore.DataStore("sotd.json")
def get_title(link):
try:
title = str(urlopen(link).read()).split("<title>", 1)[1].split("</title>", 1)[0]
if "YouTube" in title:
return title.split(" - YouTube", 1)[0]
return title
except Exception as e:
print(e)
return link
class SOTDBot(teambot.Handler,teambot.CommandHandlerMixin):
def __init__(self,*args):
self.prefix = "!"
self.botop = "khuxkm"
super(SOTDBot,self).__init__(*args)
def on_pubmsg(self,target,nick,text):
if not text.startswith(self.prefix):
if text in ("!botlist","!rollcall"):
self.do_botlist()
return
self.target = target if target!=self._bot.bot_nick else nick
self.sourcenick = nick
args = text[len(self.prefix):].split()
cmd = args.pop(0)
if hasattr(self,"do_"+cmd.lower()):
try:
getattr(self,"do_"+cmd.lower())(*args)
except TypeError as e:
self.say(self.target,"{}: Usage: {}".format(nick,USAGE[cmd.lower()].format(self.prefix)))
def do_admin(self,*args):
if self.sourcenick!=self.botop:
self.say(self.target,self.sourcenick+": You can't tell me what to do!")
return
args = list(args)
subcmd = args.pop(0)
if subcmd=="down":
self._bot.die("Bye bye!")
elif subcmd=="prefix":
self.prefix = args[0]
def do_botlist(self):
self.say(self.target,"Maintainer: khuxkm | Song-of-the-day bot | commands: {}".format(", ".join("{}{}".format(self.prefix,cmd) for cmd in PUBLIC_COMMANDS)))
def do_sotd(self,link,*name):
if name:
name = " ".join(name)
else:
name = get_title(link)
SOTDS[self.sourcenick] = (name,link)
self.say(self.target,self.sourcenick+": Your SOTD has been set to {}.".format(name))
def do_listsotd(self):
for k in SOTDS.data:
sotd = SOTDS[k]
t = time.strftime("%B %d, %Y",time.localtime(SOTDS.getSetTime(k)))
self.say(self.target,self.sourcenick+": {} - {} ({}, set {})".format(k,sotd[0],sotd[1],t))
if __name__=="__main__":
channels = "#bots #meta #team".split()
bot = teambot.TeamBot(channels,"sotdbot","localhost",chandler=SOTDBot)
bot.start()