min/bot.py

103 lines
2.7 KiB
Python
Raw Normal View History

2020-04-05 15:39:50 +00:00
#!/usr/bin/env python3
2020-07-11 22:00:18 +00:00
import pydle, asyncio, dataset, sys, os, time
2020-04-05 15:39:50 +00:00
class Kim(pydle.Client):
2020-04-05 15:39:50 +00:00
async def on_connect(self):
print('Connected!')
2020-04-05 22:25:19 +00:00
self.modules = {}
self.cmd = {}
self.rawm = {}
2020-04-05 22:25:19 +00:00
self.help = {}
2020-07-11 22:00:18 +00:00
self.db = dataset.connect('sqlite:///database.db')
self.t=0
2020-04-18 00:28:28 +00:00
2020-04-05 22:25:19 +00:00
print('loading modules...')
await self.loadMods()
2020-04-06 00:21:10 +00:00
print('joining channels')
for i in self.chansjoin:
await self.join(i)
2020-04-05 22:25:19 +00:00
print('Done!')
2020-07-11 22:00:18 +00:00
# tilde +B bot
await self.set_mode(self.nickname, '+B')
2020-04-05 22:25:19 +00:00
async def loadMods(self):
2020-07-11 22:00:18 +00:00
for i in [s for s in os.listdir('modules') if ".py" in s and '.swp' not in s]:
2020-04-05 22:25:19 +00:00
i = i[:-3]
print('loading', i)
m = __import__("modules."+i)
m = eval('m.'+i)
await m.init(self)
self.modules[i] = m
2020-04-06 00:21:10 +00:00
async def on_invite(self, channel, by):
2020-07-15 23:25:05 +00:00
await self.modules['invite'].invite(self, channel, by)
2020-07-15 23:24:09 +00:00
# print('{} invited me to {}!'.format(by, channel))
# self.t = time.time()+1
# await self.join(channel)
2020-04-05 22:25:19 +00:00
2020-04-05 15:39:50 +00:00
async def on_message(self, chan, source, msg):
2020-07-15 23:24:09 +00:00
if chan == self.nickname:
chan = source
2020-04-05 15:39:50 +00:00
if source != self.nickname:
2020-04-23 02:45:02 +00:00
2020-07-11 22:00:18 +00:00
if time.time() > self.t:
2020-04-23 02:45:02 +00:00
2020-07-11 22:00:18 +00:00
if msg == '!botlist':
2020-07-12 20:54:25 +00:00
await self.message(chan, 'helo im kim, a learning chatbot https://tildegit.org/xfnw/kim/')
2020-07-11 22:00:18 +00:00
await self.parseCommand(chan, source, msg)
for i in self.rawm:
await self.rawm[i](self, chan, source, msg)
2020-07-11 22:00:18 +00:00
async def parseCommand(self, chan, source, msg):
if msg[:len(self.prefix)] == self.prefix:
2020-04-23 02:45:02 +00:00
2020-07-11 22:00:18 +00:00
msg = msg[len(self.prefix):]
cmd = msg.split(' ')[0]
msg = msg[len(cmd)+1:]
if len(cmd) < 1:
return
if cmd in self.cmd:
await self.cmd[cmd](self, chan, source, msg)
return
2020-04-19 23:25:07 +00:00
2020-07-11 22:00:18 +00:00
# fuzzy search for commands
results = [i for i in self.cmd if i.startswith(cmd)]
if len(results) == 1:
await self.cmd[results[0]](self, chan, source, msg)
2020-04-23 02:45:02 +00:00
2020-04-23 02:14:17 +00:00
2020-04-05 22:25:19 +00:00
async def is_admin(self, nickname):
# Check the WHOIS info to see if the source has identified with NickServ.
# This is a blocking operation, so use yield.
2020-07-11 22:00:18 +00:00
info = await self.whois(nickname)
if 'account' in info:
account = info['account']
else:
# they are not nickserv registered
return False
2020-04-05 22:25:19 +00:00
2020-07-11 22:00:18 +00:00
return account in self.admins
2020-04-05 22:25:19 +00:00
2020-08-02 02:33:43 +00:00
#async def on_private_message(self, trash, source, msg):
# if source != self.nickname:
# for i in self.rawm:
# await self.rawm[i](self, source, source, msg)
2020-04-05 15:39:50 +00:00
if __name__ == "__main__":
client = Kim('kim', realname='owens bot')
2020-07-11 22:00:18 +00:00
client.admins = ['lickthecheese', 'ben', 'coffeeowl', 'gbmor', 'tomasino', 'ubergeek', 'deepend', 'calamitous', 'khuxkm']
2020-07-12 12:21:53 +00:00
client.prefix = 'kim: '
2020-04-05 23:52:31 +00:00
client.run('team.tilde.chat', tls=True, tls_verify=False)
2020-07-11 22:00:18 +00:00