예제 #1
0
def run_command(message_data):
    sender = message_data["sender"]
    full_text = message_data["full_text"]
    # "#channel" if room, "sender" if private message
    channel = message_data["channel"]
    command = message_data["command"]
    args = message_data["args"]

    def cringe_words(message):
        for w in ["xd", "spic", "cring", "derail"]:
            if w in "".join(c for c in "".join(message.lower().split())
                            if c.isalpha()):
                return True
        return False

    if sender == "eyy" and cringe_words(full_text):
        execute("KICK #bogota eyy ebin")

    # Refuse to do anything if not well fed by the users
    """
    if not food.enough(sender):
        message_queue.add("nah")
        return
    """

    # Reply to mention with a random quote
    if config.nickname in full_text and sender != config.nickname:
        message_queue.add(channel, random_quote(sender))

    # Give bux to users
    bank.make_money(sender, full_text)

    if not command:
        return

    elif command == "help":
        message_queue.add(
            sender,
            "Grass: grass-new (chip-value), grass-start, grass-join, gr, gb (amount), gp, gs, grass-cancel"
        )
        message_queue.add(
            sender,
            "Slots: slot-chips (amount), easy-slot <auto>, hard-slot <auto>, slot-stop, slot-cashout"
        )
        message_queue.add(
            sender,
            "Misc: pick [list], roll (number), ask (query), fetch (wikipedia_article), calc (expression), bux, sendbux (user) (amount), sharia (target) (amount)"
        )

    # Random choice
    elif command == "pick":
        if len(args) > 1:
            message_queue.add(channel, random.choice(args))

    # Die roll
    elif command == "roll":
        if not args:
            max_roll = 6
        elif len(args) == 1 and args[0].isdigit():
            max_roll = int(args[0])

        message_queue.add(channel, random.randint(1, max_roll))

    # Wolfram Alpha query
    elif command == "ask":
        response = wolfram.ask(" ".join(args))
        message_queue.add(channel, response)

    # Calculator
    elif command == "calc":
        expression = ''.join(args)
        result = str(calculate(expression))
        message_queue.add(channel, result)

    # Wikipedia fetch
    elif command == "fetch":
        article_name = ' '.join(args)
        extract = wikipedia.fetch(article_name)
        message_queue.add(channel, extract)

    # Check balance
    elif command == "bux":
        amount = bank.ask_money(sender)
        message_queue.add(channel,
                          "{} has {:.2f} newbux".format(sender, amount))

    # Transfer money
    elif command == "sendbux":
        if len(args) != 2:
            message_queue.add(channel, "eh")
            return
        source, destination, amount = sender, args[0], args[1]
        if not is_number(amount):
            message_queue.add(source, "numbers please")
            return
        bank.transfer_money(source, destination, float(amount))

    # Redistribute wealth
    elif command == "sharia":
        if len(args) != 2:
            message_queue.add(channel, "eh")
            return
        source, target, amount = sender, args[0], args[1]
        if not is_number(amount):
            message_queue.add(source, "numbers please")
            return
        bank.islamic_gommunism(source, target, float(amount), channel, users)

    # Grass game
    elif command == "grass-new":
        if len(args) < 1:
            message_queue.add(channel, "how much for each chip")
            return
        chip_value = args[0]
        if not is_number(chip_value):
            message_queue.add(source, "numbers please")
            return
        grass.new_game(sender, channel, float(chip_value))

    elif command == "grass-join":
        grass.add_player(sender, channel)

    elif command == "grass-start":
        grass.start(sender, channel)

    elif command == "gr":
        grass.play(sender, channel)

    elif command == "gb":
        if len(args) < 1:
            message_queue.add(channel, "how much are you betting")
            return
        bet = args[0]
        if not is_number(bet):
            message_queue.add(channel, "numbers please")
            return
        grass.bet(sender, bet, channel)

    elif command == "gp":
        grass.pass_turn(sender, channel)

    elif command == "gs":
        grass.print_chips(channel)

    elif command == "grass-cancel":
        grass.abort(channel)

    # Slot machine
    elif command == "slot-chips":
        if len(args) < 1:
            message_queue.add(channel, "how many are you buying")
            return
        amount = args[0]
        if not is_number(amount):
            message_queue.add(channel, "numbers please")
            return
        slots.buy_chips(sender, channel, int(amount))

    elif command == "easy-slot":
        auto = False
        if len(args) == 1 and args[0] == "auto":
            auto = True
        slots.start(sender, channel, slots.Games.EASY, auto=auto)

    elif command == "hard-slot":
        auto = False
        if len(args) == 1 and args[0] == "auto":
            auto = True
        slots.start(sender, channel, slots.Games.HARD, auto=auto)

    elif command == "slot-stop":
        slots.stop(sender, channel)

    elif command == "slot-cashout":
        slots.cash_out(sender, channel)

    ## Owner commands ##
    if sender == config.owner:
        # Disconnect
        if command == "quit":
            execute("QUIT")
            sys.exit(0)

        # Send message from bot
        elif command == "say":
            if len(args) > 1:
                message = ' '.join(args[1:])
                message_queue.add(args[0], message)

        # Print userlist
        elif command == "users":
            message_queue.add(channel, str(users))

        # Bot joins
        elif command == "join":
            channel = args[0]
            execute("JOIN %s" % channel)

        # Bot parts
        elif command == "part":
            execute("PART %s" % channel)
            del users[channel]

        # Bot kicks
        elif command == "kick":
            user = args[0]
            reason = " ".join(args[1:])
            if not reason:
                reason = "huh"
            bot_kick(channel, user, reason)

        # Module reloads
        elif command == "reload":
            module_name = args[0]
            importlib.reload(sys.modules[module_name])
            message_queue.add(channel, "aight")
