예제 #1
0
파일: builtin.py 프로젝트: xZwop/PCBOT
def ping(client: discord.Client, message: discord.Message):
    """ Tracks the time spent parsing the command and sending a message. """
    # Track the time it took to receive a message and send it.
    start_time = datetime.now()
    first_message = yield from client.say(message, "Pong!")
    stop_time = datetime.now()

    # Edit our message with the tracked time (in ms)
    time_elapsed = (stop_time - start_time).microseconds / 1000
    yield from client.edit_message(first_message,
                                   "Pong! `{elapsed:.4f}ms`".format(elapsed=time_elapsed))
예제 #2
0
파일: decide.py 프로젝트: EdwardBetts/PCBOT
def on_command(client: discord.Client, message: discord.Message, args: list):
    if args[0] == "!decide":
        if len(args) > 13:
            yield from client.send_message(
                message.channel, "I can't roll that many decisions.")
            return

        if len(args) > 2:
            choices = args[1:]
            rolls = randint(len(choices) * 3, len(choices) * 7)
            sleep_time = 0
            sleep_change = 2 / (rolls / 2)
            m = ""

            # Roll and send
            for i in range(rolls):
                if i > rolls / 2:
                    sleep_time += sleep_change

                # Select next choice
                choice = choices[i % len(choices)]

                if not m:
                    m = yield from client.send_message(message.channel,
                                                       "`" + choice + "`")
                else:
                    try:
                        m = yield from client.edit_message(
                            m, "`" + choice + "`")
                    except discord.errors.HTTPException:
                        pass

                if sleep_time:
                    sleep(sleep_time)

            yield from client.edit_message(
                m, "**" + m.content[1:-1] + "**" +
                " {}".format(message.author.mention))
        else:
            yield from client.send_message(message.channel,
                                           "Please see `!help decide`.")
예제 #3
0
파일: decide.py 프로젝트: EdwardBetts/PCBOT
def on_command(client: discord.Client, message: discord.Message, args: list):
    if args[0] == "!decide":
        if len(args) > 13:
            yield from client.send_message(message.channel, "I can't roll that many decisions.")
            return

        if len(args) > 2:
            choices = args[1:]
            rolls = randint(len(choices) * 3, len(choices) * 7)
            sleep_time = 0
            sleep_change = 2 / (rolls / 2)
            m = ""

            # Roll and send
            for i in range(rolls):
                if i > rolls / 2:
                    sleep_time += sleep_change

                # Select next choice
                choice = choices[i % len(choices)]

                if not m:
                    m = yield from client.send_message(message.channel, "`" + choice + "`")
                else:
                    try:
                        m = yield from client.edit_message(m, "`" + choice + "`")
                    except discord.errors.HTTPException:
                        pass

                if sleep_time:
                    sleep(sleep_time)

            yield from client.edit_message(m, "**" + m.content[1:-1] + "**" +
                                           " {}".format(message.author.mention))
        else:
            yield from client.send_message(message.channel, "Please see `!help decide`.")
예제 #4
0
파일: basic.py 프로젝트: EdwardBetts/PCBOT
def on_command(client: discord.Client, message: discord.Message, args: list):
    # Basic check
    if args[0] == "!ping":
        start = datetime.now()
        pong = yield from client.send_message(message.channel, "pong")
        end = datetime.now()
        response = (end - start).microseconds / 1000
        yield from client.edit_message(pong, "pong `{}ms`".format(response))

        logging.info("Response time: {}ms".format(response))

    # Roll from 1-100 or more
    elif args[0] == "!roll":
        if len(args) > 1:
            try:
                roll = random.randint(1, int(args[1]))
            except ValueError:
                roll = random.randint(1, 100)
        else:
            roll = random.randint(1, 100)

        yield from client.send_message(message.channel, "{0.mention} rolls {1}".format(message.author, roll))

    # Handle bot feature requests
    # (warning: this code is not representative of what I stand for in programming. I'm sorry.)
    elif args[0] == "!feature":
        m = "Please see `!help feature`."
        if len(args) > 2:
            plugin = args[1]
            if client.has_plugin(plugin):
                # Initialize the plugin for features
                if plugin not in feature_reqs.data:
                    feature_reqs.data[plugin] = []

                req_list = feature_reqs.data[plugin]

                # List feature request
                if args[2].startswith("#"):
                    feature_id = get_req_id(args[2])

                    if feature_id is not None:
                        if 0 <= feature_id < len(req_list):
                            m = "```diff\n" + format_req(plugin, feature_id) + "```"
                        else:
                            m = "There is no such feature. Try `!feature {} list`.".format(plugin)

                # List all feature requests or request with given ID.
                if args[2] == "list":
                    m = "```diff\n"
                    for req_id in range(len(req_list)):
                        m += format_req(plugin, req_id) + "\n"

                    m += "```"

                # Create a new feature
                elif args[2] == "new" or args[2] == "add" and len(args) > 3:
                    feature = " ".join(args[3:]).replace("\n", " ")

                    if feature not in req_list:
                        feature_reqs.data[plugin].append(feature)
                        feature_reqs.save()

                        m = "Feature saved as `{}` id **#{}**.".format(plugin, len(req_list))

                # Owner commands
                if client.is_owner(message.author):
                    # Mark a feature as complete
                    if args[2] == "mark" and len(args) > 3:
                        feature_id = get_req_id(args[3])

                        if feature_id is not None:
                            if 0 <= feature_id < len(req_list):
                                req = feature_reqs.data[plugin][feature_id]

                                if not req.endswith("+++"):
                                    feature_reqs.data[plugin][feature_id] += "+++"
                                    feature_reqs.save()

                                    m = "Marked feature with `{}` id **#{}**.".format(plugin, feature_id + 1)
                                else:
                                    feature_reqs.data[plugin][feature_id] = req[:-3]
                                    feature_reqs.save()

                                    m = "Unmarked feature with `{}` id **#{}**.".format(plugin, feature_id + 1)
                            else:
                                m = "There is no such feature. Try `!feature {} list`.".format(plugin)

                    # Remove a feature request
                    elif args[2] == "remove"and len(args) > 3:
                        feature_id = get_req_id(args[3])

                        if feature_id is not None:
                            if 0 <= feature_id < len(req_list):
                                feature_reqs.data[plugin].pop(feature_id)
                                feature_reqs.save()

                                m = "Removed feature with `{}` id **#{}**.".format(plugin, feature_id + 1)
                            else:
                                m = "There is no such feature. Try `!feature {} list`.".format(plugin)

            else:
                m = "Found no such plugin. Ask the bot owner if you are confused."

        yield from client.send_message(message.channel, m)
