#4.Slash Commands in discord.py (2024)

Mannu

Posted on

#4.Slash Commands in discord.py (2) #4.Slash Commands in discord.py (3) #4.Slash Commands in discord.py (4) #4.Slash Commands in discord.py (5) #4.Slash Commands in discord.py (6)

#discord #python #beginners

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.....

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

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...

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

await interaction.response.send_message("Hello World!")

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

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()

here's complete code

@bot.eventasync def on_ready(): await bot.tree.sync()

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

python main.py

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

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)

Subscribe

NDe

NDe

  • Joined

Feb 17

  • Copy link

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)

Mannu

Mannu

Hey ! I am Mannu, 17 yo from India. I am more of a backend developer. feel free to contact

  • Email

    mannuvilasara@gmail.com

  • Location

    India

  • Education

    BTech CSE (First Year)

  • Pronouns

    He/Him

  • Work

    Coding,study,listening to music and taking whole lot of Caffeine

  • Joined

May 26

  • Copy link

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

RyffTech

RyffTech

Noob Python Dev

  • Location

    USA

  • Joined

Feb 22

  • Copy link

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

Mannu

Mannu

Hey ! I am Mannu, 17 yo from India. I am more of a backend developer. feel free to contact

  • Email

    mannuvilasara@gmail.com

  • Location

    India

  • Education

    BTech CSE (First Year)

  • Pronouns

    He/Him

  • Work

    Coding,study,listening to music and taking whole lot of Caffeine

  • Joined

May 26 • Edited on Jun 4 • Edited

  • Copy link

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

Madhav

Madhav

Hii I am Madhav (from India)A 17 year old jee aspirant who has just stepped into the world of programmingLooking forward to learning new things .

  • Location

    Chhattisgarh, India

  • Pronouns

    He / him / his

  • Work

    Wants to clear Jee

  • Joined

Jul 11 '23

  • Copy link

How many posts will this series be??

Mannu

Mannu

Hey ! I am Mannu, 17 yo from India. I am more of a backend developer. feel free to contact

  • Email

    mannuvilasara@gmail.com

  • Location

    India

  • Education

    BTech CSE (First Year)

  • Pronouns

    He/Him

  • Work

    Coding,study,listening to music and taking whole lot of Caffeine

  • Joined

Jul 11 '23

  • Copy link

who knows :)

For further actions, you may consider blocking this person and/or reporting abuse

#4.Slash Commands in discord.py (2024)
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5436

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.