A library which supplements Nextcord by adding support for slash commands.
Project description
DSlash
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 random
import logging
import traceback
from nextcord import Embed, Interaction, Member, Role
from dslash import Choices, 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)
class RPSChoices(Choices):
rock = 'Rock'
paper = 'Paper'
scissors = 'Scissors'
gun = 'Gun'
@client.command()
async def rps(interaction: Interaction, choice: RPSChoices = option('Your choice.')):
"""Play rock, paper, scissors."""
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)
Planned Features
- Using
Optional[...]
for optional arguments. - Using docstring conventions for option descriptions.
- Removing the
option()
default-as-decorator. - Class-based command groups, like
nextcord.ext.commands
cogs.
Compatibility with nextcord.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:
- Create a virtual environment:
poetry shell
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dslash-0.3.0.tar.gz
.
File metadata
- Download URL: dslash-0.3.0.tar.gz
- Upload date:
- Size: 15.1 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.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a9ebd7e6aa5c622b257b728b55a2442a2fe1d5eb7dc08fd91d16948b9039a9f |
|
MD5 | 8192de2ca2c800f92a2965b7f41ce290 |
|
BLAKE2b-256 | c5a5399ef097decaf08076833ef84ede05778ef97a5ec483234866ce63bf725d |
File details
Details for the file dslash-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: dslash-0.3.0-py3-none-any.whl
- Upload date:
- Size: 15.7 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.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b115a5ed388d12ddfc4bb04a57111920b7ec484950d73d15877e5820dff56b3 |
|
MD5 | 66a2f1d958fe991a78638afeccfcbcdb |
|
BLAKE2b-256 | 77c5fae502ef73c228112c32db5bc6125f8123b39a0d132ee6b6eeecbe7688d6 |