Slash Commands | Pycord Guide (2024)

On March 24, 2021, Discord added Slash Commands to Discord as an easier, more efficient, and better way of using bot commands. Pycord has implemented Slash Commands into the library, so it's simple, efficient, and familiar.


Remember that Slash Commands require your bot to be invited with the application.commands scope or Slash Commands will not show up. Bots already in the guild can simply be invited again with the scope; there is no need to kick the bot.

Let's create a simple Slash Command.

import discord

bot = discord.Bot()

# we need to limit the guilds for testing purposes
# so other users wouldn't see the command that we're testing

@bot.command(description="Sends the bot's latency.") # this decorator makes a slash command
async def ping(ctx): # a slash command will be created with the name "ping"
await ctx.respond(f"Pong! Latency is {bot.latency}")"TOKEN")

Slash Commands | Pycord Guide (1)BobDotComused /ping

Slash Commands | Pycord Guide (2)


Pong! Latency is 335ms.

Let's go through the code.

First, we import Pycord's discord package.

Next, we create a discord.Bot object and assign it to a variable bot.

We then go ahead and use the @bot.command decorator, which registers a new Slash Command. We pass a description parameter to give a description to the Slash Command. We can also pass a name parameter to change the Slash Command's name. By default, the name of the Slash Command will be the name of the function, in this case, /ping.

We create an async function called ping with parameters ctx, which, when called, sends the bot's ping/latency using ctx.respond.

Subcommand Groups

You might want to group certain commands together to make them more organised. A command group is exactly what it sounds like, a group of individual Slash Commands together.

In order to make a Slash Command group, you can use the bot.create_group function.

import discord

bot = discord.Bot()

# create Slash Command group with bot.create_group
greetings = bot.create_group("greetings", "Greet people")

async def hello(ctx):
await ctx.respond(f"Hello, {}!")

async def bye(ctx):
await ctx.respond(f"Bye, {}!")"TOKEN")

Or, you can instead manually make a SlashCommandGroup class like so:

import discord

math = discord.SlashCommandGroup("math", "Math related commands")

async def add(ctx, num1: int, num2: int):
sum = num1 + num2
await ctx.respond(f"{num1} plus {num2} is {sum}.")

async def subtract(ctx, num1: int, num2: int):
sum = num1 - num2
await ctx.respond(f"{num1} minus {num2} is {sum}.")

# you'll have to manually add the manually created Slash Command group

Here's what the registered subcommands will look like in the Slash Command Menu:

Slash Commands | Pycord Guide (3)

You'll notice that there's the name of the Slash Command Group and then the name of the subcommand separated by a space.



If you are looking to add Slash Command Groups to cogs, please look at our Cogs page!

We've made a subcommand group, but did you know that you could create a group inside another?

from discord import SlashCommandGroup
from math import sqrt

math = SlashCommandGroup("math", "Math related commands")
advanced = math.create_subgroup("advanced", "Advanced math commands")

async def square_root(ctx, x: int):
await ctx.respond(sqrt(x))


The command created above can be invoked by typing /math advanced square_root.

Options & Option Types

Whenever you're using Slash Commands, you might notice that you can specify parameters that the user has to set or can optionally set. These are called Options.

Options can also include a description to provide more information. You can learn more about Options in our documentation!

Since you want different inputs from Options, you'll have to specify the type for that Option; there are a few ways of doing this.

  • Using Type Annotations
  • Using option decorator

You could use Type Annotations and let Pycord figure out the option type or explicitly specified using the SlashCommandOptionType enum.

import discord

bot = discord.Bot()

# pycord will figure out the types for you
async def add(ctx, first: discord.Option(int), second: discord.Option(int)):
# you can use them as they were actual integers
sum = first + second
await ctx.respond(f"The sum of {first} and {second} is {sum}.")

# this explicitly tells pycord what types the options are instead
async def join(
first: discord.Option(discord.SlashCommandOptionType.string),
second: discord.Option(discord.SlashCommandOptionType.string)
joined = first + second
await ctx.respond(f"When you join \"{first}\" and \"{second}\", you get: \"{joined}\".")"TOKEN")

Slash Commands | Pycord Guide (4)BobDotComused /add

Slash Commands | Pycord Guide (5)


The sum of 1 and 1 is 2.

Slash Commands | Pycord Guide (6)BobDotComused /join

Slash Commands | Pycord Guide (7)


When you join "Py" and "cord", you get: "Pycord".

Discord's autocomplete allows developers to determine option choices that are used in a slash command option. You can do this by defining a function:

async def get_animal_types(ctx: discord.AutocompleteContext):
Here we will check if 'ctx.options['animal_type']' is a marine or land animal and return respective option choices
animal_type = ctx.options['animal_type']
if animal_type == 'Marine':
return ['Whale', 'Shark', 'Fish', 'Octopus', 'Turtle']
else: # is land animal
return ['Snake', 'Wolf', 'Lizard', 'Lion', 'Bird']

async def animal_command(
ctx: discord.ApplicationContext,
animal_type: discord.Option(str, choices=['Marine', 'Land']),
animal: discord.Option(str, autocomplete=discord.utils.basic_autocomplete(get_animal_types))
await ctx.respond(f'You picked an animal type of `{animal_type}` that led you to pick `{animal}`!')

Slash Commands | Pycord Guide (8)BobDotComused /animal

Slash Commands | Pycord Guide (9)


You picked an animal type of Marine that led you to pick Shark!


Autocomplete can only be used with slash commands.

Related Topics

  • Interactions Index
  • Rules and Common Practices
  • Cogs
Slash Commands | Pycord Guide (2024)
Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5913

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.