As part of a learning project I'm writing a simple IRC bot and I've implemented simple text functions like !auth
, !join
, !say
, etc. In order to make it easy to add functions I've implemented each in python as bot_auth()
, bot_join()
, bot_say()
, etc, with a fallthrough to a bot_NOCOMMAND()
that logs an error.
The problem I'm having is that while most functions are found by getattr()
as expected the join
and part
functions are not, and I can't figure out why.
Code:
def process_bot_command(self, prefix, params, trailing):
# eg: !command some arguments
try:
bot_command,bot_args = trailing[1:].split(' ',1)
bot_command = bot_command.lower().strip()
bot_args = bot_args.strip()
except ValueError:
bot_command = trailing[1:].split(' ')[0].lower().strip()
bot_args = ''
try:
function = getattr(self, 'bot_'+bot_command)
function(prefix, params[0], bot_command, bot_args)
except AttributeError:
self.bot_NOCOMMAND(prefix, params[0], bot_command, bot_args)
def bot_NOCOMMAND(self, c_from, c_to, command, args):
self.log('Undefined command: %s' % ','.join([c_from, c_to, command, args]), 1)
def bot_auth(self, c_from, c_to, command, args):
user = self.parsePrefix(c_from)['name']
if self.authenticate(user, args):
self.irc_PRIVMSG(user, 'Authentication successful.')
self.admins.append(c_from)
self.log('Added %s to active admin list.' % c_from)
else:
self.irc_PRIVMSG(user, 'Authentication failed.')
# TODO: doesn't work for some reason
def bot_join(self, c_from, c_to, command, args):
if not self.isAdmin(c_from):
self.respond(c_from, c_to, self.str['unauth'])
return
channel = args.split(' ')[0].strip()
self.irc_JOIN(channel)
self.respond(c_from, c_to, 'Joining %s' % channel)
# TODO: doesn't work for some reason
def bot_part(self, c_from, c_to, command, args):
if not self.isAdmin(c_from):
self.respond(c_from, c_to, self.str['unauth'])
return
channel = args.split(' ')[0].strip()
if channel in self.subscribed_channels:
self.irc_PART(channel, 'Leaving channel')
self.respond(c_from, c_to, 'Left %s' % channel)
def bot_say(self, c_from, c_to, command, args):
if not self.isAdmin(c_from):
self.respond(c_from, c_to, self.str['unauth'])
return
if args[0] == '#':
channel,text = args.split(' ', 1)
self.irc_PRIVMSG(channel, text)
else:
self.respond(c_from, c_to, args)
Basic flow:
- If the function that handles incoming PRIVMSGs finds a
!
in the first character of thetrailing
data in sends the data toprocess_bot_command()
process_bot_command()
pops off the first portion of thetrailing
string, drops the!
, prependsbot_
, and callsgetattr()
to find a function with that name.- If a function is found, the input is passed along to the function.
- If a function is not found, the input is passed to
bot_NOCOMMAND()
which dumps the input into the log as an error.
Example output:
> Sammitch!~Sammitch@hostname.tld PRIVMSG JerkfaceMcAssbot :!auth password
* Authentication request for user: Sammitch succeeded.
< PRIVMSG Sammitch :Authentication successful.
* Added Sammitch!~Sammitch@hostname.tld to active admin list.
> Sammitch!~Sammitch@hostname.tld PRIVMSG JerkfaceMcAssbot :!identify
< PRIVMSG nickserv :IDENTIFY password
< PRIVMSG Sammitch :Sent identify command.
> NickServ!NickServ@services. NOTICE JerkfaceMcAssbot :You are now identified for JerkfaceMcAssbot.
> Sammitch!~Sammitch@hostname.tld PRIVMSG JerkfaceMcAssbot :!join ##systemadmins
! Undefined command: Sammitch!~Sammitch@hostname.tld,JerkfaceMcAssbot,join,##systemadmins
> Sammitch!~Sammitch@hostname.tld PRIVMSG JerkfaceMcAssbot :!part KitchenCommandCenter
! Undefined command: Sammitch!~Sammitch@hostname.tld,JerkfaceMcAssbot,part,KitchenCommandCenter
eg:
! Undefined command: Sammitch!~Sammitch@hostname.tld,JerkfaceMcAssbot,join,##systemadmins
Means that get_attr()
couldn't find a function named bot_join()
.
I can't figure out why only the bot_join()
and bot_part()
functions don't want to work while all of the others work just fine. Can anyone enlighten me as to what might be going wrong here?
Update
I commented out the try/catch from process_bot_command()
to get it to die with an exception for you guys in the comments, and the join/part functions started working. So then I added it back to see if I could figure out why it was breaking, but the code continues to work.
I have no idea why, but I guess problem solved?