Search code examples
pythondiscorddiscord.pybots

Making a Slash Command appear in Discord UI


On discord, the module discord.ext.commands lets you create commands. I tried to create a command, with prefix="/", although it won't appear in discord's UI.

Code:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="/")

@bot.command(name="kick", description="Kick a user to your wish.")
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.send(f"The user @{member} has been kicked. Reason: {reason}.")

bot.run("mytoken")

It won't pop up. no pop-up

But I want to do it like this: enter image description here


Solution

  • You need to install discord-py-slash-command version 1.1.2 and then put the code to import it from discord-py-slash-command import SlashCommands. You can refer the code below:

    import discord
    from discord_slash import SlashCommand
    client = discord.Client(intents=discord.Intents.all())
    slash = SlashCommand(client, sync_commands=True) # Declares slash commands through the client.
    
    guild_ids = [1234567890] # Put your server IDs in this array.
    
    @slash.slash(name="ping", guild_ids=guild_ids)
    async def _ping(ctx):
        await ctx.send("Pong!")
    
    client.run("token")