Esempio n. 1
0
def c(bot, trigger):
    """Google calculator."""
    if not trigger.group(2):
        if bot.config.lang == 'ca':
            return bot.reply("Res a calcular.")
        elif bot.config.lang == 'es':
            return bot.reply(u"Nada a calcular.")
        else:
            return bot.reply("Nothing to calculate.")
    try:
        result = str(eval_equation(trigger.group(2)))
    except ZeroDivisionError:
        if bot.config.lang == 'ca':
            result = u"La diviso entre zero no esta suportada en aquest univers."
        elif bot.config.lang == 'es':
            result = u"La division entre zero no esta soportada en ese universo."
        else:
            result = "Division by zero is not supported in this universe."
    except Exception:
        if bot.config.lang == 'ca':
            result = ("Ho sento, no puc calcular aixo amb aquesta ordre. "
                  "Potser en tinc un altre que pot fer-ho. "
                  "Escriu .ordres per una llista.")
        if bot.config.lang == 'es':
            result = ("Lo siento, pero no puedo calcular eso con ese comando. "
                  "Quizas tengo otro que si puede. "
                  "Escribe .comandos por una lista.")
        else:
            result = ("Sorry, I can't calculate that with this command. "
                      "I might have another one that can. "
                      "Use .commands for a list.")
    bot.reply(result)
Esempio n. 2
0
def c(bot, trigger):
    if not trigger.group(2):
        if bot.config.lang == 'ca':
            return bot.reply("Res a calcular.")
        elif bot.config.lang == 'es':
            return bot.reply(u"Nada a calcular.")
        else:
            return bot.reply("Nothing to calculate.")
    try:
        result = str(eval_equation(trigger.group(2).replace(',', '')))
    except ZeroDivisionError:
        if bot.config.lang == 'ca':
            result = u"La diviso entre zero no esta suportada en aquest univers."
        elif bot.config.lang == 'es':
            result = u"La division entre zero no esta soportada en ese universo."
        else:
            result = "Division by zero is not supported in this universe."
    except Exception:
        if bot.config.lang == 'ca':
            result = ("Ho sento, no puc calcular aixo amb aquesta ordre. "
                      "Potser en tinc un altre que pot fer-ho. "
                      "Escriu .ordres per una llista.")
        if bot.config.lang == 'es':
            result = ("Lo siento, pero no puedo calcular eso con ese comando. "
                      "Quizas tengo otro que si puede. "
                      "Escribe .comandos por una lista.")
        else:
            result = ("Sorry, I can't calculate that with this command. "
                      "I might have another one that can. "
                      "Use .commands for a list.")
    bot.reply(result)
Esempio n. 3
0
def roll(bot, trigger):
    # This regexp is only allowed to have one captured group, because having
    # more would alter the output of re.findall.
    dice_regexp = r"\d*d\d+(?:v\d+)?"

    # Get a list of all dice expressions, evaluate them and then replace the
    # expressions in the original string with the results. Replacing is done
    # using string formatting, so %-characters must be escaped.
    if not trigger.group(2):
        if bot.config.lang == 'ca':
            bot.reply("No m'has donat cap argument!")
        elif bot.config.lang == 'es':
            bot.reply("No me has dado ningun argumento!")
        else:
            bot.reply("You didn't give me any arguments!")
        return
    arg_str = trigger.group(2)
    dice_expressions = re.findall(dice_regexp, arg_str)
    arg_str = arg_str.replace("%", "%%")
    arg_str = re.sub(dice_regexp, "%s", arg_str)
    dice = map(_roll_dice, dice_expressions)
    if None in dice:
        if bot.config.lang == 'ca':
            bot.reply(u"Només tinc 1000 daus.")
        elif bot.config.lang == 'es':
            bot.reply(u"Solo tengo 1000 dados.")
        else:
            bot.reply(u"I only have 1000 dices.")
        return

    def _get_eval_str(dice):
        return "(%d)" % (dice.get_sum(),)

    def _get_pretty_str(dice):
        if dice.num <= 10:
            return dice.get_simple_string()
        elif dice.get_number_of_faces() <= 10:
            return dice.get_compressed_string()
        else:
            return "(...)"

    eval_str = arg_str % (tuple(map(_get_eval_str, dice)))
    pretty_str = arg_str % (tuple(map(_get_pretty_str, dice)))

    # Showing the actual error will hopefully give a better hint of what is
    # wrong with the syntax than a generic error message.
    try:
        result = eval_equation(eval_str)
    except Exception as e:
        bot.reply("SyntaxError, eval(%s), %s" % (eval_str, e))
        return

    bot.reply("You roll %s: %s = %d" % (
            trigger.group(2), pretty_str, result))
