Skip to main content

Add Click support to your SmallD bot.

Project description

SmallD-Click

PyPI version Build Maintainability Test Coverage Conventional Commits Discord

SmallD-Click is an extension for SmallD that enables the use of Click CLI applications as discord bots.

Installing

Install using pip:

$ pip install smalld-click

Example

import click

from smalld import SmallD
from smalld_click import SmallDCliRunner


@click.command()
@click.option("--count", default=1, help="Number of greetings.")
@click.option("--name", prompt="Your name", help="The person to greet.")
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo("Hello %s!" % name)


smalld = SmallD()

with SmallDCliRunner(smalld, hello, prefix="++"):
    smalld.run()

For this CLI example, if a user sends the message "++hello --count=2", then the bot will ask the user - by sending a message in the same channel - for their name, "Your name:".

If the user answers with "lymni", for example, the bot will send the message, "Hello lymni", twice.

Example Run

There is also a timeout for how long the bot will wait for the user's message, if the timeout is exceeded the bot will simply drop the execution of the command.

Guide

SmallDCliRunner(smalld, cli, prefix="", timeout=60, executor=None)

The SmallDCliRunner is the core class for running CLI applications.

  • smalld the SmallD instance for your bot.
  • prefix used to determine what messages to consider as invocations of the CLI application.
  • timeout how long will the bot wait for the user to respond to a prompt in seconds.
  • executor an instance of concurrent.futures.Executor used to execute commands. by default this is a concurrent.futures.ThreadPoolExecutor.

Instances of this class should be used as a context manager, to properly close the executor when the bot stops.

SmallDCliRunnerContext = namedtuple("SmallDCliRunnerContext", ["runner", "message"])

The context for this command invocation, consists of the runner itself, and the message payload that triggered the execution of this command.

get_runner_context()

Returns the current runner context. Must only be invoked inside of a command handler. This is similar to Click's get_current_context()

Patched functionality

You can use click.echo, and click.prompt directly to send/wait for messages. However, hidden prompts are not supported yet and shouldn't be used.

Note that, echo and prompt will send a message in the same channel as the message that triggered the command invocation.

Acknowledgements

Original idea by Princess Lana.

Contributing

  • Tox is used for running tests.
    • Run tox -e to run tests with your installed python version
    • Run tox -e fmt to format the code
  • Conventional Commits is used for commit messages and pull requests

Developing

Tox is used to setup and manage virtual environments when working on SmallD-Click

To run tests:

$ tox

To run the examples greet bot:

$ tox -e run -- examples/greet.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smalld-click-0.1.0.tar.gz (24.4 kB view hashes)

Uploaded Source

Built Distribution

smalld_click-0.1.0-py3-none-any.whl (5.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page