Beispiel #1
0
def handle_mock(command: Command):
    """
    `!mock ([TEXT] | [NUM POSTS])` - Mocks the message from the specified number of
    messages back. If no number is specified, mocks the most recent message.
    """
    num_posts_back = None
    # Add 1 here to account for the calling user's message,
    # which we don't want to mock by default.
    if not command.has_arg():
        num_posts_back = 1
    elif is_number(command.arg):
        num_posts_back = int(command.arg) + 1

    if num_posts_back is None:
        response = mock_message(command.arg)
    elif num_posts_back > MAX_NUM_POSTS_BACK:
        response = f'Cannot recall messages that far back, try under {MAX_NUM_POSTS_BACK}.'
    elif num_posts_back < 0:
        response = 'Cannot mock into the future (yet)!'
    else:
        message_to_mock = get_nth_most_recent_message(command.channel_id,
                                                      num_posts_back)
        if message_to_mock is None:
            response = 'Something went wrong (likely insufficient conversation history).'
        else:
            response = mock_message(message_to_mock)

    command.reply_with(bot, response)
Beispiel #2
0
def handle_binify(command: Command):
    """
    `!binify (binary | ascii)` - Converts a binary string to an ascii string
    or vice versa
    """
    if not command.has_arg():
        response = "Please include string to convert."
    elif set(command.arg).issubset(["0", "1"]) and len(command.arg) > 2:
        if len(command.arg) % 8 != 0:
            response = "Binary string contains partial byte."
        else:
            response = ""
            for i in range(0, len(command.arg), 8):
                n = int(command.arg[i:i + 8], 2)
                if n >= 128:
                    response = "Character out of ascii range (0-127)"
                    break
                response += chr(n)
    else:
        response = ""
        for c in command.arg.replace("&amp;",
                                     "&").replace("&lt;",
                                                  "<").replace("&gt;", ">"):
            n = ord(c)
            if n >= 128:
                response = "Character out of ascii range (0-127)"
                break
            response += f"{n:08b}"

    command.reply_with(bot, response)
Beispiel #3
0
def handle_cat(command: Command):
    """
    `!cat` - Displays the moss cat. Brings torture to CSSE2310 students.
    """
    cat = "\n".join(
        ("```", "         __..--''``\\--....___   _..,_            ",
         "     _.-'    .-/\";  `        ``<._  ``-+'~=.     ",
         " _.-' _..--.'_    \\                    `(^) )    ",
         "((..-'    (< _     ;_..__               ; `'   fL",
         "           `-._,_)'      ``--...____..-'         ```"))
    command.reply_with(bot, cat)
Beispiel #4
0
def handle_uptime(command: Command):
    """
    `!uptime` - displays the current uptime for UQCSBot
    """
    t = datetime.now() - bot.start_time
    message = (
        "The bot has been online for" +
        f" {precisedelta(t, format='%.0f'):s}" +
        (f" (`{round(t.total_seconds()):d}` seconds)"
         if t.total_seconds() >= 60 else "") +
        f", since {bot.start_time.strftime('%H:%M:%S on %b %d'):s}"
        # adds ordinal suffix
        +
        f"{(lambda n: 'tsnrhtdd'[(n//10%10!=1)*(n%10<4)*n%10::4])(bot.start_time.day):s}."
    )
    command.reply_with(bot, message)
Beispiel #5
0
def handle_help(command: Command):
    """
    `!help [COMMAND]` - Display the helper docstring for the given command.
    If unspecified, will return the helper docstrings for all commands.
    """

    # get helper docs
    helper_docs = get_helper_docs(command.arg)
    if len(helper_docs) == 0:
        message = 'Could not find any helper docstrings.'
    else:
        message = '>>>' + '\n'.join(helper_docs)

    # post helper docs
    if command.arg:
        command.reply_with(bot, message)
    else:
        bot.post_message(command.user_id, message, as_user=True)