circles/bot.py

54 lines
1.6 KiB
Python
Raw Normal View History

2020-01-31 16:32:17 +00:00
#!/usr/bin/env python3
import teambot, sys, os
2020-01-31 22:17:11 +00:00
class Circles(teambot.Handler):
def __init__(self, bot):
2020-02-01 00:57:06 +00:00
self._bot = bot
2020-01-31 23:00:47 +00:00
print('initilized')
2020-01-31 22:17:11 +00:00
self.modules = []
2020-02-01 13:16:35 +00:00
self.lm = []
2020-01-31 22:17:11 +00:00
self.cmd = {}
2020-01-31 23:00:47 +00:00
self.raw = {}
2020-01-31 22:34:37 +00:00
self.help = {}
2020-01-31 22:17:11 +00:00
self.prefix = "."
print('loading modules')
self.loadMods()
print('Ready!')
2020-01-31 22:17:11 +00:00
def loadMods(self):
for i in [s for s in os.listdir('modules') if ".py" in s]:
i = i[:-3]
print('loading',i)
m = __import__("modules."+i)
m = eval('m.'+i)
2020-01-31 22:34:37 +00:00
m.init(self)
2020-02-01 13:16:35 +00:00
self.lm.append(i)
2020-01-31 22:17:11 +00:00
self.modules.append(m)
def registerCommand(self, txt, func):
self.cmd[txt] = func
def registerRaw(self, txt, func):
2020-01-31 23:00:47 +00:00
self.raw[txt] = func
2020-01-31 22:34:37 +00:00
def registerHelp(self, cmd, ht):
self.help[cmd] = ht
2020-02-01 00:39:41 +00:00
def on_pubmsg(self,c,n,m):
2020-01-31 22:17:11 +00:00
if m.startswith(self.prefix):
args = m[len(self.prefix):].strip().split()
2020-02-01 13:05:44 +00:00
cmd = args.pop(0)
2020-01-31 22:17:11 +00:00
if cmd in self.cmd:
2020-01-31 22:34:37 +00:00
try:
self.cmd[cmd](self,c,n,m)
except TypeError:
self.say(c,n+": Usage: "+self.help[cmd])
2020-02-01 00:47:42 +00:00
try:
for i in self.raw:
self.raw[i](self,c,n,m)
except TypeError:
self.say(c,n+": Usage: "+self.help[cmd])
2020-02-01 13:05:44 +00:00
def say(self,target,message):
self._bot.conn.privmsg(target,message)
2020-01-31 22:34:37 +00:00
2020-01-31 23:00:47 +00:00
if __name__=="__main__":
chans = ['#chaos', '#lickthecheese', '#cminecraft', '#bots', '#mbgeneral']
2020-01-31 23:00:47 +00:00
bot = teambot.TeamBot(chans, "circles", "localhost", chandler=Circles)
bot.start()
2020-01-31 16:32:17 +00:00