示例#1
0
文件: bot.py 项目: Jeglet/pcbot
def main():
    """ The main function. Parses command line arguments, sets up logging,
    gets the user's login info, sets up any background task and starts the bot. """
    # Setup logger with level specified in start_args or logging.INFO
    logging.basicConfig(
        filename=start_args.log_file,
        level=start_args.log_level,
        format="%(levelname)s %(asctime)s [%(module)s / %(name)s]: %(message)s"
    )

    # Always keep the websockets.protocol logger at INFO as a minimum unless --enable-protocol-logging is set
    if not start_args.enable_protocol_logging:
        discord_logger = logging.getLogger("websockets.protocol")
        discord_logger.setLevel(start_args.log_level if start_args.
                                log_level >= logging.INFO else logging.INFO)

    # Setup some config for more customization
    bot_meta = config.Config(
        "bot_meta",
        pretty=True,
        data=dict(
            name="PCBOT",
            command_prefix=config.default_command_prefix,
            case_sensitive_commands=config.default_case_sensitive_commands,
            display_owner_error_in_chat=False))
    config.name = bot_meta.data["name"]
    config.default_command_prefix = bot_meta.data["command_prefix"]
    config.default_case_sensitive_commands = bot_meta.data[
        "case_sensitive_commands"]
    config.owner_error = bot_meta.data["display_owner_error_in_chat"]

    # Set the client for the plugins to use
    plugins.set_client(client)
    utils.set_client(client)

    # Load plugin for builtin commands
    plugins.load_plugin("builtin", "pcbot")

    # Load all dynamic plugins
    plugins.load_plugins()

    # Login with the specified token if specified
    token = start_args.token or input("Token: ")

    login = [token]

    # Setup background tasks
    client.loop.create_task(add_tasks())

    try:
        client.run(*login)
    except discord.errors.LoginFailure as e:
        logging.error(utils.format_exception(e))
示例#2
0
def main():
    parser = ArgumentParser()
    parser.add_argument("--token",
                        "-t",
                        help="Specified login token",
                        required=True)
    start_args = parser.parse_args()

    plugins.set_client(client)
    plugins.load_plugins()

    log.info("Starting client...")
    client.run(start_args.token)
示例#3
0
文件: bot.py 项目: stoz/PCBOT
def main():
    """ The main function. Parses command line arguments, sets up logging,
    gets the user's login info, sets up any background task and starts the bot. """
    # Add all command-line arguments
    parser = ArgumentParser(description="Run PCBOT.")
    parser.add_argument("--version",
                        "-V",
                        help="Return the current version.",
                        action="version",
                        version=__version__)

    # Setup a login group for handling only token or email, but not both
    login_group = parser.add_mutually_exclusive_group()
    login_group.add_argument(
        "--token", "-t", help="The token to login with. Prompts if omitted.")
    login_group.add_argument("--email",
                             "-e",
                             help="Alternative email to login with.")

    parser.add_argument("--new-pass",
                        "-n",
                        help="Always prompts for password.",
                        action="store_true")
    parser.add_argument(
        "--log-level",
        "-l",
        help=
        "Use the specified logging level (see the docs on logging for values).",
        type=lambda s: getattr(logging, s.upper()),
        default=logging.INFO,
        metavar="LEVEL")
    start_args = parser.parse_args()

    # Setup logger with level specified in start_args or logging.INFO
    logging.basicConfig(
        level=start_args.log_level,
        format="%(levelname)s %(asctime)s [%(module)s / %(name)s]: %(message)s"
    )

    # Always keep discord.py logger at INFO as a minimum
    discord_logger = logging.getLogger("discord")
    discord_logger.setLevel(start_args.log_level if start_args.
                            log_level >= logging.INFO else logging.INFO)

    # Setup some config for more customization
    bot_meta = config.Config(
        "bot_meta",
        pretty=True,
        data=dict(
            name="PCBOT",
            command_prefix=config.default_command_prefix,
            case_sensitive_commands=config.default_case_sensitive_commands,
            display_owner_error_in_chat=False))
    config.name = bot_meta.data["name"]
    config.default_command_prefix = bot_meta.data["command_prefix"]
    config.default_case_sensitive_commands = bot_meta.data[
        "case_sensitive_commands"]
    config.owner_error = bot_meta.data["display_owner_error_in_chat"]

    # Set the client for the plugins to use
    plugins.set_client(client)
    utils.set_client(client)

    # Load plugin for builtin commands
    plugins.load_plugin("builtin", "pcbot")

    # Load all dynamic plugins
    plugins.load_plugins()

    # Handle login
    if not start_args.email:
        # Login with the specified token if specified
        token = start_args.token or input("Token: ")

        login = [token]
    else:
        # Get the email from commandline argument
        email = start_args.email

        password = ""
        cached_path = client._get_cache_filename(
            email)  # Get the name of the would-be cached email

        # If the --new-pass command-line argument is specified, remove the cached password
        # Useful for when you have changed the password
        if start_args.new_pass:
            if os.path.exists(cached_path):
                os.remove(cached_path)

        # Prompt for password if the cached file does not exist (the user has not logged in before or
        # they they entered the --new-pass argument)
        if not os.path.exists(cached_path):
            password = getpass()

        login = [email, password]

    # Setup background tasks
    client.loop.create_task(add_tasks())

    try:
        client.run(*login)
    except discord.errors.LoginFailure as e:
        logging.error(utils.format_exception(e))
示例#4
0
文件: bot.py 项目: PcBoy111/PCBOT
def main():
    """ The main function. Parses command line arguments, sets up logging,
    gets the user's login info, sets up any background task and starts the bot. """
    # Add all command-line arguments
    parser = ArgumentParser(description="Run PCBOT.")
    parser.add_argument("--version", "-V", help="Return the current version.",
                        action="version", version=__version__)

    # Setup a login group for handling only token or email, but not both
    login_group = parser.add_mutually_exclusive_group()
    login_group.add_argument("--token", "-t", help="The token to login with. Prompts if omitted.")
    login_group.add_argument("--email", "-e", help="The email to login to. Token prompt is default.")

    parser.add_argument("--new-pass", "-n", help="Always prompts for password.", action="store_true")
    parser.add_argument("--log-level", "-l",
                        help="Use the specified logging level (see the docs on logging for values).",
                        type=lambda s: getattr(logging, s.upper()), default=logging.INFO, metavar="LEVEL")
    start_args = parser.parse_args()

    # Setup logger with level specified in start_args or logging.INFO
    logging.basicConfig(level=start_args.log_level,
                        format="%(levelname)s %(asctime)s [%(module)s / %(name)s]: %(message)s")

    # Always keep discord.py logger at INFO as a minimum
    discord_logger = logging.getLogger("discord")
    discord_logger.setLevel(start_args.log_level if start_args.log_level >= logging.INFO else logging.INFO)

    # Setup some config for more customization
    bot_meta = config.Config("bot_meta", pretty=True, data=dict(
        name="PCBOT",
        command_prefix=config.command_prefix
    ))
    config.name = bot_meta.data["name"]
    config.command_prefix = bot_meta.data["command_prefix"]

    # Set the client for the plugins to use
    plugins.set_client(client)

    # Load plugin for builtin commands
    plugins.load_plugin("builtin", "pcbot")

    # Load all dynamic plugins
    plugins.load_plugins()

    # Handle login
    if not start_args.email:
        # Login with the specified token if specified
        token = start_args.token or input("Token: ")

        login = [token]
    else:
        # Get the email from commandline argument
        email = start_args.email

        password = ""
        cached_path = client._get_cache_filename(email)  # Get the name of the would-be cached email

        # If the --new-pass command-line argument is specified, remove the cached password
        # Useful for when you have changed the password
        if start_args.new_pass:
            if os.path.exists(cached_path):
                os.remove(cached_path)

        # Prompt for password if the cached file does not exist (the user has not logged in before or
        # they they entered the --new-pass argument)
        if not os.path.exists(cached_path):
            password = getpass()

        login = [email, password]

    # Setup background tasks
    client.loop.create_task(add_tasks())

    try:
        client.run(*login)
    except discord.errors.LoginFailure as e:
        logging.error(utils.format_exception(e))