Skip to content

Instantly share code, notes, and snippets.

@AbstractUmbra
Last active July 14, 2024 16:01
Show Gist options
  • Save AbstractUmbra/a9c188797ae194e592efe05fa129c57f to your computer and use it in GitHub Desktop.
Save AbstractUmbra/a9c188797ae194e592efe05fa129c57f to your computer and use it in GitHub Desktop.
discord.py 2.0+ slash command info and examples

This gist has now been 'deprecated' and has moved...

... to my blog style space for easier contribution by third parties and to provide what I believe to be an easier reading experience. Please field all enquiries and issues to the source repository.

@AbstractUmbra
Copy link
Author

Hello Umbra, want to suggest some changes for #file-9-subclassing_group-py...

- # this a app command group
+ # this is an App command group
- # you can nest these up 1: 
- # `/group group` = VALID
+ # Valid combinations:
+ # /group subcommand (up to 25)
+ # /group subcommand group
+ # /group subcommand group subcommand (up to 25)
- # /group group group` = INVALID
- # etc... all invalid.

# this example shows one way to do this, subclassing
# the other is constructing an instance of app_commands.Group()
# that one is shown in "free_function_commands-py"

import discord
from discord import app_commands

- # the @app_commands.guilds and others (including checks) can be used above the class
+ # checks like @app_commands.guilds and @app_commands.default_permissions can be used above the class
- # these will apply to ALL subcommand, subcommands cannot have invidual perms!
+ # these will apply to all subcommands, subcommands cannot have individual perms.
@app_commands.guild_only()
class Group(app_commands.Group):

Done.

@lpkeates
Copy link

I know this is old, but do you have anything to add an input in the command, like /ban user:<user> reason:[reason] with <> being required and [] is optional?

@DoubleF3lix
Copy link

I know this is old, but do you have anything to add an input in the command, like /ban user:<user> reason:[reason] with <> being required and [] is optional?

You can give it a default value of something like None and check that in your function accordingly.

@KenwoodFox
Copy link

KenwoodFox commented Jan 12, 2023

I know this is old, but do you have anything to add an input in the command, like /ban user:<user> reason:[reason] with <> being required and [] is optional?

You can give it a default value of something like None and check that in your function accordingly.

Im actually having trouble with this too haha! How do you read in "the rest of the message" my old way was to just

*args

But now i cant do that because of typing, *args: tuple doesn't work, nor does str, I'm at my wits end!

@AbstractUmbra
Copy link
Author

@KenwoodFox

The reason for this is not discord.py related. Slash commands themselves have no concept of "consumption args" or varargs like ext.commands implements.

You simply cannot do this this way.

The recommend way is to take a str input and then .split() it with a clear separator, or have N many arguments.
Sad but this is the most recommended ways of doing it.

@KenwoodFox
Copy link

@KenwoodFox

The reason for this is not discord.py related. Slash commands themselves have no concept of "consumption args" or varargs like ext.commands implements.

You simply cannot do this this way.

The recommend way is to take a str input and then .split() it with a clear separator, or have N many arguments. Sad but this is the most recommended ways of doing it.

That makes it really hard for users who don't usually have concepts of wrapping strings with " or using other deliminaters than space :c i might just not migrate these commands that expect full sentence strings until something can be figured out.

@InterStella0
Copy link

Hyperlink is outdated for CommandTree.copy_global_to at Syncing gotchas.

@AbstractUmbra
Copy link
Author

Hyperlink is outdated for CommandTree.copy_global_to at Syncing gotchas.

Thanks! Resolved.

@D-Stacks
Copy link

can anyone tell me why i am unable to use slash commands while dming my bot? i have them in a tree, and sync without specifying any guilds, i.e. globally, i assumed this would sync to dms as well. but it just won't work. I know it is possible in theory to dm slash-commands, but i just see no documentation on how to achieve this with discord.py...

@AerixBlox1
Copy link

show me the code @D-Stacks

@AbstractUmbra
Copy link
Author

AbstractUmbra commented Jul 14, 2024

Please don't use this comment section of the gist for debugging or helping, join the discord.py server for that.
Every comment here notifies all watchers, which is usually most people who comment!

I'd recommend that if you're sick of the email spam from this gist, hitting the unsubscribe button at the top right of the page.

(I cannot lock comments here, hence this warning).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment