-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (48 loc) · 1.89 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
from collections import namedtuple
from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet import reactor
from plugin import pluginsystem
pluginsystem.setup()
Info = namedtuple("Info", ["bot", "user", "channel"])
class Bot(irc.IRCClient):
nickname = "Somebot"
prefix = "#"
channels = ["bots", "python-forum"]
def signedOn(self):
print "Signed on as %s." % (self.nickname,)
for channel in self.channels:
self.join(channel)
def joined(self, channel):
print "Joined %s." % (channel,)
self.msg(channel, "Hello everybody, my command prefix is %s" % self.prefix)
def privmsg(self, user, channel, msg):
print msg
user = user[:user.index("!")]
# This seems to behave inconsistently between different version of twisted.
# Therefore I present you this stupid hack.
if channel == self.nickname:
channel = None
is_directed = msg.startswith(self.nickname)
if is_directed:
msg = msg.split(" ", 1)[1]
is_prefixed = msg.startswith(self.prefix)
if is_prefixed:
msg = msg[len(self.prefix):]
if msg and (is_directed or is_prefixed):
parameters = msg.split()
command = parameters.pop(0)
target = channel or user
info = Info(self, user, channel)
self.msg(target, pluginsystem.command(info, command, *parameters))
class BotFactory(protocol.ClientFactory):
protocol = Bot
def clientConnectionLost(self, connector, reason):
print "Lost connection (%s), reconnecting." % (reason,)
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)
if __name__ == "__main__":
bot = BotFactory()
reactor.connectTCP('irc.freenode.net', 6667, bot)
reactor.run()