|
5 | 5 | .. currentmodule:: discord
|
6 | 6 |
|
7 | 7 | Quickstart
|
8 |
| -============ |
| 8 | +========== |
9 | 9 |
|
10 | 10 | This page gives a brief introduction to the library. It assumes you have the library installed,
|
11 | 11 | if you don't check the :ref:`installing` portion.
|
12 | 12 |
|
13 | 13 | A Minimal Bot
|
14 |
| ---------------- |
| 14 | +------------- |
15 | 15 |
|
16 | 16 | Let's make a bot that responds to a specific message and walk you through it.
|
17 | 17 |
|
@@ -40,7 +40,7 @@ It looks something like this:
|
40 | 40 | Let's name this file ``example_bot.py``. Make sure not to name it ``discord.py`` as that'll conflict
|
41 | 41 | with the library.
|
42 | 42 |
|
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: |
44 | 44 |
|
45 | 45 | 1. The first line just imports the library, if this raises a `ModuleNotFoundError` or `ImportError`
|
46 | 46 | then head on over to :ref:`installing` section to properly install.
|
@@ -77,3 +77,41 @@ On other systems:
|
77 | 77 | $ python3 example_bot.py
|
78 | 78 |
|
79 | 79 | 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