DEV Community

Mannu
Mannu

Posted on

#4.Slash Commands in discord.py

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: ```python

@bot.tree.command(name="mannu",description="Mannu is a good boy")

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


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: ```python

@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!")

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:
```python


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.

Top comments (6)

Collapse
 
ndev_discord profile image
NDe

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)

Collapse
 
mannu profile image
Mannu

use Bot from discord.ext.commands to create instance of client
discordpy.readthedocs.io/en/stable...

Collapse
 
ryfftech profile image
RyffTech

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

Collapse
 
mannu profile image
Mannu • Edited

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

Collapse
 
madhavseth profile image
Madhav

How many posts will this series be??

Collapse
 
mannu profile image
Mannu

who knows :)