Skip to main content

Unstable experiments with pycord.

Project description

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.

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

ragwort-0.1.2.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ragwort-0.1.2-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file ragwort-0.1.2.tar.gz.

File metadata

  • Download URL: ragwort-0.1.2.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for ragwort-0.1.2.tar.gz
Algorithm Hash digest
SHA256 53519b034d305bfd864b44649f6701ff96acf2735b6710b3358b0e274d95acd6
MD5 12547cb3f7efca696c2a1762a2d49a2b
BLAKE2b-256 50c270d7bb1c2b7805025ac229d5c2527b1c63faa767629e1bd051086141ff76

See more details on using hashes here.

File details

Details for the file ragwort-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ragwort-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for ragwort-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e58bf62eee3e9e1be3a475a7cfa016aa3c356be4f7975c82f5eeaab2f7cb4769
MD5 21d718785bbc0811121ccfd3702047b1
BLAKE2b-256 ba931737085a7d7e82eee8e2438523d8ea9f876f5962a87c5eb248a6fc1528ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page