def handler( self, command_string ): msg = helpers.prependNick( command_string ) msg += self.echo_message self.bot.privmsg( msg ) class Toggle( Command ): def __init__( self, bot = None, command = '', usage_format = '', toggle_variable = None ): Command.__init__( self, bot, command, usage_format ) self.toggle_variable = toggle_variable def handler( self, command_string ): vars( self.bot )[ self.toggle_variable ] = not vars( self.bot )[ self.toggle_variable ] msg = 'Bot\'s %s mode: %s' % ( self.toggle_variable, vars( self.bot )[ self.toggle_variable ] ) self.bot.privmsg( msg ) #START: Objects creation say = Command( command = 'say' ) google = WebQuery( command = 'google', url = 'https://www.google.com/search?q=' ) lmgtfy = WebQuery( command = 'lmgtfy', url = 'http://www.lmgtfy.com/?q=' ) yourcode = SingleEchoCommand( command = 'say', echo_message = 'https://github.com/vinaysrao/ircbot.git' ) god = SingleEchoCommand( command = 'god', echo_message = 'http://www.youtube.com/watch?v=8nAos1M-_Ts' ) listcommands = SingleEchoCommand( command = 'list', echo_message = helpers.commandList() ) quiet = Toggle( command = 'quiet', toggle_variable = 'quiet' ) welcome = Toggle( command = 'welcome', toggle_variable = 'welcome_new' )
def command( bot, line, socket ): #This is run if the bot's command symbol, by default '!' is found command, commandstring = helpers.getCmdAndCmdString( line ) command = command.lower() if command == '': return if command in [ 'say', 'echo' ]: bot.privmsg( commandstring ) if command in [ 'topic' ]: msg = helpers.prependNick( commandstring ) bot.privmsg( msg + "Topic is: " + bot.channeltopic ) if command in [ 'yourcode' ]: msg = helpers.prependNick( commandstring ) msg += 'https://github.com/vinaysrao/ircbot.git' bot.privmsg( msg ) if command in [ 'god' ]: msg = helpers.prependNick( commandstring ) msg += 'http://www.youtube.com/watch?v=8nAos1M-_Ts' bot.privmsg( msg ) if command in [ 'addnick' ]: nick = helpers.getnick( line ) if commandstring == '' or len( commandstring.split() ) > 1: return if helpers.isAdmin( bot, nick ): if bot.addKnownNick( commandstring ): bot.privmsg( commandstring + ' added to known nicks' ) if command in [ 'kick' ]: msg = chr( 1 ) + "ACTION kicks " + commandstring + chr( 1 ) bot.privmsg( msg ) if command in [ 'lmgtfy' ]: if commandstring == '': return query = commandstring.split( ' ', 1 ) if helpers.isNewNick( query[ 0 ], bot.activeNickList ): msg = '' query = '+'.join( query ) else: msg = query[ 0 ] + ': ' query = '+'.join( query[ 1: ] ) query = '+'.join( query.split() ) url = 'http://www.lmgtfy.com/?q=' + query msg += url bot.privmsg( msg ) if command in [ 'define' ]: s = wordnet.synsets( commandstring ) try: msg = s[ 0 ].definition.capitalize() except: msg = 'Definition not found' if commandstring == 'god': msg = "\' \'" bot.privmsg( msg ) if command in [ 'quit' ]: if( helpers.isAdmin( bot, helpers.getnick( line ) ) ): bot.serializeNicks() __import__( 'sys' ).exit( 0 ) if command in [ 'restart' ]: if( helpers.isAdmin( bot, helpers.getnick( line ) ) ): bot.serializeNicks() __import__( 'sys' ).exit( 1 ) if command in [ 'list' ]: msg = helpers.prependNick( commandstring ) msg += ', '.join( helpers.commandList() ) bot.privmsg( msg ) if command in 'togglewelcome': if( helpers.isAdmin( bot, helpers.getnick( line ) ) ): bot.welcome_new = not bot.welcome_new msg = 'Bot\'s welcome mode: %s' % bot.welcome_new bot.privmsg( msg )
def handler(self, command_string): msg = helpers.prependNick(command_string) msg += self.echo_message self.bot.privmsg(msg) class Toggle(Command): def __init__(self, bot=None, command="", usage_format="", toggle_variable=None): Command.__init__(self, bot, command, usage_format) self.toggle_variable = toggle_variable def handler(self, command_string): vars(self.bot)[self.toggle_variable] = not vars(self.bot)[self.toggle_variable] msg = "Bot's %s mode: %s" % (self.toggle_variable, vars(self.bot)[self.toggle_variable]) self.bot.privmsg(msg) # START: Objects creation say = Command(command="say") google = WebQuery(command="google", url="https://www.google.com/search?q=") lmgtfy = WebQuery(command="lmgtfy", url="http://www.lmgtfy.com/?q=") yourcode = SingleEchoCommand(command="say", echo_message="https://github.com/vinaysrao/ircbot.git") god = SingleEchoCommand(command="god", echo_message="http://www.youtube.com/watch?v=8nAos1M-_Ts") listcommands = SingleEchoCommand(command="list", echo_message=helpers.commandList()) quiet = Toggle(command="quiet", toggle_variable="quiet") welcome = Toggle(command="welcome", toggle_variable="welcome_new")
def command(bot, line, socket): #This is run if the bot's command symbol, by default '!' is found command, commandstring = helpers.getCmdAndCmdString(line) command = command.lower() if command == '': return if command in ['say', 'echo']: bot.privmsg(commandstring) if command in ['topic']: msg = helpers.prependNick(commandstring) bot.privmsg(msg + "Topic is: " + bot.channeltopic) if command in ['yourcode']: msg = helpers.prependNick(commandstring) msg += 'https://github.com/vinaysrao/ircbot.git' bot.privmsg(msg) if command in ['god']: msg = helpers.prependNick(commandstring) msg += 'http://www.youtube.com/watch?v=8nAos1M-_Ts' bot.privmsg(msg) if command in ['addnick']: nick = helpers.getnick(line) if commandstring == '' or len(commandstring.split()) > 1: return if helpers.isAdmin(bot, nick): if bot.addKnownNick(commandstring): bot.privmsg(commandstring + ' added to known nicks') if command in ['kick']: msg = chr(1) + "ACTION kicks " + commandstring + chr(1) bot.privmsg(msg) if command in ['lmgtfy']: if commandstring == '': return query = commandstring.split(' ', 1) if helpers.isNewNick(query[0], bot.activeNickList): msg = '' query = '+'.join(query) else: msg = query[0] + ': ' query = '+'.join(query[1:]) query = '+'.join(query.split()) url = 'http://www.lmgtfy.com/?q=' + query msg += url bot.privmsg(msg) if command in ['google']: if commandstring == '': return query = commandstring.split(' ', 1) if helpers.isNewNick(query[0], bot.activeNickList): msg = '' query = '+'.join(query) else: msg = query[0] + ': ' query = '+'.join(query[1:]) query = '+'.join(query.split()) url = 'https://www.google.com/search?q=' + query msg += url bot.privmsg(msg) if command in ['define']: try: from nltk.corpus import wordnet except ImportError: bot.privmsg('NLTK Not found!') return s = wordnet.synsets(commandstring) try: msg = s[0].definition.capitalize() except: msg = 'Definition not found' if commandstring == 'god': msg = "\' \'" bot.privmsg(msg) if command in ['ud']: meaning = urbandict.define(commandstring) if meaning is not None: msg = meaning else: msg = "Definition not found" msg = ' '.join(msg.splitlines()) bot.privmsg(msg) if command in ['quit']: if (helpers.isAdmin(bot, helpers.getnick(line))): bot.serializeNicks() __import__('sys').exit(0) if command in ['restart']: if (helpers.isAdmin(bot, helpers.getnick(line))): bot.serializeNicks() __import__('sys').exit(1) if command in ['list']: msg = helpers.prependNick(commandstring) msg += ', '.join(helpers.commandList()) bot.privmsg(msg) if command in 'togglewelcome': if (helpers.isAdmin(bot, helpers.getnick(line))): bot.welcome_new = not bot.welcome_new msg = 'Bot\'s welcome mode: %s' % bot.welcome_new bot.privmsg(msg) if command in 'quiet': if (helpers.isAdmin(bot, helpers.getnick(line))): bot.quiet = not bot.quiet