Esempio n. 4
0
def roll(bot, trigger):
    # This regexp is only allowed to have one captured group, because having
    # more would alter the output of re.findall.
    dice_regexp = r"\d*d\d+(?:v\d+)?"

    # Get a list of all dice expressions, evaluate them and then replace the
    # expressions in the original string with the results. Replacing is done
    # using string formatting, so %-characters must be escaped.
    if not trigger.group(2):
        if bot.config.lang == 'ca':
            bot.reply("No m'has donat cap argument!")
        elif bot.config.lang == 'es':
            bot.reply("No me has dado ningun argumento!")
        else:
            bot.reply("You didn't give me any arguments!")
        return
    arg_str = trigger.group(2)
    dice_expressions = re.findall(dice_regexp, arg_str)
    arg_str = arg_str.replace("%", "%%")
    arg_str = re.sub(dice_regexp, "%s", arg_str)
    dice = map(_roll_dice, dice_expressions)
    if None in dice:
        if bot.config.lang == 'ca':
            bot.reply(u"Només tinc 1000 daus.")
        elif bot.config.lang == 'es':
            bot.reply(u"Solo tengo 1000 dados.")
        else:
            bot.reply(u"I only have 1000 dices.")
        return

    def _get_eval_str(dice):
        return "(%d)" % (dice.get_sum(), )

    def _get_pretty_str(dice):
        if dice.num <= 10:
            return dice.get_simple_string()
        elif dice.get_number_of_faces() <= 10:
            return dice.get_compressed_string()
        else:
            return "(...)"

    eval_str = arg_str % (tuple(map(_get_eval_str, dice)))
    pretty_str = arg_str % (tuple(map(_get_pretty_str, dice)))

    # Showing the actual error will hopefully give a better hint of what is
    # wrong with the syntax than a generic error message.
    try:
        result = eval_equation(eval_str)
    except Exception as e:
        bot.reply("SyntaxError, eval(%s), %s" % (eval_str, e))
        return

    bot.reply("You roll %s: %s = %d" % (trigger.group(2), pretty_str, result))
Esempio n. 5
0
def roll(bot, trigger):
    """.dice XdY[vZ][+N], rolls dice and reports the result.

    X is the number of dice. Y is the number of faces in the dice. Z is the
    number of lowest dice to be dropped from the result. N is the constant to
    be applied to the end result.
    """
    # This regexp is only allowed to have one captured group, because having
    # more would alter the output of re.findall.
    dice_regexp = r"\d*d\d+(?:v\d+)?"

    # Get a list of all dice expressions, evaluate them and then replace the
    # expressions in the original string with the results. Replacing is done
    # using string formatting, so %-characters must be escaped.
    if not trigger.group(2):
        return bot.reply("No dice to roll.")
    arg_str = trigger.group(2)
    dice_expressions = re.findall(dice_regexp, arg_str)
    arg_str = arg_str.replace("%", "%%")
    arg_str = re.sub(dice_regexp, "%s", arg_str)

    f = lambda dice_expr: _roll_dice(bot, dice_expr)
    dice = list(map(f, dice_expressions))

    if None in dice:
        # Stop computing roll if there was a problem rolling dice.
        return

    def _get_eval_str(dice):
        return "(%d)" % (dice.get_sum(),)

    def _get_pretty_str(dice):
        if dice.num <= 10:
            return dice.get_simple_string()
        elif dice.get_number_of_faces() <= 10:
            return dice.get_compressed_string()
        else:
            return "(...)"

    eval_str = arg_str % (tuple(map(_get_eval_str, dice)))
    pretty_str = arg_str % (tuple(map(_get_pretty_str, dice)))

    # Showing the actual error will hopefully give a better hint of what is
    # wrong with the syntax than a generic error message.
    try:
        result = eval_equation(eval_str)
    except Exception as e:
        bot.reply("SyntaxError, eval(%s), %s" % (eval_str, e))
        return

    bot.reply("You roll %s: %s = %d" % (
        trigger.group(2), pretty_str, result))
