fixed call to aroundFor() that was causing bug, restructured

This commit is contained in:
Shauna 2014-01-11 00:50:13 -05:00
parent 15a8da0a35
commit 865113a4cb
2 changed files with 67 additions and 65 deletions

126
bot.py
View File

@ -10,31 +10,7 @@ from threading import Thread
server = "irc.freenode.net"
channel = "#openhatch"
botnick = "WelcomeBot"
waitTime = 60 # Amount of time after joining before bot replies to someone
# Connects to server and joins channel
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n") # user authentication
ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
def joinchan(chan):
ircsock.send("JOIN "+ chan +"\n")
joinchan(channel)
# Creates separate thread for reading messages from the server
def getIRC():
while True:
ircmsg = ircsock.recv(2048) # receive data from the server
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
q.put(ircmsg) # Put in queue for main loop to read
print(ircmsg)
q = Queue.LifoQueue()
t = Thread(target=getIRC)
t.daemon = True
t.start()
waitTime = 5 # Amount of time after joining before bot replies to someone
# Classes
class newcomer(object): # Newcomer class created when someone joins the room
@ -44,13 +20,23 @@ class newcomer(object): # Newcomer class created when someone joins the room
self.born = time.time()
self.status = 0
def updateStatus(self,status=0):
self.status = status
def updateStatus(self):
self.status = 1
def aroundFor(self):
return time.time() - self.born
return int(time.time() - self.born)
# Functions!
def joinchan(chan): # Joins channels
ircsock.send("JOIN "+ chan +"\n")
def getIRC(): # Creates separate thread for reading messages from the server
while True:
ircmsg = ircsock.recv(2048) # receive data from the server
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
q.put(ircmsg) # Put in queue for main loop to read
print(ircmsg)
# Functions
def ping(): # Responds to server Pings.
ircsock.send("PONG :pingis\n")
@ -61,7 +47,7 @@ def help(actor): # This function explains what the bot is when queried.
ircsock.send("PRIVMSG " + channel +" :I'm a bot! I'm from here: https://github.com/shaunagm/oh-irc-bot. You can change my behavior by submitting a pull request or by talking to shauna. \n")
def welcome(newcomer): # This welcomes a specific person.
ircsock.send("PRIVMSG "+ channel +" :Welcome "+ newcomer + "! The channel's pretty quiet right now, so I thought I'd say hello, and ping my maintainers(shauna, paulproteus) that you're here. If no one responds for a while, try emailing us at hello@openhatch.org or just coming back later.\n")
ircsock.send("PRIVMSG "+ channel +" :Welcome "+ newcomer + "! The channel's pretty quiet right now, so I thought I'd say hello, and ping my maintainers (shauna, paulproteus) that you're here. If no one responds for a while, try emailing us at hello@openhatch.org or just coming back later. FYI, you're now on my list of known nicknames, so I won't bother you again.\n")
def makeNickArray(): # On startup, makes array of nicks from Nicks.txt. New info will be written to both array and txt file.
nickArray = []
@ -69,53 +55,67 @@ def makeNickArray(): # On startup, makes array of nicks from Nicks.txt. New in
nicksData = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in nicksData:
nickArray.append(row)
# Do I need to explicitly close this?
return nickArray
# Do I need to explicitly close this file?
def addPerson(person): # After a newcomer has been greeted (either by the bot, or someone else) their information will be set recorded.
nicksData.append(person)
with open('nicks.csv', 'wb') as csvfile:
def addPerson(person): # Adds newcomer to list of known nicks
nickArray.append(person)
with open('nicks.csv', 'a') as csvfile:
nickwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
nickwriter.writerow([person])
#### Main function
# Startup
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This is http://openhatch.org/'s greeter bot.\n") # user authentication
ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
joinchan(channel)
# Starts a separate thread to get messages from server
q = Queue.LifoQueue()
t = Thread(target=getIRC) # calls getIRC() (defined above) in a separate thread
t.daemon = True
t.start()
nickArray = makeNickArray()
newList = [] # This is the array of newcomer objects that people who join the room are added to.
helloArray = ['Hello','hello','Hi','hi','Hey','hey','Yo','yo ','Sup','sup']
helpArray = ['Help','help','Info','info','faq','FAQ','explain yourself','EXPLAIN YOURSELF']
while 1:
while 1: # Loop forever
for i in newList:
i.updateStatus()
if i.status == 0 and i.aroundFor > waitTime:
for i in newList:
if i.status == 0 and i.aroundFor() > waitTime:
welcome(i.nick)
i.updateStatus(1)
addPerson(i)
i.updateStatus()
addPerson(i.nick)
if q.empty() == 0:
ircmsg = q.get()
actor = ircmsg.split(":")[1].split("!")[0]
if q.empty() == 0:
ircmsg = q.get()
actor = ircmsg.split(":")[1].split("!")[0]
# Welcome functions
if ircmsg.find("PRIVMSG "+ channel) != -1: # If someone has spoken into the channel
for i in newList:
if actor != i.nick: # Don't turn off response if the person speaking is the person who joined.
i.updateStatus(1) # Sets status to 1
addPerson(i)
## Else: Do we want to do something if the person who joined the chat says something?
# Welcome functions
if ircmsg.find("PRIVMSG "+ channel) != -1: # If someone has spoken into the channel
for i in newList:
if actor != i.nick: # Don't turn off response if the person speaking is the person who joined.
i.updateStatus() # Sets status to 1
addPerson(i.nick)
## Else: Do we want to do something extra if the person who joined the chat says something with no response?
if ircmsg.find("JOIN "+ channel) != -1: # If someone joins #channel
if actor != botnick: # Remove the case where the bot gets a message that the bot has joined.
if actor not in nickArray:
newList.append(newcomer(actor)) # Create a newcomer object and append to list.
if ircmsg.find("JOIN "+ channel) != -1: # If someone joins #channel
if actor != botnick: # Remove the case where the bot gets a message that the bot has joined.
if actor not in nickArray:
newList.append(newcomer(actor))
# Unwelcome functions
if ircmsg.find(botnick) != -1 and ircmsg.find("PRIVMSG #") != -1: # If someone talks to (or refers to) the bot
if any(x in ircmsg for x in helloArray):
hello(actor,random.choice(helloArray))
if any(y in ircmsg for y in helpArray):
help(actor)
# Unwelcome functions
if ircmsg.find(botnick) != -1 and ircmsg.find("PRIVMSG #") != -1: # If someone talks to (or refers to) the bot
if any(x in ircmsg for x in helloArray):
hello(actor,random.choice(helloArray))
if any(y in ircmsg for y in helpArray):
help(actor)
if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
ping()
if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
ping()

View File

@ -1,2 +1,4 @@
paulproteus, shauna, britta, skay
paulproteus
britta
aldeka
shauna

1 paulproteus shauna britta skay
2 britta
3 aldeka
4 shauna