Skip to main content

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

Project description

DSlash

Version: 0.7.2 Code Style: black License: MIT PyPI: dslash Python: ^3.12

THIS PROJECT IS NO LONGER MAINTAINED

Danny has returned from a break, and Discord.py is now being maintained again, with support for slash commands. Nextcord also now has built-in support for slash commands. For new projects, you should use one of these options (or another).

Dslash will continue to receive minimal updates to keep it working, at least for the near future, but it will not receive support for new features.

One change Discord has made since this library stopped being maintained was to remove support for bots configuring permissions for their own commands. In order to avoid breaking bots, Dslash still allows permissions to be configured as before, but will now ignore them. The allow_roles, allow_users, disallow_roles, disallow_users, global_permissions and guild_permissions wrappers, as well as the default_permission and permissions parameters are all deprecated and should not be used.

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, subcommand
from nextcord import Embed, Interaction, Member, Role, Attachment

GUILD_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")
        )

    @subcommand()
    async def upload(self, interaction: Interaction, image: Attachment):
        """Upload a new cute image."""
        print(f"Uploading {image.proxy_url!r}...")
        await interaction.response.send_message("All done!")


@client.group
class Admin(CommandGroup):
    """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)


@client.command()
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.12+, 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.7.2.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

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

dslash-0.7.2-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dslash-0.7.2.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dslash-0.7.2.tar.gz
Algorithm Hash digest
SHA256 506e460aca9c8df5e77f12021c00841f5f79a489f2218e2d05b786ad8d8f12ee
MD5 cb51c19e8e9f27a3f9a3cdd1b2013591
BLAKE2b-256 95923633b0eb034d0d7dba2d9a8c081ca4915b2340c76f7c724900fef5f159da

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dslash-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dslash-0.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 855bf922b718a69084bb3e177b14dcf13611153069692bf557f9b58ad3b7b304
MD5 4b7024caf54c73fd4f136b23b849602b
BLAKE2b-256 93b2d58d71a0853926d219d77061ceeb1bb2ea6afef5456826730d8126d98dcc

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