tetrisbot/bot.py

56 lines
1.9 KiB
Python

import teambot,threading,subprocess
def log(s):
with open("/home/khuxkm/tetrisbot.log","a") as f:
f.write(str(s)+"\n")
USAGE = dict(help="!help [command]",admin="!admin <task> [arg]",source="!source")
HELP = dict(help="Helps define commands.",admin="Does admin tasks.",source="Links the source of the bot.")
PREFIX = "!"
BOTOPS = "khuxkm khuxkm|lounge".split()
class TetrisBot(teambot.Handler):
def on_pubmsg(self,channel,nick,text):
if not text.startswith(PREFIX):
return
self.channel = channel
parts = text.split(PREFIX,1)[1].split()
command = parts.pop(0)
parts.insert(0,nick)
if hasattr(self,"do_"+command.lower()):
try:
getattr(self,"do_"+command.lower())(*parts)
except TypeError as e: # missing an arg or having too many? print usage
log(e)
self.say(self.channel,"{}: Usage: {}".format(nick,USAGE[command.lower()]))
def do_help(self,nick,command=None):
if command is None:
self.say(self.channel,"{}: Commands: {}".format(nick,", ".join(USAGE[k] for k in USAGE.keys())))
else:
if command not in HELP:
self.say(self.channel,"{}: no such command!".format(nick))
else:
self.say(self.channel,"{}: {} - {} ({})".format(nick,command,HELP[command],USAGE[command]))
def do_admin(self,nick,action,prefix="!"):
if nick not in BOTOPS:
self.say(self.channel,"{}: you are not authorized to use this command.".format(nick))
if action=="quit":
self._bot.die("Goodbye!")
elif action=="reload":
threading.Timer(3,lambda: subprocess.run("systemctl --user restart tetrisbot",shell=True)).start()
self.say(self.channel,"Reloading...")
elif action=="prefix":
global PREFIX
PREFIX = prefix
self.say(self.channel,"{}: Prefix set to \"{}\".".format(nick,prefix))
def do_source(self,nick):
self.say(self.channel,"{}: https://git.tilde.team/khuxkm/tetrisbot".format(nick))
if __name__=="__main__":
channels = "#tetris".split()
bot = teambot.TeamBot(channels,"tetrisbot","localhost",chandler=TetrisBot)
bot.start()