why don't you use
ON *:EXIT: .hsave -o <table_name> <file_name>
ON *:DISCONNECT: .hsave -o <table_name> <file_name>

That will save the hash table to the file when the bot exits or gets disconnected