from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
import time
import urllib2
import wikipedia
import argparse


def help():
    """
I am a bot.

~pym -- Gives link to pymbook.
<nickname>, hello or <nickname>: hello -- Hello's back!
<nickname>, ping or <nickname>: ping -- Pong's back!
~date or <nickname>, date or <nickname>: date -- Print current date and time.
~random -- display's a random snippet from commandlinefu.com
~whatis <word> -- fetches one sentence summary from wikipedia about word.
~help -- displays this help message
    """


class LoggingIRCClient(irc.IRCClient):

    def __init__(self):
        parser = argparse.ArgumentParser(description="Saves logs of ##testbot")
        parser.add_argument("-l", "--log", help="Saves the logs of the channel")
        args = parser.parse_args()

        self.args = args

    nickname = raw_input("Enter a nickname: ")

    def signedOn(self):
        self.join('##testbot')

    def privmsg(self, user, channel, msg):
        nick = self.nickname
        user = user.split('!')[0]
        if msg == '~help':
            self.msg(user, help.__doc__)
        if msg == '~pym':
            self.msg(channel, 'http://pymbook.readthedocs.org/en/latest/')
        if msg == '{}, hello'.format(nick) or msg == '{}: hello'.format(nick):
            self.msg(channel, '{}, hello!'.format(user))
        if msg == '{}, ping'.format(nick) or msg == '{}: ping'.format(nick):
            self.msg(channel, '{}, pong'.format(user))
        if msg == '{}, date'.format(nick) or msg == '{}: date'.format(nick) \
                or msg == '~date':
            self.msg(channel, "Current date & time " + time.strftime("%c"))
        if msg == '~random':
            clf = urllib2.urlopen(
                "http://www.commandlinefu.com/commands/random/plaintext").read()
            self.msg(channel, clf.split("\n\n")[1])
        if msg.startswith("~whatis"):
            self.msg(channel, wikipedia.summary(
                msg[msg.index(" ") + 1:], sentences=1).encode('utf-8'))
        if self.args.log:
            with open(self.args.log, 'a') as fobj:
                fobj.write(time.strftime('[%d-%m-%Y %H:%M:%S]') +
                           ' <' + user + '> ' + msg + '\n')


def main():
    f = protocol.ReconnectingClientFactory()
    f.protocol = LoggingIRCClient
    reactor.connectTCP('irc.freenode.net', 6667, f)
    reactor.run()

if __name__ == '__main__':
    main()

@@ -1,20 +1,80 @@
+import argparse%0Aimport textwrap%0Aimport time%0Aimport urllib2%0A%0A
 from twisted.interne
@@ -144,35 +144,8 @@
 irc%0A
-import time%0Aimport urllib2%0A
 impo
@@ -157,32 +157,16 @@
 kipedia%0A
-import argparse%0A
 %0A%0Adef he
@@ -179,16 +179,20 @@
     %22%22%22%0A
+    
 I am a b
@@ -196,16 +196,20 @@
 a bot.%0A%0A
+    
 ~pym -- 
@@ -231,16 +231,20 @@
 ymbook.%0A
+    
 %3Cnicknam
@@ -291,16 +291,20 @@
 s back!%0A
+    
 %3Cnicknam
@@ -348,16 +348,20 @@
 s back!%0A
+    
 ~date or
@@ -430,16 +430,20 @@
 d time.%0A
+    
 ~random 
@@ -495,16 +495,20 @@
 efu.com%0A
+    
 ~whatis 
@@ -573,16 +573,20 @@
 t word.%0A
+    
 ~help --
@@ -625,162 +625,147 @@
 %22%22%22%0A
-%0A%0Aclass LoggingIRCClient(irc.IRCClient):%0A%0A    def __init__(self):%0A        parser = argparse.ArgumentParser(description=%22Saves logs of ##testbot%22)%0A        
+parser = argparse.ArgumentParser(%0A    formatter_class=argparse.RawDescriptionHelpFormatter,%0A    description=textwrap.dedent(help.__doc__))%0A
 pars
@@ -833,24 +833,16 @@
 annel%22)%0A
-        
 args = p
@@ -861,16 +861,82 @@
 args()%0A%0A
+%0Aclass LoggingIRCClient(irc.IRCClient):%0A%0A    def __init__(self):%0A%0A
         
@@ -1217,16 +1217,32 @@
 g(user, 
+textwrap.dedent(
 help.__d
@@ -1246,16 +1246,17 @@
 __doc__)
+)
 %0A       

improve cli help message and cleaned up argument parser