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.

Notice that the bot responds in a single message, instead of two, even though we call click.echo multiple times. This is because calls to echo are buffered. However, calls to prompt will cause this buffer to be flushed and its content is sent immediately.

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.

For an example with multiple commands that run under different names (i.e, with no common base command name) see the multicommands bot.

Guide

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

The SmallDCliRunner is the core class for running CLI applications.

  • smalld the SmallD instance for your bot.
  • cli a click.Command instance to use for running the commands.
  • prefix each command invocation must start with this string.
  • name the name of the CLI application, defaults to cli.name. Can be used to change the command's name, or completely remove it by passing the empty string. Used with the prefix 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.
  • create_message a callback for creating the message payload for discord's create message route. By default, text is sent as is in the content field of the payload.
  • 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 patch click functions and to properly close the executor when the bot stops.

Conversation(runner, message)

Represents the the state of the command invocation. Holds the runner instance, and the message payload. Also manages the interactions between the user and the CLI application.

After each prompt, the message is updated to the latest message sent by the user.

get_conversation()

Returns the current conversation. Must only be invoked inside of a command handler.

Patched functionality

You can use click.echo, and click.prompt directly to send/wait for messages.

prompts that are hidden, using hide_input=True, are sent to the user DM, and cause the conversation to continue there.

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

Calls to echo are buffered. When the buffer is flushed, its content is sent in 2K chunks (limit set by discord.) The buffer can be flushed automatically when there is a prompt, or the command finishes execution, or the content in the buffer exceeds the 2K limit.

It's also possible to flush the buffer by passing flush=True to click.echo call.

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.2.2

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.2.2
File Size Uploaded
smalld-click-0.2.2.tar.gz 24.8 kB Details

Built distribution (wheel)

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

Total release size: 32.4 kB

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

Download URL smalld-click-0.2.2.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0ec26171e687d0061372abb8720e874719ba66fe4b674ed022d699cdbc5271e1
BLAKE2b-256 checksum
How to use checksums
e76a61350be5a24c0d96bcb9fdae6725a0f05b0480ab474473b11480500ef734
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.48.2 CPython/3.6.11

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

Download URL smalld_click-0.2.2-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2d2d291bbdc66d9a0ac80aa645772ade2620b6182a767f0b0531d87d78e405a
BLAKE2b-256 checksum
How to use checksums
a2f02da35d72201a1112e9cfeecebff5aef62cfd83a0bf3ba69df61cbf7c08c1
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.48.2 CPython/3.6.11

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

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