Skip to main content

A library which supplements Discord.py by adding support for slash commands.

Project description

DSlash

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

A library which supplements Discord.py by adding support for slash commands.

Unlike other libraries, this uses Discord.py's new support for Interactions, which means it works easily with Discord.py's Buttons and Selects.

Documentation is still a work in progress, and the library should currently be considered unstable. It will remain so at least until Discord.py v2 is released and stable.

You can install it using pip, eg. pip install dslash. Currently, because Discord.py v2 has not been published to PyPI, you will have to install it separately: pip install git+https://github.com/Rapptz/discord.py.

Example

import random
import logging
import traceback

from discord import Embed, Interaction, Member, Role
from dslash import CommandClient, SlashCommandInvokeError, allow_roles, option


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: int = option('How many sides (default 6).')):
    """Roll a dice."""
    value = random.randint(1, sides or 6)
    await interaction.response.send_message(f'You got: {value}')


images = client.group('images', 'Cute image commands.')


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


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


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


admin = client.group(
    'admin',
    'Admin-only commands.',
    default_permission=False,
    permissions=allow_roles(ADMIN_ROLE_ID)
)
roles = admin.subgroup('roles', 'Commands to manage roles.')


@roles.subcommand(name='del')
async def del_(
        interaction: Interaction, role: Role = option('The role to delete.')):
    """Delete a role."""
    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 = option('The user to ban.')):
    """Ban a user."""
    await user.ban()
    await interaction.response.send_message('Banned the user.', ephemeral=True)


client.run(TOKEN)

Planned Features

  • Class-based command groups, like discord.ext.commands cogs.

Compatibility with discord.ext.commands is not planned.

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.1.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

dslash-0.1.1-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dslash-0.1.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Linux/5.10.41-1-MANJARO

File hashes

Hashes for dslash-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f1383d28b1873837cf5fa9e795a9f512d349272fc9bf39babec877fa87c0b462
MD5 6169eccb85894143f389a6095d6caac9
BLAKE2b-256 dc87d1cb52ea99174bb1981cd26b6eeb259a178c83c9afd3957e8e55ad7f6e47

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dslash-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Linux/5.10.41-1-MANJARO

File hashes

Hashes for dslash-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 275391b9f54b595cb4bc7d35a0fdf2eb76271359d5bab47e768e8096c17e4b23
MD5 67cdee17e16c57b90dea4bcd6bebe61f
BLAKE2b-256 8b8f5b7d0dab6cf774d5d4de43aec8189c2d4ee8697e68bdd217281d959fb741

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