/
bot.py
executable file
·43 lines (34 loc) · 1.23 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
import irc.bot
import os
import subprocess
def config(key):
return open('conf/' + key).readline().strip('\n')
class Petal(irc.bot.SingleServerIRCBot):
def __init__(self, channel, name, server, port):
irc.bot.SingleServerIRCBot.__init__(self, [(server, int(port))], name, name)
self.channel = channel
def on_welcome(self, c, e):
c.join(self.channel)
def on_privmsg(self, c, e):
self.message(e, e.arguments[0])
def on_pubmsg(self, c, e):
self.message(e, e.arguments[0])
def message(self, e, msg):
user = e.source.split('!')[0]
ignore = config('ignore').split(' ')
if (user in ignore):
return
for mod in os.listdir('mods'):
try:
result = subprocess.check_output(['mods/' + mod, msg])
self.connection.privmsg(config('channel'), result.decode().strip())
except subprocess.CalledProcessError:
print('subprocess failed')
except PermissionError:
print('couldnt execute ' + mod)
def main():
bot = Petal(config('channel'), config('name'), config('server'), config('port'))
bot.start()
if __name__ == "__main__":
main()