예제 #5
0
def on_command(client: discord.Client, message: discord.Message, args: list):
    # Basic check
    if args[0] == "!ping":
        start = datetime.now()
        pong = yield from client.send_message(message.channel, "pong")
        end = datetime.now()
        response = (end - start).microseconds / 1000
        yield from client.edit_message(pong, "pong `{}ms`".format(response))

        logging.info("Response time: {}ms".format(response))

    # Roll from 1-100 or more
    elif args[0] == "!roll":
        if len(args) > 1:
            try:
                roll = random.randint(1, int(args[1]))
            except ValueError:
                roll = random.randint(1, 100)
        else:
            roll = random.randint(1, 100)

        yield from client.send_message(
            message.channel,
            "{0.mention} rolls {1}".format(message.author, roll))

    # Handle bot feature requests
    # (warning: this code is not representative of what I stand for in programming. I'm sorry.)
    elif args[0] == "!feature":
        m = "Please see `!help feature`."
        if len(args) > 2:
            plugin = args[1]
            if client.has_plugin(plugin):
                # Initialize the plugin for features
                if plugin not in feature_reqs.data:
                    feature_reqs.data[plugin] = []

                req_list = feature_reqs.data[plugin]

                # List feature request
                if args[2].startswith("#"):
                    feature_id = get_req_id(args[2])

                    if feature_id is not None:
                        if 0 <= feature_id < len(req_list):
                            m = "```diff\n" + format_req(plugin,
                                                         feature_id) + "```"
                        else:
                            m = "There is no such feature. Try `!feature {} list`.".format(
                                plugin)

                # List all feature requests or request with given ID.
                if args[2] == "list":
                    m = "```diff\n"
                    for req_id in range(len(req_list)):
                        m += format_req(plugin, req_id) + "\n"

                    m += "```"

                # Create a new feature
                elif args[2] == "new" or args[2] == "add" and len(args) > 3:
                    feature = " ".join(args[3:]).replace("\n", " ")

                    if feature not in req_list:
                        feature_reqs.data[plugin].append(feature)
                        feature_reqs.save()

                        m = "Feature saved as `{}` id **#{}**.".format(
                            plugin, len(req_list))

                # Owner commands
                if client.is_owner(message.author):
                    # Mark a feature as complete
                    if args[2] == "mark" and len(args) > 3:
                        feature_id = get_req_id(args[3])

                        if feature_id is not None:
                            if 0 <= feature_id < len(req_list):
                                req = feature_reqs.data[plugin][feature_id]

                                if not req.endswith("+++"):
                                    feature_reqs.data[plugin][
                                        feature_id] += "+++"
                                    feature_reqs.save()

                                    m = "Marked feature with `{}` id **#{}**.".format(
                                        plugin, feature_id + 1)
                                else:
                                    feature_reqs.data[plugin][
                                        feature_id] = req[:-3]
                                    feature_reqs.save()

                                    m = "Unmarked feature with `{}` id **#{}**.".format(
                                        plugin, feature_id + 1)
                            else:
                                m = "There is no such feature. Try `!feature {} list`.".format(
                                    plugin)

                    # Remove a feature request
                    elif args[2] == "remove" and len(args) > 3:
                        feature_id = get_req_id(args[3])

                        if feature_id is not None:
                            if 0 <= feature_id < len(req_list):
                                feature_reqs.data[plugin].pop(feature_id)
                                feature_reqs.save()

                                m = "Removed feature with `{}` id **#{}**.".format(
                                    plugin, feature_id + 1)
                            else:
                                m = "There is no such feature. Try `!feature {} list`.".format(
                                    plugin)

            else:
                m = "Found no such plugin. Ask the bot owner if you are confused."

        yield from client.send_message(message.channel, m)