A python wrapper for discord slash-commands
Project description
dislash.py
It's a small extending library for discord.py, that allows to register slash-commands and respond to relevant interactions with them.
Installation
Run any of these commands in terminal to install the lib:
pip install dislash.py
python3 -m pip install discord.py
Examples
Note, that this library does require discord.py installed.
Registering a slash-command
import discord
from discord.ext import commands
from dislash import slash_commands
# Import slash-command constructor
from dislash.interactions import *
# Init a client instance using discord.py
client = commands.Bot(
command_prefix="!",
intents=discord.Intents.default()
)
# Init a <SlashClient> instance
slash_client = slash_commands.SlashClient(client)
@client.event
async def on_connect():
# Let's register a /random command
sc = SlashCommand(
name="random",
description="Returns a random number from the given range",
options=[
Option(
name="start",
description="Enter a number",
required=True,
type=4 # Type-4 means INTEGER
# Read more about types in docs
),
Option(
name="end",
description="Enter a number",
required=True,
type=4
)
]
)
# Post this command via API
await slash_client.register_global_slash_command(sc)
# Discord API uploads global commands for more than 1 hour
# That's why I highly recommend .register_guild_slash_command for testing:
await slash_client.register_guild_slash_command(guild_id, sc)
You should register a slash-command only once in order to make it work. You can always edit it if you want, using .edit_global_slash_command
/ .edit_guild_slash_command
methods.
Responding to a slash-command
import discord
from random import randint
from discord.ext import commands
from dislash import slash_commands
# Import slash-command constructor
from dislash.interactions import *
# Init a client instance using discord.py
client = commands.Bot(
command_prefix="!",
intents=discord.Intents.default()
)
# Init a <SlashClient> instance
# in order to start tracking slash-command interactions
slash_client = slash_commands.SlashClient(client)
# Let's make a function that responds to /random
@slash_client.command()
async def random(interaction):
# interaction is instance of `interactions.Interaction`
# Read more about it in docs
a = interaction.data.get_option('start').value
b = interaction.data.get_option('end').value
if b < a: a, b = b, a
await interaction.reply(randint(a, b))
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
dislash.py-0.0.1.tar.gz
(9.4 kB
view hashes)
Built Distribution
Close
Hashes for dislash.py-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13256289bf069458714e6bdae19e21f26a00fcce894c66814b5fad6359571c34 |
|
MD5 | c6c8d2a90a16132ff2868b7e4dbdf235 |
|
BLAKE2b-256 | d844c113f4dd5fa342abe82d78db920a91fa8c2725ed75f2f2d4cd7f51e9a531 |