-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestbot.py
executable file
·90 lines (73 loc) · 2.89 KB
/
testbot.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
#! /usr/bin/env python
#
# Example program using ircbot.py.
#
# Joel Rosdahl <[email protected]>
"""A simple example bot.
This is an example bot that uses the SingleServerIRCBot class from
ircbot.py. The bot enters a channel and listens for commands in
private messages and channel traffic. Commands in channel messages
are given by prefixing the text by the bot name followed by a colon.
It also responds to DCC CHAT invitations and echos data sent in such
sessions.
The known commands are:
stats -- Prints some channel information.
disconnect -- Disconnect the bot. The bot will try to reconnect
after 60 seconds.
die -- Let the bot cease to exist.
dcc -- Let the bot invite you to a DCC CHAT connection.
"""
from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr
class TweetBot(SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
c.join(self.channel)
def on_privmsg(self, c, e):
if e.arguments()[0].startswith('!'):
cmd = e.arguments()[0].split()[0].replace('!','',1)
self.do_command(e, cmd)
def on_pubmsg(self, c, e):
if e.arguments()[0].startswith('!'):
cmd = e.arguments()[0].split()[0].replace('!','',1)
self.do_command(e, cmd)
def on_dccmsg(self, c, e):
c.privmsg("You said: " + e.arguments()[0])
def on_dccchat(self, c, e):
if len(e.arguments()) != 2:
return
args = e.arguments()[1].split()
if len(args) == 4:
try:
address = ip_numstr_to_quad(args[2])
port = int(args[3])
except ValueError:
return
self.dcc_connect(address, port)
def do_command(self, e, cmd):
nick = nm_to_n(e.source())
c = self.connection
if cmd == self._nickname:
cmd = e.arguments()[0].split()[1].strip()
if cmd == "disconnect":
self.disconnect()
elif cmd == "die":
self.die()
elif cmd == "help":
c.privmsg(self.channel,self.helpmsg)
elif cmd == 'tweet':
if self.channels[self.channel].is_voiced(nick):
c.privmsg(self.channel,'i twat this on @cjdnsirc: '+tweet(e.arguments()[0].replace('!tweet','',1)))
else:
c.privmsg(self.channel,'i can\'t hear you without voice.')
'''elif cmd == "dcc":
dcc = self.dcc_listen()
c.ctcp("DCC", nick, "CHAT chat %s %d" % (
ip_quad_to_numstr(dcc.localaddress),
dcc.localport))
else:
c.privmsg(nick, "Not understood: " + cmd)'''