127 lines
3.3 KiB
Python
Executable File
127 lines
3.3 KiB
Python
Executable File
#!/usr/bin/python
|
|
# http://wiki.shellium.org/w/Writing_an_IRC_bot_in_Python
|
|
|
|
# Import some necessary libraries.
|
|
import socket
|
|
import os
|
|
import sys
|
|
from optparse import OptionParser
|
|
import fileinput
|
|
import random
|
|
import re
|
|
|
|
import formatter
|
|
import get_users
|
|
import mentions
|
|
import pretty_date
|
|
import inflect
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-s", "--server", dest="server", default='127.0.0.1',
|
|
help="the server to connect to", metavar="SERVER")
|
|
parser.add_option("-c", "--channel", dest="channel", default='#bot_test',
|
|
help="the channel to join", metavar="CHANNEL")
|
|
parser.add_option("-n", "--nick", dest="nick", default='tildebot',
|
|
help="the nick to use", metavar="NICK")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
p = inflect.engine()
|
|
|
|
def ping():
|
|
ircsock.send("PONG :pingis\n")
|
|
|
|
def sendmsg(chan , msg):
|
|
ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
|
|
|
|
def joinchan(chan):
|
|
ircsock.send("JOIN "+ chan +"\n")
|
|
|
|
def hello():
|
|
ircsock.send("PRIVMSG "+ channel +" :Hello!\n")
|
|
|
|
def score_banter(channel, user, messageText):
|
|
score = 5
|
|
with open("banterscores.txt", "r") as banterfile:
|
|
bantz = banterfile.readlines()
|
|
words = messageText.strip("\n").split(" ")
|
|
for word in words:
|
|
for bant in bantz:
|
|
bword = bant.strip("\n").split("|")
|
|
if re.sub('[^a-z0-9]+','',word.lower()) == bword[0]:
|
|
score += int(bword[1])
|
|
|
|
score += messageText.count('!') * 2
|
|
score += messageText.count('#') * 3
|
|
|
|
|
|
score = score * 100 / len(messageText)
|
|
msg = ""
|
|
if score > 100:
|
|
msg = "Top drawer, mate! That was some #banter! You earned a " + str(score)
|
|
elif score > 50:
|
|
msg = "Bangin' #banter! You got a " + str(score)
|
|
elif score > 10:
|
|
msg = "Acceptible #banter. You get a " + str(score)
|
|
else:
|
|
msg = "That was hardly #banter. You get a " + str(score) + ". Maybe try again?"
|
|
|
|
ircsock.send("PRIVMSG " + channel + " :" + msg + "\n")
|
|
|
|
def rollcall(channel):
|
|
ircsock.send("PRIVMSG "+ channel +" :U wot m8? I score all the top drawer #banter and #bantz on this channel!\n")
|
|
|
|
def connect(server, channel, botnick):
|
|
ircsock.connect((server, 6667))
|
|
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :krowbar\n") # user authentication
|
|
ircsock.send("NICK "+ botnick +"\n")
|
|
|
|
joinchan(channel)
|
|
|
|
def get_user_from_message(msg):
|
|
try:
|
|
i1 = msg.index(':') + 1
|
|
i2 = msg.index('!')
|
|
return msg[i1:i2]
|
|
except ValueError:
|
|
return ""
|
|
|
|
def listen():
|
|
while 1:
|
|
|
|
ircmsg = ircsock.recv(2048)
|
|
ircmsg = ircmsg.strip('\n\r')
|
|
|
|
if ircmsg.find("PING :") != -1:
|
|
ping()
|
|
|
|
formatted = formatter.format_message(ircmsg)
|
|
|
|
if "" == formatted:
|
|
continue
|
|
|
|
# print formatted
|
|
|
|
split = formatted.split("\t")
|
|
time = split[0]
|
|
user = split[1]
|
|
command = split[2]
|
|
channel = split[3]
|
|
messageText = split[4]
|
|
|
|
if ircmsg.find("#banter") != -1 or ircmsg.find("#bantz") != -1:
|
|
score_banter(channel, user, messageText)
|
|
|
|
if ircmsg.find(":!rollcall") != -1:
|
|
rollcall(channel)
|
|
|
|
if ircmsg.find("PING :") != -1:
|
|
ping()
|
|
|
|
sys.stdout.flush()
|
|
|
|
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
connect(options.server, options.channel, options.nick)
|
|
listen()
|