Added try and except statements to fix Issue #18

This commit is contained in:
james.bertino 2014-05-23 16:03:34 -04:00
parent e15634bae9
commit d9f994ae98
2 changed files with 19 additions and 8 deletions

25
bot.py
View File

@ -3,12 +3,13 @@
######################################################################### #########################################################################
# Import some necessary libraries. # Import some necessary libraries.
import socket import socket
import time import time
import csv import csv
import Queue import Queue
import random import random
import re import re
from sys import exc_info
from threading import Thread from threading import Thread
@ -57,7 +58,7 @@ def msg_handler():
# Responds to server Pings. # Responds to server Pings.
def pong(): def pong():
ircsock.send("PONG :pingis\n") ircsock.send("PONG :pingis\n")
# This function responds to a user that inputs "Hello Mybot". # This function responds to a user that inputs "Hello Mybot".
@ -126,13 +127,13 @@ def get_regex(options):
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 = re.search(r'\d\d*', re.search(r'--wait-time \d\d*', ircmsg) finder = re.search(r'\d\d*', re.search(r'--wait-time \d\d*',
.group()) ircmsg).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()))
return int(finder.group()) return int(finder.group())
ircsock.send("PRIVMSG {0} :{1} you are not authorized to make that " ircsock.send("PRIVMSG {0} :{1} you are not authorized to make that change"
"change. Please contact one of the channel greeters, like {2}, for " ". Please contact one of the channel greeters, like {2}, for "
"assistance.\n".format(channel, actor, greeter_string("or"))) "assistance.\n".format(channel, actor, greeter_string("or")))
@ -191,7 +192,15 @@ while 1: # loop forever
# get the next msg in the queue # get the next msg in the queue
ircmsg = q.get() ircmsg = q.get()
# and get the nick of the msg sender # and get the nick of the msg sender
actor = ircmsg.split(":")[1].split("!")[0] try:
actor = ircmsg.split(":")[1].split("!")[0]
except IndexError:
err_log = open('./error.log', 'a')
err_log.write('\nError occurred: {0}.\nircmsg was: {1}.\nSystem '
'error was: {2}'
'\n'.format(time.strftime('%x %X %Z'),
ircmsg, exc_info()[0]))
err_log.close()
##### Welcome functions ##### ##### Welcome functions #####
# If someone has spoken into the channel... # If someone has spoken into the channel...
@ -231,4 +240,4 @@ while 1: # loop forever
# If the server pings us then we've got to respond! # If the server pings us then we've got to respond!
if ircmsg.find("PING :") != -1: if ircmsg.find("PING :") != -1:
pong() pong()

2
error.log Normal file
View File

@ -0,0 +1,2 @@
This log was created to help catch an error when a 'mystery message' is received by the bot: