1 All the api's
Qemlokriu edited this page 2 years ago

API Class

The API class is used to interact with the bot, primarily for managing plugins and getting bot status. Here are some of the methods available in the API class:

  • get_config(): Returns the content of the config.toml file in an array.
  • save_config(): Dumps all the TOML data from self.config and stores it inside config.toml.
  • log(status, log): Connects the main .log file to the API so plugins can log to latest.log without risking corrupting the file. This function is not very useful as you can log directly from the bot instance.
  • uptime(): Gets the uptime of the bot.

The API class constructor takes the following arguments:

  • config: The contents of the config.toml file.
  • bot: An instance of the bot.
  • log: A function for logging.
  • token: The Discord bot token.
  • cli: An instance of the CLI class.

CLI Class

The CLI class provides a command-line interface to the bot. Here are some of the methods available in the CLI class:

  • register_command(command, function, description): Registers a new command in the CLI with the specified function and description.
  • remove_command(command): Removes a command from the CLI.
  • handle_input(user_input): Handles user input from the CLI.
  • run(): Starts the CLI loop.

The CLI class constructor takes the API instance as an argument.

Here's an example of how to use the API and CLI classes together:

import toml

# Load the contents of the config.toml file
with open("config.toml", "r") as f:
    config = toml.load(f)

# Create an instance of the bot
bot = MyBot(config["bot"]["token"])

# Create an instance of the API
api = API(config, bot, bot.log, bot.token, None)

# Create an instance of the CLI
cli = CLI(api)

# Register some commands with the CLI
def say_hello(cli, name):
    print(f"Hello, {name}!")

cli.register_command("hello", say_hello, "Says hello to the specified name.")

# Start the bot and CLI
bot.start()
cli.run()
Table of Contents