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.
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 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
- 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
Hashes for smalld_click-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6faea3e5af4dc7fa68b0603a129a2afc9e6d2730cf4eb129549c78300400ec3 |
|
MD5 | 04e99903c9eb3147bbed23a522e3b4b2 |
|
BLAKE2b-256 | 4c3c084cbcd70a0f7e42cd63be1a0b9f5daa78707121045dc43dd5a11f526f54 |