forked from LukeusMaximus/Python-IRC-Bot-Framework
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamplebot.py
85 lines (71 loc) · 3.24 KB
/
examplebot.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
from ircbotframe import ircBot
import sys
# Bot specific function definitions
def authFailure(recipient, name):
bot.say(recipient, "You could not be identified")
def quitSuccess(quitMessage):
bot.disconnect(quitMessage)
bot.stop()
def joinSuccess(channel):
bot.joinchan(channel)
def saySuccess(channel, message):
bot.say(channel, message)
def kickSuccess(nick, channel, reason):
bot.kick(nick, channel, reason)
def identPass():
pass
def identFail():
pass
def privmsg(sender, headers, message):
if message.startswith("!say "):
firstSpace = message[5:].find(" ") + 5
if sender == owner:
bot.identify(sender, saySuccess, (message[5:firstSpace], message[firstSpace+1:]), authFailure, (headers[0], sender))
elif message.startswith("!quit"):
if sender == owner:
if len(message) > 6:
bot.identify(sender, quitSuccess, (message[6:],), authFailure, (headers[0], sender))
else:
bot.identify(sender, quitSuccess, ("",), authFailure, (headers[0], sender))
elif message.startswith("!join "):
if sender == owner:
bot.identify(sender, joinSuccess, (message[6:],), authFailure, (headers[0], sender))
elif message.startswith("!kick "):
firstSpace = message[6:].find(" ") + 6
secondSpace = message[firstSpace+1:].find(" ") + (firstSpace + 1)
if sender == owner:
bot.identify(sender, kickSuccess, (message[6:firstSpace], message[firstSpace+1:secondSpace], message[secondSpace+1:]), authFailure, (headers[0], sender))
else:
print("PRIVMSG: \"" + message + "\"")
def actionmsg(sender, headers, message):
print("An ACTION message was sent by " + sender + " with the headers " + str(headers) + ". It says: \"" + sender + " " + message + "\"")
def endMOTD(sender, headers, message):
bot.joinchan(chanName)
bot.say(chanName, "I am an example bot.")
bot.say(chanName, "I have 4 functions, they are Join, Kick, Quit and Say.")
bot.say(chanName, "Join (joins a channel); Usage: \"!join #<channel>\"")
bot.say(chanName, "Kick (kicks a user); Usage: \"!kick <nick> #<channel> <reason>\"")
bot.say(chanName, "Quit (disconnects from the IRC server); Usage: \"!quit [<quit message>]\"")
bot.say(chanName, "Say (makes the bot say something); Usage: \"!say <channel/user> <message>\"")
bot.say(chanName, "The underlying framework is in no way limited to the above functions.")
bot.say(chanName, "This is merely an example of the framework's usage")
# Main program begins here
if __name__ == "__main__":
if len(sys.argv) == 5:
server = sys.argv[1]
port = int(sys.argv[2])
owner = sys.argv[3]
chanName = "#" + sys.argv[4]
bot = ircBot(server, port, "ExampleBot", "An example bot written with the new IRC bot framework")
bot.bind("PRIVMSG", privmsg)
bot.bind("ACTION", actionmsg)
bot.bind("376", endMOTD)
bot.debugging(True)
bot.start()
inputStr = ""
while inputStr != "stop":
inputStr = input()
bot.stop()
bot.join()
else:
print("Usage: python examplebot.py <server> <port> <your IRC nick> <irc channel (no '#' character please)>")