Skip to main content

Ragwort

PyPI Downloads Code style: black

Unstable experiments with pycord. An adaptation of tansy.

Slash Commands

ragwort provides a unique way to define options for slash commands.

Instead of needing a decorator per option or to define the option in one huge list (or using awkward annotations), ragwort allows you to define each option in the function itself. By using a special metadata function, you can specify what each argument/parameter in a function should be like as an option, with ragwort smartly handling the rest for you.

import discord
import ragwort

bot = discord.Bot(...)

@ragwort.slash_command(description="Nice test command, huh?")
async def test(
    ctx: discord.ApplicationContext,
    the_user: discord.Member = ragwort.Option(name="user", description="The user to ping."),
):
    await ctx.respond(the_user.mention)

bot.add_application_command(test)  # only needed if the command is not in a cog

[!TIP] You can also use from ragwort import X for individual imports if you prefer.

ragwort frequently also aliases classes and functions to make overlap between ragwort and discord easier to avoid; for example, instead of ragwort.slash_command, you could use ragwort_slash_command.

Subcommands

ragwort also supports defining subcommands in a similar way.

# see imports from last example

math = ragwort.SlashCommandGroup("math", "Math related commands")
advanced = math.create_subgroup("advanced", "Advanced math commands")

@advanced.command()
async def square_root(
    ctx: discord.ApplicationContext,
    x: int = ragwort.Option(description="The number to find the square root of.")
):
    await ctx.respond(x ** 0.5)

Bridge Commands

ragwort also provides support for bridge commands:

import discord
from discord.ext import bridge
import ragwort

bot = bridge.Bot(...)

@ragwort.bridge_command(description="Nice test command, huh?")
async def test(
    ctx: bridge.BridgeContext,
    the_user: discord.Member = ragwort.BridgeOption(name="user", description="The user to ping."),
):
    await ctx.respond(the_user.mention)

bot.add_bridge_command(test)  # only needed if the command is not in a cog

Autocomplete

ragwort also adds a new way of handling autocomplete functions for slash commands and bridge commands:

@ragwort.slash_command(name="animal")
async def animal_command(
    ctx: discord.ApplicationContext,
    animal_type: str = ragwort.Option(
        "The type of animal.", choices=["Marine", "Land"]
    ),
    animal: str = ragwort.Option("The animal you want to pick."),
):
    await ctx.respond(
        f"You picked an animal type of `{animal_type}` that led you to pick `{animal}`!"
    )


@animal_command.autocomplete("animal")
async def animal_autocomplete(ctx: discord.AutocompleteContext):
    animal_type = ctx.options["animal_type"]
    if animal_type == "Marine":
        options = ["Whale", "Shark", "Fish", "Octopus", "Turtle"]
    else:
        options = ["Snake", "Wolf", "Lizard", "Lion", "Bird"]

    return [o for o in options if o.lower().startswith(ctx.value.lower())]

Auto Defer

ragwort provides a way of making all slash commands automatically defer their responses, and the ability to change how said auto defer works at the bot, cog, and command levels.

To use auto defer, you can call the setup_auto_defer function with your bot instance:

import discord
import ragwort

bot = discord.Bot(...)
ragwort.setup_auto_defer(bot)

Bot Wide Auto Defer

By default, setup_auto_defer enables auto defer for all commands. However, you can change ragwort.setup_auto_defer to whatever you desire:

# only defers extensions and commands marked
# to be autodeferred - see the next sections
ragwort.setup_auto_defer(bot, default=False)

# or
ragwort.setup_auto_defer(
    bot,
    default=ragwort.AutoDefer(
        enabled=True,
        ephemeral=False,
        time_until_defer=0.0
    )  # allows more custom behavior
)

Cog Wide Auto Defer

You can also set up auto defer at the cog level by using the cog_auto_defer decorator. This will apply the auto defer settings to all commands within that cog:

@ragwort.cog_auto_defer(enabled=True, ephemeral=False, time_until_defer=0.0)
class MyCog(discord.Cog):
    @ragwort.slash_command()
    async def my_command(self, ctx: discord.ApplicationContext):
        await ctx.respond("Hello, World!")

Command Level Auto Defer

Finally, you can set up auto defer at the command level by using the auto_defer decorator. This will apply the auto defer settings to that specific command:

@ragwort.slash_command()  # works for normal slash commands too
@ragwort.auto_defer(enabled=True, ephemeral=False, time_until_defer=0.0)
async def my_command(self, ctx: discord.ApplicationContext):
    await ctx.respond("Hello, World!")

Other Notes

  • Auto defer is applied in the following order: command level, then cog level, then bot level. This means that if a command has auto defer disabled (with enabled=False), but the cog or bot has it enabled, the command will not be deferred. This is useful for commands that send modals.
  • Auto defer partially works with bridge commands; the slash variant of the command will be deferred, but the prefixed variant will not be.

Metadata

Release files for ragwort 0.1.3

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

Source distribution (sdist)

Source distribution for ragwort 0.1.3
File Size Uploaded
ragwort-0.1.3.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ragwort 0.1.3
File Interpreter ABI Platform
ragwort-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 28.9 kB

Release files / ragwort-0.1.3.tar.gz

Download URL ragwort-0.1.3.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
738edae533e73c4ca41045386564077f3c214be1f7735ffe17646a123822a45c
BLAKE2b-256 checksum
How to use checksums
b9bc97227f2a530f9fb71b8e7e0242bfcef39b876d835a5cc85a00d52c53bc67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.20

Release files / ragwort-0.1.3-py3-none-any.whl

Download URL ragwort-0.1.3-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f3eaba2fe102303c05fe5119af137dc1e24fd1d9be5c37f950c0739c41c4ef84
BLAKE2b-256 checksum
How to use checksums
f6eebb4a97249b6c0674ce7eb15d4683e791cae2fdb7738c2970f10452552635
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.20

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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