How to Create Slash Commands with Discord.py

I guess Discord.py docs are confusing to me.

I’ve spent an hour on researching this and nothing I found worked. I want to create a slash command /radar with an optional argument station.

I need guidance on simply creating the command and sending a message within it.

Here is my code currently:


# Import various libraries.
import os, discord, urllib.request
from discord.ext import commands
from discord_slash import SlashCommand
from discord_slash.utils import manage_commands

# Set the token.
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")

# Define whatever this is.
client = commands.Bot(command_prefix="!", intents= discord.Intents.all())
slash = SlashCommand(client, sync_commands=True)

# Login success event.
@client.event
async def on_ready():
    client.change_presence(status=discord.Status.online)
    print(f'We have logged in as {client.user}')

# Message respondings.
@client.event
async def on_message(message):
    print("message was: " + message.content)
    if message.author == client.user:
        return
    if message.content == '!radar':
        urllib.request.urlretrieve('https://radar.weather.gov/ridge/standard/CONUS_loop.gif', "rdr.gif")
        await message.channel.send(file=discord.File('rdr.gif'))

#Commands
@slash.slash(name="radar", description="Retrieves the live doppler radar for a station, if none given, gets CONUS.", options=[manage_commands.create_option(
    name = "Station",
    description = "The station ID (e.g. KIWX)",
    option_type = 3,
    required = False
  )])
async def _space(ctx: SlashContent):
	await ctx.send(file=discord.File('rdr.gif'))

# Command to start the bot.
def start(): 
    client.run(DISCORD_TOKEN)

and the error I get with this code:
image

Ok, I was following a tutorial on this one time and this is what I did.

  if msg.content.startwith('//hello'):
    await msg.channel.send('Hello, how are you?')

Also, where did you learn to code a discord bot? I am wanting to get into that even more here soon.

Yeah, I just read that error the “Discord Slash” isn’t something you can put in I believe. So if you remove it, and put in the code I send, it should work for you! :slight_smile:

Sounds good, but I need an interaction. You know, that option that comes up after you select the slash command and you can enter text into it?

Also, learning to make the bot was not easy. There were no working online tutorials, the docs are quite vague, and I ended up asking a ton of questions in many places. Here is the Repl link. You can work off of that.

2 Likes

Thanks, man, I know what you mean by it. It’s like all the tutorials are all outdated now. Idk why, thanks again for the link tho! <3

discord.py now supports interactions by default.
Instead of using @slash.slash, use @client.tree.command(name="", description="")
Put the on_ready() at the end of the file (or after you have declared all the commands anyways).
Try this, and see if it works

# Import various libraries.
import os, discord, urllib.request
from discord.ext import commands

# Set the token.
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")

# Define whatever this is.
intents = discord.Intents.default()
intents.message_content = True
client = commands.Bot(command_prefix='/', intents=intents)

# Message respondings.
@client.event
async def on_message(message):
    print("message was: " + message.content)
    if message.author == client.user:
        return
    if message.content == '!radar':
        urllib.request.urlretrieve('https://radar.weather.gov/ridge/standard/CONUS_loop.gif', "rdr.gif")
        await message.channel.send(file=discord.File('rdr.gif'))

#Commands
@client.tree.command(name="radar", description="Retrieves the live doppler radar for a station, if none given, gets CONUS.")
async def _space(ctx: discord.interactions.Interaction):
	await ctx.response.send_message(file=discord.File('rdr.gif'))

@client.event
async def on_ready():
    client.change_presence(status=discord.Status.online)
    await client.tree.sync()
    print(f'We have logged in as {client.user}')

# Command to start the bot.
def start(): 
    client.run(DISCORD_TOKEN)
1 Like

The code doesn’t work, there are no slash commands showing for my bot.

1 Like

Change that too

@client.event
async def on_ready():
    client.change_presence(status=discord.Status.online)
    print(f'We have logged in as {client.user}')
    await client.tree.sync()

You need to sync the commands with discord :slight_smile:

2 Likes

Wops sorry I forgot to add that. Thanks

Yes! The commands show now!
image

But… I get this error:

With this code:


# Import various libraries.
import os, discord, urllib.request
from discord.ext import commands

# Set the token.
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")

# Define whatever this is.
intents = discord.Intents.default()
intents.message_content = True
client = commands.Bot(command_prefix='/', intents=intents)

# Message respondings.
@client.event
async def on_message(message):
    print("message was: " + message.content)
    if message.author == client.user:
        return
    if message.content == '!radar':
        urllib.request.urlretrieve('https://radar.weather.gov/ridge/standard/CONUS_loop.gif', "rdr.gif")
        await message.channel.send(file=discord.File('rdr.gif'))

#Commands
@client.tree.command(name="radar", description="Retrieves the live doppler radar for a station, if none given, gets CONUS.")
async def _space(ctx: discord.interactions.Interaction):
	await ctx.reponse.send_message(file=discord.File('rdr.gif'))

@client.event
async def on_ready():
    await client.tree.sync()
    print(f'We have logged in as {client.user}')

# Command to start the bot.
def start(): 
    client.run(DISCORD_TOKEN)

Remember that I also need an optional interaction for the command.

Misspelling error, sorry. It’s response, not reponse, in _space

4 Likes

Thank you, the command works without error!

Now I just need to add an optional interaction.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.