예제 #2
0
def run_command(message_data):
    sender = message_data['sender']
    said = message_data['said']
    # '#channel' if room, 'sender' if private message
    current_channel = message_data['current_channel']
    params = message_data['params']

    # Get title from web pages
    if 'http://' in said:
        url = extract_url(said)
        title = get_title(url)
        if title:
            say(current_channel, 'Title: %s' % title)

    # Get link to Wikipedia article
    if '[[' in said:
        for article_name in extract_article(said):
            say(current_channel, get_link(article_name))

    # Reply to mention with a random quote
    if nickname in said:
        say(current_channel, random_quote(sender))

    ## IRC commands ##
    search_term = '+'.join(params)
    
    # List all commands
    if said.find('@help') == 0:
        say(sender, 'Search engines: google, wa, ddg, drae, dpd, en, es')
        say(sender, 'Misc: random [list], conv (unit) to (unit), fetch (wikipedia_article), link <start|get|check|stop>, calc (expression)')

    # Google
    elif said.find('@google') == 0:
        say(current_channel, 'https://www.google.com/search?q=%s' % search_term)

    # Wolfram Alpha
    elif said.find('@wa') == 0:
        say(current_channel, 'http://www.wolframalpha.com/input/?i=%s' % search_term)

    # DuckDuckGo
    elif said.find('@ddg') == 0:
        say(current_channel, 'http://duckduckgo.com/?q=%s' % search_term)

    # DRAE
    elif said.find('@drae') == 0:
        say(current_channel, 'http://lema.rae.es/drae/?val=%s' % search_term)

    # DPD
    elif said.find('@dpd') == 0:
        say(current_channel, 'http://lema.rae.es/dpd/?key=%s' % search_term)

    # Jisho kanji lookup
    elif said.find('@kan') == 0:
        escaped_term = urllib2.quote(search_term)
        say(current_channel, 'http://jisho.org/kanji/details/%s' % escaped_term)

    # EN > JP
    elif said.find('@ei') == 0:
        say(current_channel, 'http://jisho.org/words?jap=&eng=%s&dict=edict' % search_term)

    # JP > EN
    elif said.find('@ni') == 0:
        escaped_term = urllib2.quote(search_term)
        say(current_channel, 'http://jisho.org/words?jap=%s&eng=&dict=edict' % escaped_term)

    # EN > ES
    elif said.find('@en') == 0:
        say(current_channel, 'http://www.wordreference.com/es/translation.asp?tranword=%s' % search_term)

    # ES > EN
    elif said.find('@es') == 0:
        say(current_channel, 'http://www.wordreference.com/es/en/translation.asp?spen=%s' % search_term)

    # Random choice
    elif said.find('@random') == 0:
        if len(params) == 1:
            say(current_channel, 'f****t')
        elif len(params) > 1:
            say(current_channel, random.choice(said.split(',').strip()))
        else:
            say(current_channel, random.choice([0, 1]))

    # Unit converter
    elif said.find('@conv') == 0:
        if 'to' not in params:
            return
        index = params.index('to')
        amount = params[0]
        unit_from = params[1:index]
        unit_from = urllib2.quote(' '.join(unit_from))
        # 'to' == params[index]
        unit_to = params[index + 1:]
        unit_to = urllib2.quote(' '.join(unit_to))

        conversion_url = 'http://www.google.com/ig/calculator?hl=en&q='

        conversion = fetch_url(conversion_url + amount + unit_from + '=?' + unit_to).read()
        parsed_conversion = conversion.split('"')

        # Check for errors
        if len(parsed_conversion[5]) == 0:
            unit_result = urllib2.unquote(unit_to)
            say(current_channel, '%s %s' % (parsed_conversion[3].split()[0], unit_result))

    # Linkrace module
    elif said.find('@link') == 0:
        # Get race links
        if params[0] == 'get':
            url = 'http://es.wikipedia.org/wiki/%s'
            start, end = random_pair()
            starturl = url % urllib2.quote(start)
            endurl = url % urllib2.quote(end)
            say(current_channel, 'Start article is %s' % starturl)
            say(current_channel, 'Goal article is %s' % endurl)

        # Check if chain is valid
        elif params[0]  == 'check':
            chain = ' '.join(params[1:])
            broken_links = check_chain(chain)
            if not broken_links:
                say(current_channel, 'The chain is valid.')
            else:
                error_list = ' | '.join(broken_links)
                say(current_channel, error_list)
                say(current_channel, 'The chain is not valid.')

    # Calculator
    elif said.find('@calc') == 0:
        expression = ''.join(params)
        result = str(calculate(expression))
        say(current_channel, result)

    # Wikipedia fetch
    elif said.find('@fetch') == 0:
        article_name = ' '.join(params)
        extract = fetch(article_name)
        say(current_channel, extract)

    # Text game
    elif said.find('@dicks') == 0:
        global game
        # Commands available for everyone
        if params[0] == 'join':
            game.join_game(sender)
        elif params[0] == 'players':
            say(current_channel, [player.name for player in game.players])
        # Commands available for players
        if sender in [player.name for player in game.players]:
            if params[0] == 'panel':
                panel_url = sprunge(game.panel(sender))
                say(sender, '[i] Uploading panel')
                say(sender, panel_url)
            elif params[0] == 'settle':
                group = params[1]
                game.settle(sender, group)
            elif params[0] == 'move':
                troop = params[1]
                new_position = [params[2], params[3]]
                game.move(sender, troop, new_position)
    
    ## Owner commands ##
    if sender == owner:
        # Disconnect
        if said == '.quit':
            execute('QUIT')
            sys.exit(0)
        
        # Send message from bot
        elif said.find('.say') == 0:
            if len(params) > 1:
                say(params[0], ' '.join(params[1:]))

        # Print userlist
        elif said.find('.users') == 0:
            say(current_channel, str(users))

        # Bot joins
        elif said.find('.join') == 0:
            channel = params[0]
            execute('JOIN %s' % channel)

        # Bot parts
        elif said.find('.part') == 0:
            execute('PART %s' % current_channel)
            del users[current_channel]

        # Bot kicks
        elif said.find('.kick') == 0:
            user = params[0]
            reason = ' '.join(params[1:])
            if not reason:
                reason = 'huh'
            bot_kick(current_channel, user, reason)
