Cannot match regex with re.search on Twisted Framework ircbot

64 views Asked by At

I'll start with "I'm not familiar with Python". I'm trying to change the default ircbot script from twisted for my channel, but I cannot get re.match or re.search working.

For example, this works:

prompt = ("%s" % self.nickname)
prompt1 = ("%s:" % self.nickname)
prompt2 = ("%s," % self.nickname)

if msg == (prompt1 + " whoareyou?") or msg == (prompt2 + " who are you?") or msg == (prompt1 + " whoareyou") or msg == (prompt2 + " who are you"):

This one as well

if msg == (prompt1 + " help") or msg == (prompt2 + " help"):

However these two conditions do not work on the bot (but they work on a local script):

if re.search(r'%s[:,] help' % self.nickname, msg):

elif re.search(r'%s[:,] who ?are ?you?' % self.nickname, msg):

A previous version of the script that is not using re.search can be found here

1

There are 1 answers

0
victorbrca On

I have found what was causing the problem. Twisted included a logic to handle nickname collision for the bot. It adds a ^ at the end of the name:

# For fun, override the method that determines how a nickname is changed on
# collisions. The default method appends an underscore.
def alterCollidedNick(self, nickname):
    """
    Generate an altered version of a nickname that caused a collision in an
    effort to create an unused related name for subsequent registration.
    """
    return nickname + '^'

This was causing my bots nickname to end with nickname^, and because I used %s to add the nickname variable as part of the Regex, it interpreted the ^ as a modifier.

if re.search(r'%s[:,] help' % self.nickname, msg):

To this:

if re.search(r'nickname^[:,] help', msg):