Esempio n. 6
0
def c(bot, trigger):
    """Google calculator."""
    if not trigger.group(2):
        return bot.reply("Nothing to calculate.")
    try:
        result = str(eval_equation(trigger.group(2)))
    except ZeroDivisionError:
        result = "Division by zero is not supported in this universe."
    except Exception:
        result = ("Sorry, I can't calculate that with this command. "
                  "I might have another one that can. "
                  "Use .commands for a list.")
    bot.reply(result)
Esempio n. 7
0
def c(bot, trigger):
    """Google calculator."""
    if not trigger.group(2):
        return bot.reply("Nothing to calculate.")
    try:
        result = str(eval_equation(trigger.group(2)))
    except ZeroDivisionError:
        result = "Division by zero is not supported in this universe."
    except Exception:
        result = ("Sorry, I can't calculate that with this command. "
                  "I might have another one that can. "
                  "Use .commands for a list.")
    bot.reply(result)
Esempio n. 8
0
def c(bot, trigger):
    """Evaluate some calculation."""
    if not trigger.group(2):
        return bot.reply("Nothing to calculate.")
    # Account for the silly non-Anglophones and their silly radix point.
    eqn = trigger.group(2).replace(',', '.')
    try:
        result = eval_equation(eqn)
        result = "{:.10g}".format(result)
    except ZeroDivisionError:
        result = "Division by zero is not supported in this universe."
    except Exception as e:
        result = "{error}: {msg}".format(error=type(e), msg=e)
    bot.reply(result)
Esempio n. 9
0
def c(bot, trigger):
    """Evaluate some calculation."""
    if not trigger.group(2):
        return bot.reply("Nothing to calculate.")
    # Account for the silly non-Anglophones and their silly radix point.
    eqn = trigger.group(2).replace(',', '.')
    try:
        result = eval_equation(eqn)
        result = "{:.10g}".format(result)
    except ZeroDivisionError:
        result = "Division by zero is not supported in this universe."
    except Exception as e:
        result = "{error}: {msg}".format(error=type(e), msg=e)
    bot.reply(result)
Esempio n. 10
0
def c(bot, trigger):
    """Evaluate some calculation."""
    if not trigger.group(2):
        return bot.reply("Añade como parámetro algo a calcular.")
    # Account for the silly non-Anglophones and their silly radix point.
    eqn = trigger.group(2).replace(',', '.')
    try:
        result = eval_equation(eqn)
        result = "{:.10g}".format(result)
    except ZeroDivisionError:
        result = "La división por cero no existe en este universo."
    except Exception as e:
        result = "{error}: {msg}".format(
                error=type(e), msg=e)
    bot.reply(result)
Esempio n. 11
0
File: calc.py Progetto: icook/willie
def c(bot, trigger):
    """Google calculator."""
    if not trigger.group(2):
        return bot.reply("Nothing to calculate.")
    # Account for the silly non-Anglophones and their silly radix point.
    eqn = trigger.group(2).replace(',', '.')
    try:
        result = str(eval_equation(eqn))
    except ZeroDivisionError:
        result = "Division by zero is not supported in this universe."
    except Exception:
        result = ("Sorry, I can't calculate that with this command. "
                  "I might have another one that can. "
                  "Use .commands for a list.")
    bot.reply(result)
Esempio n. 12
0
def c(bot, trigger):
    """Google calculator."""
    if not trigger.group(2):
        return bot.reply("Nothing to calculate.")
    # Account for the silly non-Anglophones and their silly radix point.
    eqn = trigger.group(2).replace(',', '.')
    try:
        result = str(eval_equation(eqn))
    except ZeroDivisionError:
        result = "Division by zero is not supported in this universe."
    except Exception:
        result = ("Sorry, I can't calculate that with this command. "
                  "I might have another one that can. "
                  "Use .commands for a list.")
    bot.reply(result)