#4.Slash Commands in discord.py
Manpreet Singh

Manpreet Singh @mannu

About: Full Stack Developer from India proficient in latest technologies (Reactjs, Nextjs)

Location:
India
Joined:
Jun 10, 2023

#4.Slash Commands in discord.py

Publish Date: Jul 10 '23
85 6

Hey Fellows!
I am back with another post and this time we will learn about:

1.Slash Commands

  • What are slash commands?

    when you type (/) in your discord chat (if you have use application commands perms in channel) then u might see some suggestions. These are slash commands. It helps to interact with bots. Slash command got features like autocomplete.

  • How to create a slash Command?

    to create a slash command we will create an interaction.
    Now what is interacton? Well we will discuss about it.....

Slash Commands


2.Creating A Slash Command:

  1. First of all we will use a decorator:
@bot.tree.command(name="mannu",description="Mannu is a good boy")
Enter fullscreen mode Exit fullscreen mode

2.Under this decorator we will create our async def as we always do:

async def slash_command(interaction:discord.Interaction):
    # Your Logic Goes Here...
Enter fullscreen mode Exit fullscreen mode

3.in this def we will write a syntax to send message:

await interaction.response.send_message("Hello World!")
Enter fullscreen mode Exit fullscreen mode
  • Here's the complete code just in case:
@bot.tree.command(name="mannu",description="Mannu is a good boy")
async def slash_command(interaction:discord.Interaction):
    await interaction.response.send_message("Hello World!")
Enter fullscreen mode Exit fullscreen mode

Now let's try to understand what we did here:

In step 1, we used a decorator to create our command and in that decorator, I provided name and description of my command.

In step 2, I created a async def and in this async def there is param interaction which is discord.Interaction. its work iss same as ctx we used in our first .hello command. we will use it to control our bot.

now as our interaction is created let's create a logic

In step 3, I awaited a syntex to send a response of that interaction.

Before running our code we need to make sure our command gets synced with bot so we will add the following syntax in our on_ready event:

await bot.tree.sync()
Enter fullscreen mode Exit fullscreen mode

here's complete code

@bot.event
async def on_ready():
    await bot.tree.sync()
Enter fullscreen mode Exit fullscreen mode

Now Let's run our code and see does it work or not...

python main.py
Enter fullscreen mode Exit fullscreen mode

Now on typing (/) we can see our slash command

Slash Command

Slash Command output

Well I had to change the bot coz my bot was giving me error coz I accidently used discord in my bot username which isn't allowed.
I'll fix it in next post

So This is how We create our slash command. Hope it helps you.


Github: https://github.com/MannuVilasara/discord.py-tutorial
Refrance : https://discordpy.readthedocs.io/
Discord: https://discord.gg/pvvJvZU6nS


Up Next:

  • error handling.
  • accepting user input.

Comments 6 total

  • Madhav
    MadhavJul 11, 2023

    How many posts will this series be??

  • NDe
    NDeFeb 17, 2024

    i tried but its not working here code:
    import os
    import discord

    TOKEN = ('i dont show code to strangers')

    client = discord.Client()

    @client.tree.command(name="HW",description="hello world command")
    async def slash_command(interaction:discord.Interaction):
    await interaction.response.send_message("Hello World!")

    @client.event
    async def on_ready():
    print(f'{client.user} has connected to Discord!')

    client.run(TOKEN)

  • RyffTech
    RyffTechFeb 22, 2024

    how do i add multiple slash commands? I've tried adding another one using the same layout but discord only recognizes the first command .

    • Manpreet Singh
      Manpreet SinghMay 26, 2024

      its name as well as function name should be different and it will work out. sometimes commands doesn't show and das discord issue. just restart discord and it will work out

Add comment