Skip to content

Commit a1b8a4a

Browse files
committed
Format application command examples
1 parent 303d085 commit a1b8a4a

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

examples/app_commands/slash_autocomplete.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import discord
2-
from discord.commands import slash_command, Option
2+
from discord.commands import Option, slash_command
3+
34
bot = discord.Bot()
45

56
COLORS = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
@@ -85,15 +86,19 @@
8586
"yellowgreen",
8687
]
8788

88-
BASIC_ALLOWED = [...] # this would normally be a list of discord user IDs for the purpose of this example
89+
BASIC_ALLOWED = [
90+
...
91+
] # this would normally be a list of discord user IDs for the purpose of this example
8992

9093

9194
async def color_searcher(ctx: discord.AutocompleteContext):
9295
"""Returns a list of matching colors from the LOTS_OF_COLORS list
9396
In this example, we've added logic to only display any results in the returned list if the user's ID exists in the BASIC_ALLOWED list.
9497
This is to demonstrate passing a callback in the discord.utils.basic_autocomplete function.
9598
"""
96-
return [color for color in LOTS_OF_COLORS if ctx.interaction.user.id in BASIC_ALLOWED]
99+
return [
100+
color for color in LOTS_OF_COLORS if ctx.interaction.user.id in BASIC_ALLOWED
101+
]
97102

98103

99104
async def get_colors(ctx: discord.AutocompleteContext):
@@ -115,7 +120,9 @@ async def get_animals(ctx: discord.AutocompleteContext):
115120
elif picked_color == "blue":
116121
return ["blue jay", "blue whale"]
117122
elif picked_color == "indigo":
118-
return ["eastern indigo snake"] # needs to return an iterable even if only one item
123+
return [
124+
"eastern indigo snake"
125+
] # needs to return an iterable even if only one item
119126
elif picked_color == "violet":
120127
return ["purple emperor butterfly", "orchid dottyback"]
121128
else:
@@ -129,14 +136,26 @@ async def autocomplete_example(
129136
animal: Option(str, "Pick an animal!", autocomplete=get_animals),
130137
):
131138
"""This demonstrates using the ctx.options parameter to to create slash command options that are dependent on the values entered for other options."""
132-
await ctx.respond(f"You picked {color} for the color, which allowed you to choose {animal} for the animal.")
139+
await ctx.respond(
140+
f"You picked {color} for the color, which allowed you to choose {animal} for the animal."
141+
)
133142

134143

135144
@bot.slash_command(name="ac_basic_example")
136145
async def autocomplete_basic_example(
137146
ctx: discord.ApplicationContext,
138-
color: Option(str, "Pick a color from this big list", autocomplete=discord.utils.basic_autocomplete(color_searcher)), # Demonstrates passing a callback to discord.utils.basic_autocomplete
139-
animal: Option(str, "Pick an animal from this small list", autocomplete=discord.utils.basic_autocomplete(["snail", "python", "cricket", "orca"])), # Demonstrates passing a static iterable discord.utils.basic_autocomplete
147+
color: Option(
148+
str,
149+
"Pick a color from this big list",
150+
autocomplete=discord.utils.basic_autocomplete(color_searcher),
151+
), # Demonstrates passing a callback to discord.utils.basic_autocomplete
152+
animal: Option(
153+
str,
154+
"Pick an animal from this small list",
155+
autocomplete=discord.utils.basic_autocomplete(
156+
["snail", "python", "cricket", "orca"]
157+
),
158+
), # Demonstrates passing a static iterable discord.utils.basic_autocomplete
140159
):
141160
"""This demonstrates using the discord.utils.basic_autocomplete helper function.
142161
For the `color` option, a callback is passed, where additional logic can be added to determine which values are returned.
@@ -145,4 +164,5 @@ async def autocomplete_basic_example(
145164
Note that the basic_autocomplete function itself will still only return a maximum of 25 items."""
146165
await ctx.respond(f"You picked {color} as your color, and {animal} as your animal!")
147166

167+
148168
bot.run("TOKEN")

examples/app_commands/slash_basic.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Note: If you want you can use commands.Bot instead of discord.Bot
77
# Use discord.Bot if you don't want prefixed message commands
88

9-
# With discord.Bot you can use @bot.command as an alias
9+
# With discord.Bot you can use @bot.command as an alias
1010
# of @bot.slash_command but this is overridden by commands.Bot
1111

1212

@@ -28,7 +28,9 @@ async def joined(
2828
ctx, member: discord.Member = None
2929
): # Passing a default value makes the argument optional
3030
user = member or ctx.author
31-
await ctx.respond(f"{user.name} joined at {discord.utils.format_dt(user.joined_at)}")
31+
await ctx.respond(
32+
f"{user.name} joined at {discord.utils.format_dt(user.joined_at)}"
33+
)
3234

3335

3436
# To learn how to add descriptions and choices to options, check slash_options.py

examples/app_commands/slash_cog.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
from discord.commands import slash_command # Importing the decorator that makes slash commands.
12
from discord.ext import commands
2-
from discord.commands import slash_command # Importing the decorator that makes slash commands.
3+
34

45
class Example(commands.Cog):
56
def __init__(self, bot):
67
self.bot = bot
7-
8-
@slash_command(guild_ids=[...]) # Create a slash command for the supplied guilds.
8+
9+
@slash_command(guild_ids=[...]) # Create a slash command for the supplied guilds.
910
async def hello(self, ctx):
1011
await ctx.respond("Hi, this is a slash command from a cog!")
11-
1212

13-
@slash_command() # Not passing in guild_ids creates a global slash command (might take an hour to register).
13+
@slash_command() # Not passing in guild_ids creates a global slash command (might take an hour to register).
1414
async def hi(self, ctx):
1515
await ctx.respond("Hi, this is a global slash command from a cog!")
1616

17+
1718
def setup(bot):
1819
bot.add_cog(Example(bot))

examples/app_commands/slash_options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ async def hello(
1616
):
1717
await ctx.respond(f"Hello {name}")
1818

19-
bot.run('TOKEN')
19+
20+
bot.run("TOKEN")

examples/app_commands/slash_perms.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import discord
2-
32
# Imports permissions from discord.commands
43
from discord.commands import permissions
54

@@ -8,7 +7,7 @@
87
# Note: If you want you can use commands.Bot instead of discord.Bot
98
# Use discord.Bot if you don't want prefixed message commands
109

11-
# With discord.Bot you can use @bot.command as an alias
10+
# With discord.Bot you can use @bot.command as an alias
1211
# of @bot.slash_command but this is overridden by commands.Bot
1312

1413
# by default, default_permission is set to True, you can use
@@ -31,45 +30,60 @@
3130
# Note: Please replace token, GUILD_ID, USER_ID and ROLE_NAME.
3231

3332
# Guild Slash Command Example with User Permissions
33+
34+
GUILD_ID = ...
35+
USER_ID = ...
36+
37+
3438
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
3539
@permissions.is_user(USER_ID)
3640
async def user(ctx):
3741
"""Say hello to the author""" # the command description can be supplied as the docstring
3842
await ctx.respond(f"Hello {ctx.author}!")
3943

44+
4045
# Guild Slash Command Example with Owner Permissions
4146
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
4247
@permissions.is_owner()
4348
async def owner(ctx):
4449
"""Say hello to the author""" # the command description can be supplied as the docstring
4550
await ctx.respond(f"Hello {ctx.author}!")
4651

52+
4753
# Guild Slash Command Example with Role Permissions
4854
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
4955
@permissions.has_role("ROLE_NAME")
5056
async def role(ctx):
5157
"""Say hello to the author""" # the command description can be supplied as the docstring
5258
await ctx.respond(f"Hello {ctx.author}!")
5359

60+
5461
# Guild Slash Command Example with Any Specified Role Permissions
5562
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
5663
@permissions.has_any_role("ROLE_NAME", "ROLE_NAME2")
5764
async def multirole(ctx):
5865
"""Say hello to the author""" # the command description can be supplied as the docstring
5966
await ctx.respond(f"Hello {ctx.author}!")
6067

68+
6169
# Guild Slash Command Example with Permission Decorator
6270
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False)
6371
@permissions.permission(user_id=USER_ID, permission=True)
6472
async def permission_decorator(ctx):
6573
"""Say hello to the author""" # the command description can be supplied as the docstring
6674
await ctx.respond(f"Hello {ctx.author}!")
6775

76+
6877
# Guild Slash Command Example with Permissions Kwarg
69-
@bot.slash_command(guild_ids=[GUILD_ID], default_permission=False, permissions=[permissions.Permission(id=USER_ID, type=2, permission=True)])
78+
@bot.slash_command(
79+
guild_ids=[GUILD_ID],
80+
default_permission=False,
81+
permissions=[permissions.Permission(id=USER_ID, type=2, permission=True)],
82+
)
7083
async def permission_kwarg(ctx):
7184
"""Say hello to the author""" # the command description can be supplied as the docstring
7285
await ctx.respond(f"Hello {ctx.author}!")
7386

87+
7488
# To learn how to add descriptions, choices to options check slash_options.py
7589
bot.run("token")

0 commit comments

Comments
 (0)