Slash Commands

How to use Slash Commands with Pycord

This guide has been moved to https://namantech.me/pycord. Please visit the new guide instead, since this one might be outdated.

The new guide looks better, has better explanations, and is way better overall.

Docs: https://pycord.readthedocs.io/en/master/api.html#discord.Bot.slash_command

Simple Slash Command

Simple Example:

import discord
from discord.app import Option

bot = discord.Bot()

# Note: If you want you can use commands.Bot instead of discord.Bot
# Use discord.Bot if you don't want prefixed message commands

# With discord.Bot you can use @bot.command as an alias 
# of @bot.slash_command but this is overriden by commands.Bot


@bot.slash_command(guild_ids=[...])  # create a slash command for the supplied guilds
async def hello(ctx):
    """Say hello to the bot"""  # the command description can be supplied as the docstring
    await ctx.send(f"Hello {ctx.author}!")


@bot.slash_command(
    name="hi", description="Wish someone!"
)  # Not passing in guild_ids creates a global slash command (might take an hour to register)
async def global_command(ctx, num: int):  # Takes one integer parameter
    await ctx.send(f"This is a global command, {num}!")


@bot.slash_command(guild_ids=[...])
async def joined(
    ctx, member: discord.Member = None
):  # Passing a default value makes the argument optional
    user = member or ctx.author
    await ctx.send(f"{user.name} joined at {discord.utils.format_dt(user.joined_at)}")
    
    
choose = bot.command_group(
    name='choose', description='This is a slash command group!', guild_ids=[...])

@choose.command(name='member')
async def choose_a_user(ctx,
    user: Option(discord.Member, "Choose a member", required=False), #required is True by default
):
    await ctx.send(f"You chose {user}!")

@choose.command()
async def choose_a_channel(ctx,
    channel: Option(discord.Channel, "Choose a channel"),
):
    await ctx.send(f"You chose {channel}!")

bot.run("TOKEN")

You need to have Py-cord version 2.0 Alpha to use Slash Commands

Last updated