# Getting started with new discord slash commands with Python

It is really hard to find anything reliable about this topic so I am writing about it myself.

# 1. Installation

You can install discord-py-slash-command module for slash commands

```
pip install -U discord-py-slash-command
``` 

## 2. basic bot with slash commands

- We need to import modules needed for this which are normal discord.py module and discord-py-slash-command module


```
import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
``` 

- We'll create our bot object with all intents and slash command object

```
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
slash = SlashCommand(bot)
``` 

- We'll create easy command, for this example I am going to create Hello World! command

```
@slash.slash(name="test")
async def _test(ctx: SlashContext):
    await ctx.send("Hello World!")
```

1. We created command named test

2. We started the command and named slash commands context to commands context

3. We sent message to channel where command was used

- Last but no least we need to trigger the bot on

```
bot.run("discord_token")
```

Whole code is:

```
import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
slash = SlashCommand(bot)

@slash.slash(name="test")
async def _test(ctx: SlashContext):
    await ctx.send("Hello World!")

bot.run("discord_token")
```

### 3. Slash command in Cog file

- This part is meant for advanced discord.py developers **not for beginners**

- In bot's main file you'll want to do same things as in basic slash commands but instead of creating command you'll want to load your cog file

```
from discord.ext import commands
from discord_slash import SlashCommand

bot = commands.Bot(command_prefix="prefix")
slash = SlashCommand(bot, override_type = True)

bot.load_extension("cog")
bot.run("TOKEN")
```

- In your Cog file you'll want to import everything needed and create class for your slash commands

```
# cog.py
import discord
from discord.ext import commands
from discord_slash import cog_ext, SlashContext

class Slash(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

def setup(bot):
    bot.add_cog(Slash(bot))
```

- In this Cog class you'll want to do your commands in this format shown below. I'll be doing same simple Hello World! program as the one in the basic bot

```
    @cog_ext.cog_slash(name="test")
    async def _test(self, ctx: SlashContext):
        await ctx.send("Hello World!")
```

Whole code:

```
import discord
from discord.ext import commands
from discord_slash import cog_ext, SlashContext

class Slash(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @cog_ext.cog_slash(name="test")
    async def _test(self, ctx: SlashContext):
        await ctx.send("Hello World!")

def setup(bot):
    bot.add_cog(Slash(bot))
```

#### 4. Thank you

Thank you for reading my first blog post, I hope it helped you atleast a bit
