Improves slash commands for Python
Project description
Pyslash
Pyslash is a wrapper around discord-py-slash-command, that makes command creation more natural.
The examples provided are based of the examples from the original repository.
Quick startup
Firstly you must install the package, as explained here.
A simple setup using slash
and adding a basic command.
import discord
from discord.ext import commands
from pyslash import SlashCommand, SlashContext
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
s = SlashCommand(bot)
@s.slash(name="test")
async def _test(ctx: SlashContext, arg0: str):
embed = discord.Embed(title="embed test")
await ctx.send(content=arg0, embeds=[embed])
bot.run("discord_token")
As of 1.1.0
, you no longer need to import slash
from pyslash
and provide the instance as the first variable, as pyslash now has a subclass of SlashCommand
that does this for you.
Converters are automatically handled, for example
@s.slash(name="test")
async def _test(ctx: SlashContext, member: discord.Member):
await ctx.send(f"*taps mic* testing, {member.mention}")
And names don't have to be given
@s.slash()
async def foo(ctx: SlashContext, member: discord.Member):
# This command will automatically be called 'foo'
await ctx.send(f"Hello, {member.mention}")
Descriptions
By default, each argument and command has the description No description
, but that can be changed by providing a docstring. Docstrings are supported as provided by docstring-parser — at time of writing, that is ReST, Google, and Numpydoc.
from typing import Tuple, Literal
# ...
@s.slash()
async def foo(ctx: SlashContext, member: discord.Member):
"""
My command description here!
:param member: my description here
"""
# This command will automatically be called 'foo', and have the description
# "My command description here!", and the argument `member` will have the
# description "my description here".
await ctx.send(f"Hello, {member.mention}")
It's also possible to pass the command description through the decorator as follows, although that's not recommended (and will override any docstring provided description):
@s.slash(description="My description!")
async def command(ctx):
pass
Advanced usage
The same usage applies for cogs, but a different function is used.
# bot.py
from discord.ext import commands
from pyslash import SlashCommand
bot = commands.Bot(command_prefix="prefix")
slash = SlashCommand(bot, override_type = True)
bot.load_extension("cog")
bot.run("TOKEN")
# cog.py
import discord
from discord.ext import commands
from pyslash import SlashContext, slash_cog
class Slash(commands.Cog):
def __init__(self, bot):
self.bot = bot
@slash_cog(name="test")
async def _test(self, ctx: SlashContext):
await ctx.send("Hey there folks")
def setup(bot):
bot.add_cog(Slash(bot))
Installation
To install from pip, run
pip install dpyslash
To install from source, clone the repository and then build:
git clone https://github.com/starsflower/discordpy-slash-commands.git
cd discordpy-slash-commands
python setup.py install
Tests
To run basic tests, run
python -m tests/test
To run the test bot (that requires BOT_TOKEN
in the environment variables), and a further pip requirement of python-dotenv
python -m tests/test_bot
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
Built Distribution
File details
Details for the file dpyslash-1.2.2.tar.gz
.
File metadata
- Download URL: dpyslash-1.2.2.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.5.0 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 | 08a4202d92ae27592f9c4c8bcd963a96606a2eaa329d54f65092f3f6de4da00b |
|
MD5 | 7eb15771a325c86019f612be88e224bd |
|
BLAKE2b-256 | ca74f47b655a5c727a828380c94faebf9be5fe98c725ffedb74b397dbaac4d63 |
File details
Details for the file dpyslash-1.2.2-py3-none-any.whl
.
File metadata
- Download URL: dpyslash-1.2.2-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.5.0 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 | f6d63d5ed97f7f74fdfb8876366c5c1446d6fd047bce7d1c567361b8d7cc6e99 |
|
MD5 | 9891e1a27c2c4ccd6015fde9f5be32f8 |
|
BLAKE2b-256 | 5814e8c197394434f32f938c1109aeff055f17e97e088a1e45e26d067b9e8e1b |