0

I've been making a bot for fun (and to learn) and noticed my command aren't previewed like you would see for other bots. Here is an example of a bot I made using javascript:

As you can see, when typing a '/' into the the message bar, the user gets a preview of all commands that use '/'. However, my bot's commands are not added to this list but still work.

How do I register these commands in a way that shows users the available commands when they type the command prefix? I will attach my source code to the bottom of this post. Thank you for the help!

import discord
import os
from discord.ext import commands
from dotenv import load_dotenv
from datetime import datetime, timezone

load_dotenv()
__TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='/', intents=intents)

active_channel = 'testing'

@bot.event
async def on_ready():
    try:
        print(f'Signed in as { bot.user }')
        await bot.tree.sync()
        print(f"Successfully synced commands.")
    except Exception as e:
        print(e)

@bot.event
async def on_message(message):
    if (message.author == bot.user):
        return
    
    if (message.channel == active_channel):
        if ('uncle terry' in message.content):
            await message.channel.send('Hi')
            
    await bot.process_commands(message)

@bot.command(name='ping')
async def ping(ctx):
    await ctx.send('pong')
    
@bot.command(name='time')
async def time(ctx):
    await ctx.send(f"It is {datetime.now(timezone.utc)} UTC.")

bot.run(__TOKEN)

Note: The bot is a little goofy and meant to be in my friends' server, so don't make fun of it too much :)

Yeonari
  • 61
  • 8
  • Does this answer your question? [How do i make a working slash command in discord.py](https://stackoverflow.com/questions/71165431/how-do-i-make-a-working-slash-command-in-discord-py) – Blue Robin Apr 05 '23 at 02:19

2 Answers2

2

Your code is not a slash command code it is just a code with a prefix set as /.

That is if I change / to . your command would function the same as .time

Make sure you have the latest DISCORD.PY version installed.


#The type of command you wanna create is called a slash command and comes
# under the application commands


import discord
from discord import app_commands
import os
from discord.ext import commands
from dotenv import load_dotenv
from datetime import datetime, timezone

intents = discord.Intents.default()
intents.message_content = True
bot= discord.Client(command_prefix='.',intents=intents)
tree= app_commands.CommandTree(bot)


#The tree holds all of your application commands.


@tree.command(name = "Time", description = "Gives TIme")
async def time(interaction):
    await interaction.response.send_message(f"It is {datetime.now(timezone.utc)} UTC.")


bot.run(__TOKEN)
#You can continue making normal commands by just writing


@bot.command(name='ping')
async def ping(ctx):
    await ctx.send('pong')


#And if u want to make it a slash command just do 

@tree.command(name = "Ping", description = "Checks ping")
async def time(interaction):
    await interaction.response.send_message("Pong")


Shweta K
  • 249
  • 2
  • 10
  • I thought creating a bot through `bot= discord.Client(command_prefix='.',intents=intents)` was preferred over creating a client on its own? Since a bot has all the functionality of the client, at least it says so on their docs. Thanks! – Yeonari Apr 04 '23 at 19:45
  • Naming the app command holder `client` may be a bit confusing. @Yeonari Client is just the app_commands.CommandTree(). – Blue Robin Apr 05 '23 at 02:13
  • @Yeonari i have updated the code by using tree as variable to reduce the confusion – Shweta K Apr 05 '23 at 17:37
0

It's not actually a slash command

bot.command() is just a text command. That's why it doesn't show up. As shown in the docs, bot.command invokes command() which transforms a function into a discord.ext.commands.Command which is:

A class that implements the protocol for a bot text command.

Changing the bot prefix to a slash changes nothing.

How to make a slash command

Using the answer from here:

  1. Import the app_commands:
    from discord import app_commands
  1. Define client and tree:
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
  1. Create an application command:
@tree.command(name = "ping", description = "Get bot ping")
async def ping(interaction):
    await interaction.response.send_message(f"Pong! {round(bot.latency * 1000)}ms")
  1. Update slash commands in on_ready:
@client.event
async def on_ready():
    await tree.sync(guild=discord.Object(id=Your guild id))
Blue Robin
  • 847
  • 2
  • 11
  • 31