-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydrabot.py
52 lines (43 loc) · 1.46 KB
/
hydrabot.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
44
45
46
47
48
49
50
51
52
import hydracommands
import irc.bot
import irc.strings
class BotParser(hydracommands.HydraCommands):
buffer = ''
def _print(self, x):
self.buffer += x + '\n'
def readAll(self):
data = self.buffer.rstrip()
self.buffer = ''
return data
class HydraBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
self.parser = BotParser()
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
c.join(self.channel)
def do_command(self, c, e, destination):
line = e.arguments[0]
if not line.startswith('!'):
return
self.parser.execute(line[1:])
output = self.parser.readAll()
if output:
try:
for line in output.split('\n'):
c.privmsg(destination, line)
except:
c.privmsg(destination, "Error running command.")
def on_privmsg(self, c, e):
self.do_command(c, e, e.source.nick)
def on_pubmsg(self, c, e):
self.do_command(c, e, self.channel)
if __name__ == '__main__':
server = 'irc.nac.net'
port = 6667
channel = '#conduit'
nick = 'HydraBot'
bot = HydraBot(channel, nick, server, port)
bot.start()