Skip to content

Commit 0711ee7

Browse files
authored
Add slash command example to quickstart (#639)
1 parent 8e5ef7e commit 0711ee7

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

docs/quickstart.rst

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
.. currentmodule:: discord
66

77
Quickstart
8-
============
8+
==========
99

1010
This page gives a brief introduction to the library. It assumes you have the library installed,
1111
if you don't check the :ref:`installing` portion.
1212

1313
A Minimal Bot
14-
---------------
14+
-------------
1515

1616
Let's make a bot that responds to a specific message and walk you through it.
1717

@@ -40,7 +40,7 @@ It looks something like this:
4040
Let's name this file ``example_bot.py``. Make sure not to name it ``discord.py`` as that'll conflict
4141
with the library.
4242

43-
There's a lot going on here, so let's walk you through it step by step.
43+
There's a lot going on here, so let's walk you through it step by step:
4444

4545
1. The first line just imports the library, if this raises a `ModuleNotFoundError` or `ImportError`
4646
then head on over to :ref:`installing` section to properly install.
@@ -77,3 +77,41 @@ On other systems:
7777
$ python3 example_bot.py
7878
7979
Now you can try playing around with your basic bot.
80+
81+
A Minimal Bot with Slash Commands
82+
---------------------------------
83+
84+
As a continuation, let's create a bot that registers a simple slash command!
85+
86+
It looks something like this:
87+
88+
.. code-block:: python3
89+
90+
import discord
91+
92+
bot = discord.Bot()
93+
94+
@bot.event
95+
async def on_ready():
96+
print(f"We have logged in as {bot.user}")
97+
98+
@bot.slash_command(guild_ids=[your, guild_ids, here])
99+
async def hello(ctx):
100+
await ctx.respond("Hello!")
101+
102+
bot.run("your token here")
103+
104+
Let's look at the differences compared to the previous example, step-by-step:
105+
106+
1. The first line remains unchanged.
107+
2. Next, we create an instance of :class:`.Bot`. This is different from :class:`.Client`, as it supports
108+
slash command creation and other features, while inheriting all the features of :class:`.Client`.
109+
3. We then use the :meth:`.Bot.slash_command` decorator to register a new slash command.
110+
The ``guild_ids`` attribute contains a list of guilds where this command will be active.
111+
If you omit it, the command will be globally available, and may take up to an hour to register.
112+
4. Afterwards, we trigger a response to the slash command in the form of a text reply. Please note that
113+
all slash commands must have some form of response, otherwise they will fail.
114+
6. Finally, we, once again, run the bot with our login token.
115+
116+
117+
Congratulations! Now you have created your first slash command!

0 commit comments

Comments
 (0)