Add new command system

This commit is contained in:
Robert Miles 2018-12-02 02:43:01 +00:00
parent 0071f83362
commit 494917a441
1 changed files with 25 additions and 4 deletions

29
bot.py
View File

@ -1,11 +1,17 @@
import teambot,tasks,rss,time
import teambot,tasks,rss,time,sys
class CosmicBot(teambot.Handler):
def __init__(self,bot):
super(CosmicBot,self).__init__(bot)
self.prefix = "!"
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)
self.commands = dict()
self.register_command("botlist",self.on_botlist)
self.register_command("admin",self.on_admin,True)
def register_command(self,name,action,is_admin=False):
self.commands[name] = dict(action=action,is_admin=is_admin)
def on_connection_established(self):
self.tasks.run()
def check_rss(self,state,base_state):
@ -19,12 +25,27 @@ class CosmicBot(teambot.Handler):
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":
if not text.startswith(self.prefix):
return
args = text[len(self.prefix):].strip().split()
cmd = args.pop(0)
is_admin = self.event.source.userhost == "khuxkm@sudoers.tilde.team"
if cmd in self.commands:
if (not self.commands[cmd]["is_admin"]) or (self.commands[cmd]["is_admin"] and is_admin):
try:
self.commands[cmd]["action"](channel,nick,*args)
except:
pass
def on_botlist(self,channel,nick):
self.say(channel,nick+": Maintainer: khuxkm@cosmic.voyage | Utility bot")
def on_admin(self,channel,nick,subcmd,*args):
if subcmd=="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)
sys.exit()
elif subcmd=="check":
self.tasks.states[0] = self.check_rss(self.tasks.states[0],self.tasks.base_state)
if __name__=="__main__":
channels = "#cosmic".split()