Examples
A simple slash command
from discord.ext import commands
from dislash import InteractionClient
bot = commands.Bot(command_prefix="!")
inter_client = InteractionClient(bot)
@inter_client.slash_command(description="Sends Hello")
async def hello(interaction):
await interaction.reply("Hello!")
bot.run("BOT_TOKEN")
See also
What’s interaction? See SlashInteraction to learn more.
Slash embed
Let’s make something more complicated than /hello. For example, a command that generates an embed.
import discord
from discord.ext import commands
from dislash import InteractionClient, Option, OptionType
bot = commands.Bot(command_prefix="!")
inter_client = InteractionClient(bot)
test_guilds = [12345] # Insert ID of your guild here
@inter_client.slash_command(
guild_ids=test_guilds,
description="Builds a custom embed",
options=[
Option('title', 'Makes the title of the embed', OptionType.STRING),
Option('description', 'Makes the description', OptionType.STRING),
Option('color', 'The color of the embed', OptionType.STRING)
# Note that all args are optional
# because we didn't specify required=True in Options
]
)
async def embed(inter, title=None, description=None, color=None):
# Converting color
if color is not None:
try:
color = await commands.ColorConverter().convert(inter, color)
except:
color = None
if color is None:
color = discord.Color.default()
# Generating an embed
emb = discord.Embed(color=color)
if title is not None:
emb.title = title
if desc is not None:
emb.description = desc
# Sending the output
await inter.respond(embed=emb, hide_user_input=True)
bot.run("BOT_TOKEN")
See also
Option to learn more about slash command options.
Here’s the result we’ve just achieved:
Slash user-info
It’s time to work with different argument types. This example shows how to easily make a /user-info command
import discord
from discord.ext import commands
from dislash import InteractionClient, Option, OptionType
bot = commands.Bot(command_prefix="!")
inter_client = InteractionClient(bot)
test_guilds = [12345]
@inter_client.slash_command(
guild_ids=test_guilds,
name="user-info",
description="Shows user's profile",
options=[
Option("user", "Specify any user", OptionType.USER),
]
)
async def user_info(inter, user=None):
# Default user is the command author
user = user or inter.author
emb = discord.Embed(color=discord.Color.blurple())
emb.title = str(user)
emb.description = (
f"**Created at:** `{user.created_at}`\n"
f"**ID:** `{user.id}`"
)
emb.set_thumbnail(url=user.avatar_url)
await inter.respond(embed=emb)
bot.run("BOT_TOKEN")
Here’s how this slash command looks like in Discord: