Merge pull request #19 from noobur/develop2

Fix for Issue #5.
This commit is contained in:
shaunagm 2014-05-06 18:26:56 -04:00
commit e15634bae9

37
bot.py
View File

@ -8,8 +8,9 @@ import time
import csv import csv
import Queue import Queue
import random import random
import re
from threading import Thread from threading import Thread
from re import search
# Some basic variables used to configure the bot. # Some basic variables used to configure the bot.
server = "irc.freenode.net" server = "irc.freenode.net"
@ -18,6 +19,8 @@ botnick = 'WelcomeBot'
channel_greeters = ['shauna', 'paulproteus', 'marktraceur'] channel_greeters = ['shauna', 'paulproteus', 'marktraceur']
wait_time = 60 # amount of time after joining before bot replies to someone wait_time = 60 # amount of time after joining before bot replies to someone
change_wait = botnick + " --wait-time " change_wait = botnick + " --wait-time "
hello_list = [r'hello', r'hi', r'hey', r'yo', r'sup']
help_list = [r'help', r'info', r'faq', r'explain yourself']
#################### Classes #################### #################### Classes ####################
@ -106,14 +109,14 @@ def add_known_nick(new_known_nick):
nickwriter.writerow([new_known_nick]) nickwriter.writerow([new_known_nick])
# "I NEED NOTES!!!!", said the function. # Builds a regex that matches one of the options + (space) botnick.
def get_welcome_regex(string_array): def get_regex(options):
#make regex case-insenstive pattern = "("
pattern = r'(?i)' for s in options:
for s in string_array: pattern += s
pattern += r'(?:[ :]'+s+r'(?:[ \.!\?,\)]|$))|' pattern += "|"
#delete trailing '|'
pattern = pattern[:-1] pattern = pattern[:-1]
pattern += ").({})".format(botnick)
return pattern return pattern
@ -123,7 +126,7 @@ def get_welcome_regex(string_array):
def wait_time_change(): def wait_time_change():
for admin in channel_greeters: for admin in channel_greeters:
if actor == admin: if actor == admin:
finder = search(r'\d\d*', search(r'--wait-time \d\d*', ircmsg) finder = re.search(r'\d\d*', re.search(r'--wait-time \d\d*', ircmsg)
.group()) .group())
ircsock.send("PRIVMSG {0} :{1} the wait time is changing to {2} " ircsock.send("PRIVMSG {0} :{1} the wait time is changing to {2} "
"seconds.\n".format(channel, actor, finder.group())) "seconds.\n".format(channel, actor, finder.group()))
@ -164,10 +167,10 @@ t.start()
# This is the array of NewComer objects that people who join are added to. # This is the array of NewComer objects that people who join are added to.
newcomers = [] newcomers = []
hello_list = [r'hello', r'hi', r'hey', r'yo', r'sup'] # Create a couple of regular expressions to use in the main loop
help_list = [r'help', r'info', r'faq', r'explain yourself'] # Basically, it creates a RE that matches something from the list + botnick
hello_pattern = get_welcome_regex(hello_list) hello_RE = re.compile(get_regex(hello_list), re.I)
help_pattern = get_welcome_regex(help_list) help_RE = re.compile(get_regex(help_list), re.I)
#################### The Workhorse #################### #################### The Workhorse ####################
@ -216,12 +219,10 @@ while 1: # loop forever
##### Unwelcome functions ##### ##### Unwelcome functions #####
# If someone talks to (or refers to) the bot. # If someone talks to (or refers to) the bot.
if ircmsg.find(botnick) != -1 and ircmsg.find("PRIVMSG") != -1: if botnick.lower() and "PRIVMSG".lower() in ircmsg.lower():
matchHello = search(hello_pattern, ircmsg) if hello_RE.search(ircmsg):
matchHelp = search(help_pattern, ircmsg)
if matchHello:
bot_hello(random.choice(hello_list)) bot_hello(random.choice(hello_list))
if matchHelp: if help_RE.search(ircmsg):
bot_help() bot_help()
# If someone tries to change the wait time... # If someone tries to change the wait time...