Search code examples
pythondiscorddiscord.pybots

@client.command doesn't create a slash command


I have this python code, but the slash commands aren't working. When I run the code, the bots shows online, but slash commands don't work and no commands are logged in the console:

import discord #Imports the discord module.
from discord.ext import commands #Imports discord extensions.

#The below code verifies the "client".
client = commands.Bot(command_prefix='-t', intents=discord.Intents.all())
#The below code stores the token.
token = "[TOKEN]"

'''
The below code displays if you have any errors publicly. This is useful if you don't want to display them in your output shell.
'''
@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send('Please pass in all requirements :rolling_eyes:.')
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You dont have all the requirements :angry:")

#The below code bans player.
@client.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member : discord.Member, *, reason = None):
    await member.ban(reason = reason)

#The below code unbans player.
@client.command()
@commands.has_permissions(administrator = True)
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split("#")

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'Unbanned {user.mention}')
            return

#The below code runs the bot.
client.run(token)

I tried removing the intents=intents block at the start, but it gave an error, and nothing changed when I changed it to what it is now.

The bot has all the Privileged Gateway Intents. attachment 1 - Image Of Enabled Privileged Gateway Intents]


Solution

  • Why isn't this working?

    This has absolutely nothing to do with intents at all. You're really just creating some text-commands and expecting them to work like slash command which is not the case.

    You said that:

    but the slash commands aren't working

    But that's cause you're creating text-commands. Every time you use:

    @client.command()
    

    It's creating a text-based command. The documentation for this says:

    A shortcut decorator that invokes command() and adds it to the internal command list via add_command().

    The command() invocation "transforms a function into a Command." The definition of that is:

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

    What to do instead:

    See this Stack Overflow post.

    Sources