-
Notifications
You must be signed in to change notification settings - Fork 1
/
sandiegopythonbot.py
100 lines (74 loc) · 3.31 KB
/
sandiegopythonbot.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
"""This is San Diego Python's chat bot.
It is based on the example "TestBot" from the "irc" Python module
The bot sends welcome messages when people join the channel
The known commands are:
welcome -- Send a welcome message
stats -- Prints some channel information.
dcc -- Let the bot invite you to a DCC CHAT connection.
"""
import logging
import irc.bot
import irc.strings
logger = logging.getLogger('SDPythonBot')
class SanDiegoPythonBot(irc.bot.SingleServerIRCBot):
MEETUP_PAGE = 'http://www.meetup.com/pythonsd/'
def __init__(self, server='irc.freenode.net', port=6667):
self.nick = 'SDPythonBot'
self.channel = '#sandiegopython'
self.known_nicks = set([irc.strings.lower(self.nick)])
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], self.nick, self.nick)
def on_nicknameinuse(self, connection, event):
connection.nick(connection.get_nickname() + "_")
def on_welcome(self, connection, event):
connection.join(self.channel)
def on_privmsg(self, connection, event):
self.do_command(event, event.arguments[0])
def on_pubmsg(self, connection, event):
a = event.arguments[0].split(":", 1)
if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(self.connection.get_nickname()):
self.do_command(event, a[1].strip())
return
def on_join(self, connection, event):
nick = irc.strings.lower(event.source.nick)
if nick not in self.known_nicks:
self.do_welcome(connection, nick)
self.known_nicks.add(nick)
def do_welcome(self, connection, nick):
logger.info(u'Sending welcome message to {}'.format(nick))
connection.privmsg(self.channel, u'{}: Check out our Meetup page for upcoming events: {}. Please be respectful of our members. See our code of conduct: http://pythonsd.org/pages/code-of-conduct.html'.format(nick, self.MEETUP_PAGE))
def do_command(self, event, cmd):
nick = event.source.nick
c = self.connection
logger.debug(u'Received command {} from {}'.format(cmd, nick))
if cmd == "help":
c.privmsg(self.channel, u"I'm a bot. My source code is here: https://github.com/pythonsd/ircbot You're welcome to send a pull request to change my behavior.")
elif cmd == "welcome":
self.do_welcome(c, nick)
elif cmd.startswith('kick'):
c.privmsg(self.channel, u"Don't tempt me")
elif cmd == "import this":
c.privmsg(self.channel, u"Don't get me started on The Zen of Python. I can talk for days.")
else:
c.notice(nick, "Not understood: " + cmd)
def main():
import argparse
parser = argparse.ArgumentParser(description='San Diego Python IRC Bot')
parser.add_argument(
'--server',
dest='server',
default='irc.freenode.net',
help='Hostname for the IRC server [irc.freenode.net]',
)
parser.add_argument(
'--port',
dest='port',
type=int,
default=6667,
help='Port for the IRC server [6667]',
)
args = parser.parse_args()
logging.basicConfig(format='[%(asctime)s]: %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
bot = SanDiegoPythonBot(server=args.server, port=args.port)
bot.start()
if __name__ == "__main__":
main()