Skip to content

Commit

Permalink
Fix for Issue shaunagm#5.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbertino committed May 3, 2014
1 parent 6717db3 commit b145445
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import csv
import Queue
import random
import re
from threading import Thread
from re import search


# Some basic variables used to configure the bot.
server = "irc.freenode.net"
Expand All @@ -18,6 +19,8 @@
channel_admins = ('shauna', 'paulproteus', 'marktraceur')
wait_time = 60 # amount of time after joining before bot replies to someone
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 ####################
Expand Down Expand Up @@ -91,14 +94,14 @@ def add_known_nick(new_known_nick):
nickwriter.writerow([new_known_nick])


# "I NEED NOTES!!!!", said the function.
def get_welcome_regex(string_array):
#make regex case-insenstive
pattern = r'(?i)'
for s in string_array:
pattern += r'(?:[ :]'+s+r'(?:[ \.!\?,\)]|$))|'
#delete trailing '|'
# Builds a regex that matches one of the options + (space) botnick.
def get_regex(options):
pattern = "("
for s in options:
pattern += s
pattern += "|"
pattern = pattern[:-1]
pattern += ").({})".format(botnick)
return pattern


Expand All @@ -108,7 +111,7 @@ def get_welcome_regex(string_array):
def wait_time_change():
for admin in channel_admins:
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())
ircsock.send("PRIVMSG {0} :{1} the wait time is changing to {2} "
"seconds.\n".format(channel, actor, finder.group()))
Expand Down Expand Up @@ -149,10 +152,10 @@ def wait_time_change():
# This is the array of NewComer objects that people who join are added to.
newcomers = []

hello_list = [r'hello', r'hi', r'hey', r'yo', r'sup']
help_list = [r'help', r'info', r'faq', r'explain yourself']
hello_pattern = get_welcome_regex(hello_list)
help_pattern = get_welcome_regex(help_list)
# Create a couple of regular expressions to use in the main loop
# Basically, it creates a RE that matches something from the list + botnick
hello_RE = re.compile(get_regex(hello_list), re.I)
help_RE = re.compile(get_regex(help_list), re.I)


#################### The Workhorse ####################
Expand Down Expand Up @@ -201,12 +204,10 @@ def wait_time_change():

##### Unwelcome functions #####
# If someone talks to (or refers to) the bot.
if ircmsg.find(botnick) != -1 and ircmsg.find("PRIVMSG") != -1:
matchHello = search(hello_pattern, ircmsg)
matchHelp = search(help_pattern, ircmsg)
if matchHello:
if botnick.lower() and "PRIVMSG".lower() in ircmsg.lower():
if hello_RE.search(ircmsg):
bot_hello(random.choice(hello_list))
if matchHelp:
if help_RE.search(ircmsg):
bot_help()

# If someone tries to change the wait time...
Expand Down

3 comments on commit b145445

@shaunagm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jbertino! We're adding tutorial-style documentation of various WelcomeBot features/tools and I was wondering if you wanted to create a brief tutorial on what regex is, how the python regex library works, and how WelcomeBot is using it! If not, no worries - someone else can write it, but I thought I'd ask the person who introduced regex to the codebase first. :)

@jbertino
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @shaunagm! Thanks so much for the opportunity, but I am not comfortable committing to this right now. I am in the military and I had to transfer soon after I started to learn Python and started contributing to this project. Unfortunately, after the move I was unable to continue learning and have since lost a lot of the gains that I made. That being said, regex is not something that I have enough knowledge about to write this tutorial.

I have just recently, in the last two weeks, started going through Python tutorials again and had planned on coming back to this project once I got comfortable enough with Python to contribute again. I really did enjoy helping out and contributing and the OpenHatch folks were always really nice and helpful.

Again, thanks for thinking of me and I am sorry again that I cannot help more, right now.

@shaunagm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jbertino. It's totally okay that you don't have time for this right now. WelcomeBot and OpenHatch will still be here whenever you're able to contribute again, and we look forward to having your help again. In the meantime, do feel free to say hello in the #openhatch channel - it's not just for current contributors, but for everyone in the OpenHatch community to hang out in.

I hope you are able to stay safe and healthy! Sending many good wishes your way.

Please sign in to comment.