Skip to main content

A library which supplements Nextcord by adding support for slash commands.

Project description

DSlash

Version: 0.5.1 Code Style: black License: MIT PyPI: dslash Python: ^3.9

A library which supplements Nextcord (a fork of Discord.py) by adding support for slash commands.

Documentation is still a work in progress, and the library should currently be considered unstable.

You can install it using pip, eg. pip install dslash.

Example

import logging
import random
import typing

from dslash import Choices, CommandClient, CommandGroup, CommandSubGroup, allow_roles, subcommand
from nextcord import Embed, Interaction, Member, Role

GUILD_ID = ...
ADMIN_ROLE_ID = ...
TOKEN = ...

logging.basicConfig(level=logging.INFO)
client = CommandClient(guild_id=GUILD_ID)


@client.event
async def on_ready():
    print(f"Logged in as {client.user}.")


@client.command()
async def roll(interaction: Interaction, sides: typing.Optional[int]):
    """Roll a dice.

    :param sides: How many sides (default 6).
    """
    value = random.randint(1, sides or 6)
    await interaction.response.send_message(f"You got: {value}")


@client.group
class Images(CommandGroup):
    """Cute image commands."""

    @subcommand()
    async def cat(self, interaction: Interaction):
        """Get a cat image."""
        await interaction.response.send_message(
            embed=Embed().set_image(url="https://cataas.com/cat")
        )

    @subcommand()
    async def dog(self, interaction: Interaction):
        """Get a dog image."""
        await interaction.response.send_message(
            embed=Embed().set_image(url="https://placedog.net/500?random")
        )

    @subcommand(name="any")
    async def any_(self, interaction: Interaction):
        """Get any random image."""
        await interaction.response.send_message(
            embed=Embed().set_image(url="https://picsum.photos/600")
        )


@client.group
@allow_roles(ADMIN_ROLE_ID)
class Admin(CommandGroup, default_permissions=False):
    """Admin-only commands."""

    class Roles(CommandSubGroup):
        """Commands to manage roles."""

        @subcommand(name="del")
        async def del_(self, interaction: Interaction, role: Role):
            """Delete a role.

            :param role: The role to delete.
            """
            await role.delete()
            await interaction.response.send_message("Deleted the role.", ephemeral=True)


@allow_roles(ADMIN_ROLE_ID)
@client.command(default_permission=False)
async def ban(interaction: Interaction, user: Member):
    """Ban a user.

    :param user: The user to ban.
    """
    await user.ban()
    await interaction.response.send_message("Banned the user.", ephemeral=True)


class RPSChoices(Choices):
    rock = "Rock"
    paper = "Paper"
    scissors = "Scissors"
    gun = "Gun"


@client.command()
async def rps(interaction: Interaction, choice: RPSChoices):
    """Play rock, paper, scissors.

    :param choice: Your choice.
    """
    if choice == RPSChoices.gun:
        await interaction.response.send_message("That's cheating!")
    else:
        await interaction.response.send_message(f"You picked {choice.name}.")


client.run(TOKEN)

Development

As well as Python 3.9+, this project requires Poetry for development. Click this link for installation instructions, or:

  • *nix (Linux/MacOS)

    curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -

  • Windows Powershell

    (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py -UseBasicParsing).Content | python -

Once you have Poetry installed:

  1. Create a virtual environment: poetry shell
  2. Install dependencies: poetry install

The following commands are then available:

  • poe format - Run auto-formatting and linting.

Prefix these with poetry run if outside of the Poetry shell.

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

dslash-0.5.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

dslash-0.5.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file dslash-0.5.1.tar.gz.

File metadata

  • Download URL: dslash-0.5.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for dslash-0.5.1.tar.gz
Algorithm Hash digest
SHA256 ee2166ce2b433cf82e930284af21c0aed409f949f45b3fb4134e31510f68a659
MD5 2bf5e8f157bfa950530a8b6c1779137b
BLAKE2b-256 9cca0b03245b802b7050ca56e339a4b115e1b64680b8ff5f647cddf36bc006e8

See more details on using hashes here.

File details

Details for the file dslash-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: dslash-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for dslash-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e4a979a31b3e93843f8d65656067949ea6f683064e796daf57550fc00bce52ee
MD5 adc60706fc6d55d1f8edb6205572e478
BLAKE2b-256 a67b6bdcf50a59a4209c978bbe9fbe12fdea62d43b5a97e25e22cc2ce7084d5c

See more details on using hashes here.

Supported by

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