import discordfrom discord.app import Optionbot = discord.Bot()# Note: If you want you can use commands.Bot instead of discord.Bot# Use discord.Bot if you don't want prefixed message commands# With discord.Bot you can use @bot.command as an alias # of @bot.slash_command but this is overriden by commands.Bot@bot.slash_command(guild_ids=[...])# create a slash command for the supplied guildsasyncdefhello(ctx):"""Say hello to the bot"""# the command description can be supplied as the docstringawait ctx.send(f"Hello {ctx.author}!")@bot.slash_command( name="hi", description="Wish someone!")# Not passing in guild_ids creates a global slash command (might take an hour to register)asyncdefglobal_command(ctx,num:int): # Takes one integer parameterawait ctx.send(f"This is a global command, {num}!")@bot.slash_command(guild_ids=[...])asyncdefjoined(ctx,member: discord.Member =None): # Passing a default value makes the argument optional user = member or ctx.authorawait ctx.send(f"{user.name} joined at {discord.utils.format_dt(user.joined_at)}")choose = bot.command_group( name='choose', description='This is a slash command group!', guild_ids=[...])@choose.command(name='member')asyncdefchoose_a_user(ctx,user:Option(discord.Member, "Choose a member", required=False),#required is True by default):await ctx.send(f"You chose {user}!")@choose.command()asyncdefchoose_a_channel(ctx,channel:Option(discord.Channel, "Choose a channel"),):await ctx.send(f"You chose {channel}!")bot.run("TOKEN")
You need to have Py-cord version 2.0 Alpha to use Slash Commands