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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file smalld-click-0.1.0.tar.gz.

File metadata

  • Download URL: smalld-click-0.1.0.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.10

File hashes

Hashes for smalld-click-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d093fe62bc03edb711784206db3b1caea359edef2a4d4f75e8f9a1683625d751
MD5 19821f2933bbc67965787d44ff41640c
BLAKE2b-256 43de44c5c95f5152c6ed057fac6b0ce0bf2c5cfa3539916d727afe6c082a159c

See more details on using hashes here.

File details

Details for the file smalld_click-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: smalld_click-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.10

File hashes

Hashes for smalld_click-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6faea3e5af4dc7fa68b0603a129a2afc9e6d2730cf4eb129549c78300400ec3
MD5 04e99903c9eb3147bbed23a522e3b4b2
BLAKE2b-256 4c3c084cbcd70a0f7e42cd63be1a0b9f5daa78707121045dc43dd5a11f526f54

See more details on using hashes here.

Supported by

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