Add Click support to your SmallD bot.
Project description
SmallD-Click
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.
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 ofconcurrent.futures.Executor
used to execute commands. by default this is aconcurrent.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
- Run
- 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
Built Distribution
File details
Details for the file smalld-click-0.2.0.tar.gz
.
File metadata
- Download URL: smalld-click-0.2.0.tar.gz
- Upload date:
- Size: 24.2 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d7feccd63a0afd2315e51cec4ab7adde9733f1a3b2271ccdb646513caf192e5 |
|
MD5 | b4d153d091ff948d836f4a92b99746e7 |
|
BLAKE2b-256 | 4bf049a5d5938fe29f42ac008d649a9431af423f88545f76576bf6b0f3c80f91 |
File details
Details for the file smalld_click-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: smalld_click-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.1 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42170e1b64bf909061de60ce9936f96fa03a1edc08f1734fa904b624daea2208 |
|
MD5 | ceaa4c519401cae18fcd8a2165379b88 |
|
BLAKE2b-256 | 19612c6d6ca473f829053846d3f5aea3ae9ea77cd9da64762e93f6924e54b2d0 |