Skip to main content

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

Release files for smalld-click 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smalld-click 0.1.0
File Size Uploaded
smalld-click-0.1.0.tar.gz 24.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for smalld-click 0.1.0
File Interpreter ABI Platform
smalld_click-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.9 kB

Release files / smalld-click-0.1.0.tar.gz

Download URL smalld-click-0.1.0.tar.gz
Size 24.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d093fe62bc03edb711784206db3b1caea359edef2a4d4f75e8f9a1683625d751
BLAKE2b-256 checksum
How to use checksums
43de44c5c95f5152c6ed057fac6b0ce0bf2c5cfa3539916d727afe6c082a159c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / smalld_click-0.1.0-py3-none-any.whl

Download URL smalld_click-0.1.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b6faea3e5af4dc7fa68b0603a129a2afc9e6d2730cf4eb129549c78300400ec3
BLAKE2b-256 checksum
How to use checksums
4c3c084cbcd70a0f7e42cd63be1a0b9f5daa78707121045dc43dd5a11f526f54
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

This release

0.1.0 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page