A Discord.py extension allowing you to pass flags as arguments.
Project description
Disclaimer: this is a quick fix to get the library to work on Discord.py v2
The changes made have not been tested exclusively as I made this module to upgrade my Discord.py version and maintain flags. Use at your own risk (taking pull requests and issues though). Parts of the code were directly copy-pasted from the core Discord.py.
This is an alternative to CircuitSacul/Flag-Parsing, which is not maintained.
Flag Parsing
A util for discord.py bots that allow passing flags into commands.
To install, run the following command:
pip install alt-discord-flags
2.1.0 changes how signatures appear. If you wish to use
the legacy signatures, use command.old_signature
instead.
Basic example usage:
import discord
from discord.ext import flags, commands
bot = commands.Bot("!")
# Invocation: !flags --count=5 --string "hello world" --user Xua --thing y
@flags.add_flag("--count", type=int, default=10)
@flags.add_flag("--string", default="hello!")
@flags.add_flag("--user", type=discord.User)
@flags.add_flag("--thing", type=bool)
@flags.command()
async def flags(ctx, **flags):
await ctx.send("--count={count!r}, --string={string!r}, --user={user!r}, --thing={thing!r}".format(**flags))
bot.add_command(flags)
Important note that @flags.command
MUST be under all @flags.add_flag
decorators.
@flags.add_flag
takes the same arguments as argparse.ArgumentParser.add_argument
to keep things simple.
Subcommands are just as simple:
@commands.group()
async def my_group(ctx):
...
@flags.add_flag("-n")
@my_group.command(cls=flags.FlagCommand)
async def my_subcommand(ctx, **flags):
...
Usage of discord.py's consume rest
behaviour is not perfect with discord-flags
,
meaning that you have to use a flag workaround:
@flags.add_flag("message", nargs="+")
@flags.command()
async def my_command(ctx, arg1, **options):
""" You can now access `message` via `options['message']` """
message = ' '.join(options['message'])
Project details
Release history Release notifications | RSS feed
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 alt-discord-flags-v2-1.0.0.tar.gz
.
File metadata
- Download URL: alt-discord-flags-v2-1.0.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60647c56070bf99de6a1671d754d1847174944fc451c1e304ee41157dc35116f |
|
MD5 | 48a5ca60a64a1db6f00985fca8d5d1c9 |
|
BLAKE2b-256 | 52cd8c0287cb0884eeeaf41427c2dfb9eb8181a8a381529005a4ecfe21e6e45e |
File details
Details for the file alt_discord_flags_v2-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: alt_discord_flags_v2-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50a9036fb3c24ebca9367db8d7527fb70dd04eecc7b4203bfc195cee5141d335 |
|
MD5 | 03a477f7bebd670044626a786f520fa2 |
|
BLAKE2b-256 | 2af975670681b30111e823335e47aeadde66554e15b4d9b5095a950d316211be |