예제 #3
0
파일: bot.py 프로젝트: ctrlcctrlv/raisin
def read_line(line):
    # Variables inside curly braces are optional

    # Dirty but better than having "self" all over the damn code
    global logged_in, deposit, scores

    # PING messages don't start with ':'
    if line.startswith(':'):
        ### Parsing ###
        # Complete command (:name!username@host command {args} :args)
        full_command = line.split(
        )  # [:name!username@host, command{, args}, :args]

        if len(full_command) < 2:
            return

        # Sender info (:name!username@host)
        sender_info = full_command[0]
        sender_info = sender_info.lstrip(':').split(
            '!')  # [name, username@host]
        sender = sender_info[0]  # name

        # Message and parameters (command {args} :args)
        message = full_command[1:]
        command = message[0]  # command

        ### Numeric replies ###
        # Initial connection
        if not logged_in and (command == '439' or 'NOTICE' in command):
            execute('NICK %s' % nickname)
            execute('USER %s %s %s :%s' %
                    (nickname, nickname, nickname, nickname))
            execute('NS IDENTIFY %s' % password)
            # execute('NS GHOST %s %s' % (nickname, password))
            logged_in = True

        # Start of MOTD
        elif command == '375':
            for channel in channels:
                execute('JOIN %s' % channel)

        # NAMES list
        elif command == '353':
            # message = ['353', bot_nickname, '=', #chan, :nick1, nick2, nick3]
            channel = message[3]  # #chan
            message[4] = message[4].lstrip(':')  # nick1
            names_list = message[4:]  # [nick1, nick2, nick3]

            for name in names_list:
                add_user(channel, name)

        ### Handle common messages ###
        elif command == 'KICK':
            current_channel = full_command[2]
            user = full_command[3]
            # Autojoin
            if user == nickname:
                execute('JOIN %s' % current_channel)
            # User KICKs
            else:
                remove_user(user, current_channel)
            deposit += 10

        # User JOINs
        elif command == 'JOIN' and sender != nickname:
            # message = ['JOIN', {':' + }#chan]
            current_channel = message[1].lstrip(':')
            add_user(current_channel, sender)

        # User PARTs
        elif command == 'PART':
            # message = ['PART', #chan, ':' + reason]
            current_channel = message[1]
            remove_user(current_channel, sender)

        # User QUITs
        elif command == 'QUIT':
            for channel in channels:
                remove_user(channel, sender)

        # User commands
        elif command == 'PRIVMSG':
            # message = ['PRIVMSG', #chan, ':' + word word word]
            message[2] = message[2].lstrip(':')

            current_channel = message[1]
            said = ' '.join(message[2:])
            params = message[3:]  # List of parameters (split by spaces)
            search_term = '+'.join(params)

            # Get title from web pages
            if 'http://' in said:
                url = extract_url(said)
                title = get_title(url)
                if title:
                    say(current_channel, 'Title: %s' % title)

            # Get link to Wikipedia article
            if '[[' in said:
                article = extract_article(said)
                link = get_link(article)
                if link:
                    say(current_channel, link)

            ## IRC commands ##
            # Commands with parameters
            if len(params) > 0:
                # Google
                if said.find('@g') == 0:
                    say(current_channel,
                        'https://www.google.com/search?q=%s' % search_term)

                # Wolfram Alpha
                elif said.find('@wa') == 0:
                    say(
                        current_channel,
                        'http://www.wolframalpha.com/input/?i=%s' %
                        search_term)

                # DuckDuckGo
                elif said.find('@ddg') == 0:
                    say(current_channel,
                        'http://duckduckgo.com/?q=%s' % search_term)

                # DRAE
                elif said.find('@drae') == 0:
                    say(current_channel,
                        'http://lema.rae.es/drae/?val=%s' % search_term)

                # DPD
                elif said.find('@dpd') == 0:
                    say(current_channel,
                        'http://lema.rae.es/dpd/?key=%s' % search_term)

                # Jisho kanji lookup
                elif said.find('@kan') == 0:
                    escaped_term = urllib2.quote(search_term)
                    say(current_channel,
                        'http://jisho.org/kanji/details/%s' % escaped_term)

                # EN > JP
                elif said.find('@ei') == 0:
                    say(
                        current_channel,
                        'http://jisho.org/words?jap=&eng=%s&dict=edict' %
                        search_term)

                # JP > EN
                elif said.find('@ni') == 0:
                    escaped_term = urllib2.quote(search_term)
                    say(
                        current_channel,
                        'http://jisho.org/words?jap=%s&eng=&dict=edict' %
                        escaped_term)

                # EN > ES
                elif said.find('@en') == 0:
                    say(
                        current_channel,
                        'http://www.wordreference.com/es/translation.asp?tranword=%s'
                        % search_term)

                # ES > EN
                elif said.find('@es') == 0:
                    say(
                        current_channel,
                        'http://www.wordreference.com/es/en/translation.asp?spen=%s'
                        % search_term)

                # Unit converter
                elif said.find('@conv') == 0:
                    if 'to' not in params:
                        return
                    index = params.index('to')
                    amount = params[0]
                    unit_from = params[1:index]
                    unit_from = urllib2.quote(' '.join(unit_from))
                    # 'to' == params[index]
                    unit_to = params[index + 1:]
                    unit_to = urllib2.quote(' '.join(unit_to))

                    conversion_url = 'http://www.google.com/ig/calculator?hl=en&q='

                    conversion = fetch_url(conversion_url + amount +
                                           unit_from + '=?' + unit_to).read()
                    parsed_conversion = conversion.split('"')

                    # Check for errors
                    if len(parsed_conversion[5]) == 0:
                        unit_result = urllib2.unquote(unit_to)
                        say(
                            current_channel, '%s %s' %
                            (parsed_conversion[3].split()[0], unit_result))

                # Linkrace module
                elif said.find('@link') == 0:
                    linkcommand = params[0]

                    # Get race links
                    if linkcommand == 'get':
                        url = 'http://es.wikipedia.org/wiki/%s'
                        start, end = random_pair()
                        starturl = url % urllib2.quote(start)
                        endurl = url % urllib2.quote(end)
                        say(current_channel, 'Start article is %s' % starturl)
                        say(current_channel, 'Goal article is %s' % endurl)

                    # Check if chain is valid
                    if linkcommand == 'check':
                        chain = ' '.join(params[1:])
                        broken_links = check_chain(chain)
                        if not broken_links:
                            say(current_channel, 'The chain is valid.')
                        else:
                            error_list = ' | '.join(broken_links)
                            say(current_channel, error_list)
                            say(current_channel, 'The chain is not valid.')

                # Calculator
                elif said.find('@calc') == 0:
                    expression = ''.join(params)
                    result = str(calculate(expression))
                    say(current_channel, result)

                # Wikipedia fetch
                elif said.find('@fetch') == 0:
                    article_name = ' '.join(params)
                    extract = fetch(article_name)
                    say(current_channel, extract)

                # Bet
                elif said.find('@bet') == 0:
                    wager = int(params[0])
                    if wager > deposit:
                        say(current_channel,
                            'Deposit is %s, cannot bet %s' % (deposit, wager))
                        return

                    # Cannot bet if last roll was 1 or 6
                    last = last_roll(sender)
                    if last == 1 or last == 6:
                        say(current_channel, 'Last roll was' % last)
                        return

                    roll = roll_dice(sender)
                    # Won the bet
                    if roll > last:
                        wallet[sender] += wager
                        deposit -= wager
                        balance = wallet[sender]
                        say(
                            current_channel,
                            '%s: roll: %s, +%sC, balance: %sC' %
                            (sender, roll, wager, balance))
                    # Lost the bet
                    else:
                        # User doesn't have enough money to pay
                        if wager > wallet[sender]:
                            deposit += wallet[sender]
                        # User has enough money to pay
                        else:
                            deposit += wager

                        wallet[sender] -= wager
                        balance = wallet[sender]
                        say(
                            current_channel,
                            '%s: roll: %s, -%sC, balance: %sC' %
                            (sender, roll, wager, balance))

                        # Kick if debt is greater than 10
                        if wallet[sender] < -10:
                            bot_kick(current_channel, sender, 'Debt')

            # Commands without parameters
            else:
                # Roll
                if said.find('@roll') == 0:
                    roll = roll_dice(sender)
                    # Roll 1, give 1C to deposit (if available)
                    if roll == 1 and wallet[sender] > 0:
                        deposit += 1
                        wallet[sender] -= 1
                        balance = wallet[sender]
                        say(
                            current_channel,
                            '%s: roll: %s, -1C, balance: %sC' %
                            (sender, roll, balance))
                    # Roll 6, get 1C from deposit (if available)
                    elif roll == 6 and deposit > 0:
                        deposit -= 1
                        wallet[sender] += 1
                        balance = wallet[sender]
                        say(
                            current_channel,
                            '%s: roll: %s, +1C, balance: %sC' %
                            (sender, roll, balance))
                    else:
                        say(current_channel, '%s: roll: %s' % (sender, roll))

                # Check deposit
                elif said.find('@deposit') == 0:
                    say(current_channel, 'Deposit: %s' % deposit)

                # Return last roll
                elif said.find('@last') == 0:
                    say(current_channel,
                        '%s: Last roll: %s' % (sender, last_roll(sender)))

                # List all wallets
                elif said.find('@wallets') == 0:
                    say(current_channel, str(wallet))

                # List own balance
                elif said.find('@balance') == 0:
                    balance = wallet[sender]
                    say(current_channel,
                        '%s: balance: %sC' % (sender, balance))

            ## Owner commands ##
            if sender == owner:
                # Disconnect
                if said == '.quit':
                    end_bot()

                # Print userlist
                elif said.find('.users') == 0:
                    say(current_channel, str(users))

                # Bot joins
                elif said.find('.join') == 0:
                    channel = said.split()[1]
                    execute('JOIN %s' % channel)

                # Bot parts
                elif said.find('.part') == 0:
                    execute('PART %s' % current_channel)
                    del users[current_channel]

                # Bot kicks
                elif said.find('.kick') == 0:
                    user = said.split()[1]
                    bot_kick(current_channel, user)

    # PING messages don't start with ':'
    else:
        # Pong
        if 'PING' in line:
            execute('PONG %s' % line.split()[1])