-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
executable file
·169 lines (130 loc) · 5.47 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Run this script with one argument, the channel :
$ ./bot.py ##atal
"""
from bicloo import bicloo
from fortune_teller import fortune_teller
from ics import ics
import re
import sys
from tan import tan
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.words.protocols import irc
def splitUserString(user_string):
"""
Splits a full user string to return only the user name.
For example, [email protected] → m0g
"""
return user_string.split('!', 1)[0]
class Atalatchatche(irc.IRCClient):
"""ATAL's irc bot"""
pattern = re.compile(u'^# *')
nickname = 'atalatchatche'
def __init__(self, channel):
self.channel = channel
# callbacks for events
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.channel)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
self.msg(channel,
'♥ Salut {channel} ♥'.format(channel=self.factory.channel))
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = splitUserString(user)
# Check to see if they're sending me a private message
if channel == self.nickname:
pass
# Otherwise check to see if it's a command
elif channel == self.channel and msg.startswith('#'):
command = self.pattern.sub('', msg, 1).rstrip()
print 'received command ' + command
split = map(lambda x: x.strip(), command.split(' ', 2))
if not split:
return
else:
function = split[0]
if function in ['help', 'h4lp']:
self.msg(self.channel,
("Je comprends les commandes:\n"
"- bow : je fais la carpette\n"
"- spit : je défends mon maître\n"
"- bicloo station : je donne des infos "
"sur la station donnée en arg "
"- tan station : je donne des infos "
"sur la station donnée en arg "
"(lowercase et sans accent). Liste des "
"stations dispo ici : "
"http://www.bicloo.nantesmetropole.fr"
"/Les-stations/Plan-des-stations-en-PDF"
"/Consultez-le-plan\n"
"- cours : je donne des infos "
"sur le prochain cours qui aura lieu\n"
"- biorulz : comme cours mais en "
"différent\n"
"- shine : je vous éblouis "
"en racontant des trucs super "
"intéressants"))
elif function == 'bow':
self.describe(self.channel, "s'incline")
elif function == 'spit':
if len(split) < 2:
self.msg(self.channel,
"j'ai besoin d'une cible ;(")
return
self.describe(self.channel, "crache sur " + split[1])
elif function == 'bicloo':
if len(split) < 2:
self.msg(self.channel,
"j'ai besoin d'un nom de station ;(")
return
station = split[1]
self.msg(self.channel, bicloo(station))
elif function == 'tan':
if len(split) < 2:
self.msg(self.channel,
"j'ai besoin d'un nom de station ;(")
return
station = split[1]
self.msg(self.channel, tan(station))
elif function == 'cours':
self.msg(self.channel, ics('g78125'))
elif function == 'biorulz':
self.msg(self.channel, ics('g18535'))
elif function == 'shine':
self.msg(self.channel, fortune_teller())
# irc callbacks
def alterCollidedNick(self, nickname):
"""
Generate an altered version of a nickname that caused a collision in an
effort to create an unused related name for subsequent registration.
"""
return nickname + '`'
class BotFactory(protocol.ClientFactory):
"""
A factory for bots.
A new protocol instance will be created each time we connect to the server.
"""
def __init__(self, channel):
self.channel = channel
def buildProtocol(self, addr):
p = Atalatchatche(self.channel)
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "connection failed: ", reason
reactor.stop()
if __name__ == '__main__':
# create factory protocol and application
f = BotFactory(sys.argv[1])
# connect factory to this host and port
reactor.connectTCP("irc.freenode.net", 6667, f)
# run bot
reactor.run()